none
Navigate to a directory's only subdirectory (batch file) RRS feed

  • Question

  • If I use the "cd" command to navigate to a directory in my C: drive via a batch file, and in that directory there's only one subdirectory but I don't know its name, is there a way I can navigate to that subdirectory? Is there some kind of environment variable I could use? Thanks in advance!
    • Edited by ZL217 Wednesday, July 23, 2014 8:10 PM
    Wednesday, July 23, 2014 8:05 PM

Answers

  • Here you go:

    @ECHO Off
    cd /d "d:\Test"
    for /d %%a in (*.*) do cd "%%a"

    • Marked as answer by ZL217 Wednesday, July 23, 2014 9:20 PM
    Wednesday, July 23, 2014 8:31 PM

All replies

  • Here you go:

    @ECHO Off
    cd /d "d:\Test"
    for /d %%a in (*.*) do cd "%%a"

    • Marked as answer by ZL217 Wednesday, July 23, 2014 9:20 PM
    Wednesday, July 23, 2014 8:31 PM
  • This works at a prompt:

    for /f %i in ('dir /ad /b') do cd %i

    If in a batch file it needs to be this:

    for /f %%i in ('dir /ad /b') do cd %%i


    ¯\_(ツ)_/¯

    Wednesday, July 23, 2014 8:33 PM