Suppose in a batch file:

cd C:\Program Files\path\test\ start someprg.exe 

How after executing these commands, go back to the directory to the batch file, wherever it is?

  • If you get a comprehensive answer, please click on the green check mark next to the answer. - nikita_sergeevich February

4 answers 4

In fact, both proposed solutions return the memorized working directory , which does not correspond to the literal condition of the problem:

go back to the directory to the batch file, wherever it is

You can change the directory to the one in which the batch file is located as follows:

 CD /DA:\SOME\ REM делаем что-либо с относительными путями CD /D "%~dp0" 
  • 3
    @Dexter, I find it difficult to answer right away. Specifically, this information is from CALL /? . I also often look at the third-party site ss64.com. In principle, I consider the topic of information and literature necessary for administrative scripting in NT worthy of a separate issue. - karmadro4
  • @ karmadro4, cool resource you planted! Are you a system administrator working? - Free_man
  • 3
    If you are very interested in the work of batch files in Windows, then I recommend reading two articles: - rsdn.ru/article/winshell/batanyca.xml - rsdn.ru/article/winshell/NTCommandProcessor.xml These are the best materials that I have met on the great and mighty . Another couple of useful resources: - robvanderwoude.com - commandwindows.com - evlanoff
  • @Dexter, @evlanoff, I still strongly recommend putting this in a separate question, because the directories are good, there are bad, and there are even from Microsoft. No comment on them is enough. - karmadro4
  • @evlanoff, the original links are " dead ." Now these articles are available only from the Wayback Machine: one , two . - ߊߚߤߘ

See help set in cmd

 set curdir=%cd% cd c:\ushare dir cd %curdir% 

Something like that.

  • or open another CMD and after executing the program, exit it, then you will be where it was again - sudo97
  • Not bad, but the @ karmadro4 option is much more flexible =) - Free_man

Sometimes it can be useful to use the commands PUSHD (move to a folder and remember the old one) and POPD (return to a memorized folder)

 PUSHD папка REM Что-то полезное делаем POPD 
  • So for sure better. On .bat (or is it right there?) I do not program (unlike * nix Shells). It just became interesting how to do it in windows, so I looked at help in cmd. What I found in a few minutes, then wrote. - avp
  • It's better through a variable) You declare set curdir =% cd% at the beginning of the file and use cd% curdir% in the whole file) - sys1n4

It is best to use variables in this case:

 set up=%cd% Rem здесь тело батника cd %up%