List directories in Batch











up vote
1
down vote

favorite












I have multiple folders with a structure like this inside:




aaa
000
important
v001
v002
v003
not_important
something_else
001
important
v001
v002
v003
not_important
something_else
002
important
v001
v002
v003
not_important
something_else
bbb
000
important
v001
v002
v003
not_important
something_else
001
important
v003
not_important
something_else
002
important
v001
v002
v003
not_important
something_else
ccc
000
important
v001
v002
v003
not_important
something_else
001
important
v001
v002
v003
v004
v005
not_important
something_else
002
important
v001
v002
v003
not_important
something_else


One of the folders is called v00?, which contains a specific version of a file.
Now I want to list all the dates of the latest files.



However here are my problems:




  • I assume I cant use a recursive function, because I have some folders that I don't care about (not_important in my example above)

  • I only want the lastest Version

  • The versions don't always start at one and are not limited by number

  • The Folder names are not an easy aaa, bbb, 000, structure and cannot be looped by name, the also not nessecarily repeat (Folder1 and Folder2 are not the same, just the amount of folders/layers that they go in)
    -I need to iterate through the outer folders as well.


How can I only filter out the latest Version of these folders?



I tried this, however I can't get multiple layers in with that function
and the "dir" command gives me everything



"FOR /D %%d IN (*.*) DO ("


How can I solve this?



My current code looks like this>



@echo off

setlocal

for /D %%d in (*.*) do (
echo %%d
cd %%d

for /D %%e in (*.*) do (
echo %%e
cd %%e
for /D %%f in (*.*) do (
cd %%f
echo %%f



Insert Here CD ??



for /f "tokens=*" %%1 in ('dir /b /ad /on v*') do (
set latestdir=%%1
)

echo Latest directory name=%latestdir%

cd %latestdir%fullres
pause
cd..


)
cd..
)
cd ..

)
pause









share|improve this question
























  • how do you determine what files you want? you have listed things that are NOT usable for filtering ... so what IS usable for a filter?
    – Lee_Dailey
    Nov 7 at 14:25










  • I want all the files in the important folder with the last version on it, where the folder has been created within the last 7 days - at the moment I fail at entering the important folder where the cd important doesn not work
    – Frezzley
    Nov 7 at 14:45












  • so ... is the folder actually named important?
    – Lee_Dailey
    Nov 7 at 14:52















up vote
1
down vote

favorite












I have multiple folders with a structure like this inside:




aaa
000
important
v001
v002
v003
not_important
something_else
001
important
v001
v002
v003
not_important
something_else
002
important
v001
v002
v003
not_important
something_else
bbb
000
important
v001
v002
v003
not_important
something_else
001
important
v003
not_important
something_else
002
important
v001
v002
v003
not_important
something_else
ccc
000
important
v001
v002
v003
not_important
something_else
001
important
v001
v002
v003
v004
v005
not_important
something_else
002
important
v001
v002
v003
not_important
something_else


One of the folders is called v00?, which contains a specific version of a file.
Now I want to list all the dates of the latest files.



However here are my problems:




  • I assume I cant use a recursive function, because I have some folders that I don't care about (not_important in my example above)

  • I only want the lastest Version

  • The versions don't always start at one and are not limited by number

  • The Folder names are not an easy aaa, bbb, 000, structure and cannot be looped by name, the also not nessecarily repeat (Folder1 and Folder2 are not the same, just the amount of folders/layers that they go in)
    -I need to iterate through the outer folders as well.


How can I only filter out the latest Version of these folders?



I tried this, however I can't get multiple layers in with that function
and the "dir" command gives me everything



"FOR /D %%d IN (*.*) DO ("


How can I solve this?



My current code looks like this>



@echo off

setlocal

for /D %%d in (*.*) do (
echo %%d
cd %%d

for /D %%e in (*.*) do (
echo %%e
cd %%e
for /D %%f in (*.*) do (
cd %%f
echo %%f



Insert Here CD ??



for /f "tokens=*" %%1 in ('dir /b /ad /on v*') do (
set latestdir=%%1
)

echo Latest directory name=%latestdir%

cd %latestdir%fullres
pause
cd..


)
cd..
)
cd ..

)
pause









share|improve this question
























  • how do you determine what files you want? you have listed things that are NOT usable for filtering ... so what IS usable for a filter?
    – Lee_Dailey
    Nov 7 at 14:25










  • I want all the files in the important folder with the last version on it, where the folder has been created within the last 7 days - at the moment I fail at entering the important folder where the cd important doesn not work
    – Frezzley
    Nov 7 at 14:45












  • so ... is the folder actually named important?
    – Lee_Dailey
    Nov 7 at 14:52













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have multiple folders with a structure like this inside:




aaa
000
important
v001
v002
v003
not_important
something_else
001
important
v001
v002
v003
not_important
something_else
002
important
v001
v002
v003
not_important
something_else
bbb
000
important
v001
v002
v003
not_important
something_else
001
important
v003
not_important
something_else
002
important
v001
v002
v003
not_important
something_else
ccc
000
important
v001
v002
v003
not_important
something_else
001
important
v001
v002
v003
v004
v005
not_important
something_else
002
important
v001
v002
v003
not_important
something_else


One of the folders is called v00?, which contains a specific version of a file.
Now I want to list all the dates of the latest files.



However here are my problems:




  • I assume I cant use a recursive function, because I have some folders that I don't care about (not_important in my example above)

  • I only want the lastest Version

  • The versions don't always start at one and are not limited by number

  • The Folder names are not an easy aaa, bbb, 000, structure and cannot be looped by name, the also not nessecarily repeat (Folder1 and Folder2 are not the same, just the amount of folders/layers that they go in)
    -I need to iterate through the outer folders as well.


How can I only filter out the latest Version of these folders?



I tried this, however I can't get multiple layers in with that function
and the "dir" command gives me everything



"FOR /D %%d IN (*.*) DO ("


How can I solve this?



My current code looks like this>



@echo off

setlocal

for /D %%d in (*.*) do (
echo %%d
cd %%d

for /D %%e in (*.*) do (
echo %%e
cd %%e
for /D %%f in (*.*) do (
cd %%f
echo %%f



Insert Here CD ??



for /f "tokens=*" %%1 in ('dir /b /ad /on v*') do (
set latestdir=%%1
)

echo Latest directory name=%latestdir%

cd %latestdir%fullres
pause
cd..


)
cd..
)
cd ..

)
pause









share|improve this question















I have multiple folders with a structure like this inside:




aaa
000
important
v001
v002
v003
not_important
something_else
001
important
v001
v002
v003
not_important
something_else
002
important
v001
v002
v003
not_important
something_else
bbb
000
important
v001
v002
v003
not_important
something_else
001
important
v003
not_important
something_else
002
important
v001
v002
v003
not_important
something_else
ccc
000
important
v001
v002
v003
not_important
something_else
001
important
v001
v002
v003
v004
v005
not_important
something_else
002
important
v001
v002
v003
not_important
something_else


One of the folders is called v00?, which contains a specific version of a file.
Now I want to list all the dates of the latest files.



However here are my problems:




  • I assume I cant use a recursive function, because I have some folders that I don't care about (not_important in my example above)

  • I only want the lastest Version

  • The versions don't always start at one and are not limited by number

  • The Folder names are not an easy aaa, bbb, 000, structure and cannot be looped by name, the also not nessecarily repeat (Folder1 and Folder2 are not the same, just the amount of folders/layers that they go in)
    -I need to iterate through the outer folders as well.


How can I only filter out the latest Version of these folders?



I tried this, however I can't get multiple layers in with that function
and the "dir" command gives me everything



"FOR /D %%d IN (*.*) DO ("


How can I solve this?



My current code looks like this>



@echo off

setlocal

for /D %%d in (*.*) do (
echo %%d
cd %%d

for /D %%e in (*.*) do (
echo %%e
cd %%e
for /D %%f in (*.*) do (
cd %%f
echo %%f



Insert Here CD ??



for /f "tokens=*" %%1 in ('dir /b /ad /on v*') do (
set latestdir=%%1
)

echo Latest directory name=%latestdir%

cd %latestdir%fullres
pause
cd..


)
cd..
)
cd ..

)
pause






windows powershell batch-file recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 14:54









DodgyCodeException

3,182423




3,182423










asked Nov 7 at 11:16









Frezzley

125112




125112












  • how do you determine what files you want? you have listed things that are NOT usable for filtering ... so what IS usable for a filter?
    – Lee_Dailey
    Nov 7 at 14:25










  • I want all the files in the important folder with the last version on it, where the folder has been created within the last 7 days - at the moment I fail at entering the important folder where the cd important doesn not work
    – Frezzley
    Nov 7 at 14:45












  • so ... is the folder actually named important?
    – Lee_Dailey
    Nov 7 at 14:52


















  • how do you determine what files you want? you have listed things that are NOT usable for filtering ... so what IS usable for a filter?
    – Lee_Dailey
    Nov 7 at 14:25










  • I want all the files in the important folder with the last version on it, where the folder has been created within the last 7 days - at the moment I fail at entering the important folder where the cd important doesn not work
    – Frezzley
    Nov 7 at 14:45












  • so ... is the folder actually named important?
    – Lee_Dailey
    Nov 7 at 14:52
















how do you determine what files you want? you have listed things that are NOT usable for filtering ... so what IS usable for a filter?
– Lee_Dailey
Nov 7 at 14:25




how do you determine what files you want? you have listed things that are NOT usable for filtering ... so what IS usable for a filter?
– Lee_Dailey
Nov 7 at 14:25












I want all the files in the important folder with the last version on it, where the folder has been created within the last 7 days - at the moment I fail at entering the important folder where the cd important doesn not work
– Frezzley
Nov 7 at 14:45






I want all the files in the important folder with the last version on it, where the folder has been created within the last 7 days - at the moment I fail at entering the important folder where the cd important doesn not work
– Frezzley
Nov 7 at 14:45














so ... is the folder actually named important?
– Lee_Dailey
Nov 7 at 14:52




so ... is the folder actually named important?
– Lee_Dailey
Nov 7 at 14:52












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










@echo off
setlocal enabledelayedexpansion

for /r /d %%A in (v00*) do (
pushd "%%~A.."
for /d %%B in (v00*) do set "subfolder=%%~B"
if not "!save!" == "!cd!" echo !cd!!subfolder!
set "save=!cd!"
popd
)


This recurses through all the directories in the current
directory
and searches for a match of v00*. It will
pushd into each matched directory parent dir found and
then the nested for loop searches for the last matching
subfolder of v00*. If the saved current directory
does not match the current directory then the matched
directory path will be echoed. The current directory
will be saved for the next loop and popd will return
to the initial directory.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188393%2flist-directories-in-batch%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    @echo off
    setlocal enabledelayedexpansion

    for /r /d %%A in (v00*) do (
    pushd "%%~A.."
    for /d %%B in (v00*) do set "subfolder=%%~B"
    if not "!save!" == "!cd!" echo !cd!!subfolder!
    set "save=!cd!"
    popd
    )


    This recurses through all the directories in the current
    directory
    and searches for a match of v00*. It will
    pushd into each matched directory parent dir found and
    then the nested for loop searches for the last matching
    subfolder of v00*. If the saved current directory
    does not match the current directory then the matched
    directory path will be echoed. The current directory
    will be saved for the next loop and popd will return
    to the initial directory.






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      @echo off
      setlocal enabledelayedexpansion

      for /r /d %%A in (v00*) do (
      pushd "%%~A.."
      for /d %%B in (v00*) do set "subfolder=%%~B"
      if not "!save!" == "!cd!" echo !cd!!subfolder!
      set "save=!cd!"
      popd
      )


      This recurses through all the directories in the current
      directory
      and searches for a match of v00*. It will
      pushd into each matched directory parent dir found and
      then the nested for loop searches for the last matching
      subfolder of v00*. If the saved current directory
      does not match the current directory then the matched
      directory path will be echoed. The current directory
      will be saved for the next loop and popd will return
      to the initial directory.






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        @echo off
        setlocal enabledelayedexpansion

        for /r /d %%A in (v00*) do (
        pushd "%%~A.."
        for /d %%B in (v00*) do set "subfolder=%%~B"
        if not "!save!" == "!cd!" echo !cd!!subfolder!
        set "save=!cd!"
        popd
        )


        This recurses through all the directories in the current
        directory
        and searches for a match of v00*. It will
        pushd into each matched directory parent dir found and
        then the nested for loop searches for the last matching
        subfolder of v00*. If the saved current directory
        does not match the current directory then the matched
        directory path will be echoed. The current directory
        will be saved for the next loop and popd will return
        to the initial directory.






        share|improve this answer














        @echo off
        setlocal enabledelayedexpansion

        for /r /d %%A in (v00*) do (
        pushd "%%~A.."
        for /d %%B in (v00*) do set "subfolder=%%~B"
        if not "!save!" == "!cd!" echo !cd!!subfolder!
        set "save=!cd!"
        popd
        )


        This recurses through all the directories in the current
        directory
        and searches for a match of v00*. It will
        pushd into each matched directory parent dir found and
        then the nested for loop searches for the last matching
        subfolder of v00*. If the saved current directory
        does not match the current directory then the matched
        directory path will be echoed. The current directory
        will be saved for the next loop and popd will return
        to the initial directory.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 7 at 16:23

























        answered Nov 7 at 15:51









        michael_heath

        2,5342416




        2,5342416






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188393%2flist-directories-in-batch%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()