Home > Windows > Changing Directory in Windows Batch File

Changing Directory in Windows Batch File


Just noticed a small thing yesterday.


set curr_dir=%cd%

chdir /D J:\

REM...do your stuff...

chdir /D %curr_dir%

notice two important things here:

  • The script changes the current working directory to whatever it requires but then switches it back again so that if the user runs the script from a command window he is does not find himself in a strange directory after the script is finished
  • There is a /D switch that needs to be added to chdir so that it changes the drive if required. Without this chdir won’t work ( it had me baffled for quite some time 🙂 )
  1. nick
    October 1, 2010 at 5:14 am

    Thanks 😉

    found this off a google search. I have some batch files I inherited. Making the scripts more dynamic by using the subst command. So now the full path has a drive letter in it, whereas it used to just change to a sub directory. Probem is, it wasnt’ actually changing directories when I do an echo %cd% for the current directory.

    Adding the /d to a cd (chdir) command did the trick!

  2. Puff
    January 12, 2012 at 1:44 am

    Thank you so much this really helped me out!!

  3. Nik
    September 28, 2012 at 6:00 pm

    Thanks a ton, was making a batch file lanuncher but wasnt able to change drives, thanks.

  4. February 20, 2013 at 5:58 pm

    Thank you for posting this steps. It solved my problem !!!

  5. January 3, 2014 at 4:03 pm

    Работаю с этим сайтом http://obnalmc.blogspot.com/.
    Рекомендую. Все честно.

  6. Jaswerl
    July 2, 2014 at 2:10 am

    On a Windows 2008 server the root directory is C:Users/username/desktop. I cannot get a batch file to change the directory to the data drive (e:) regardless if i use \D, CD.., Pushd, setlocal or any other command. File:
    cmd
    SETLOCAL
    CD ..
    CD /D “e:\Sync Tool\programfolder\programsubfolder

    Is there somthing about the “Users” Folder in Windows that I don’t understand??

  7. Maks
    February 24, 2017 at 5:21 am

    It is kind of reinventing the wheel 🙂 Check pushd/popd commands. They will do the trick without need of storing values in local vars
    https://technet.microsoft.com/en-us/library/bb490978.aspx

  8. March 22, 2020 at 2:31 pm

    Kulitnya putih bersih, dengan betis yang woow, berambut menantang pastilah perempuan indah ini
    punya nafsu seksual yang liar, itu kata temanku yang pengalaman seksnya tinggi Buah dadanya terlihat menyembul dari balik gaun tidur
    itu, apalagi ketika dia melangkah di sampingku, samar-samar dari sudut mataku menonjol cantik payudaranya yang putih lembut

    “Uh , apa ini gara-gara film itu?”, batinku lagi

    Imajinasi bos aon338 mulai kurang didik, atau selama ini saya mengamati
    cs senantiasa mengaplikasikan kutang seksi dan berhijab jadi saya tak
    tahu wujud tubuhnya yang hakekatnya, segera saya
    memasukkan bayang-bayang ke dalam adegan film tadi

    “Hmm ”, tidak sadar mulutku mengeluarkan bunyi itu
    “Ada apa, Di?”, isteri bos aon338 itu memandangku dengan alis berkerut

    “Eh , kau ngelamun yah, ngelamunin siapa sih? cs yang
    seksi?”, tanyanya
    memperhatikan cs seksi dada bos dan leader aon338 berdesir
    sesaat pandangan mataku tertuju pada belahan dadanya

  1. No trackbacks yet.

Leave a comment