I have one root directory from where I am running master batch file, which call another batch file(2nd batch file) present in sub directory of root directory.

After the execution of 2nd batch file, would like to remain in root directory:

@echo off set root_dir=C:\Users\milan\Desktop cd "%root_dir%\2nd_dir" call 2nd_batch.bat echo %cd% 

Last echo command should show me the patch of root directory.I tried doing this

cd "\%root_dir%\2nd_dir\" 

But it didn't work.

2

4 Answers

The problem you're facing is with how the call instruction works.

When you use the call instruction in a batch file, it will include that batch file into the current script and run its code. That means that all changes the new script makes to your environment are carried over to the previous batch file, as you have noticed.

You can use the start command instead of the call command to start the new batch file in a new process, thus not carrying over its changes to the previous batchfile.

Alternatively, you can store the current folder before executing the new script and return back to it after the script is finished.

Below are 2 examples. The :: row is a remark. You can copy it inside your script or ommit it if you want.

:: example that uses start cd "%root_dir%\2nd_dir" start 2nd_batch.bat echo %cd% 
:: example that stores and sets the path :: store current folder for retrieval set masterfolder=%cd% cd "%root_dir%\2nd_dir" call 2nd_batch.bat :: restore folder cd /d %masterfolder% echo %cd% 
1

Use setlocal for to save current settings (current drive, folder, environ, ...), and endlocal for to restore them.

[test1.bat]

@echo off setlocal echo test1-1 : %cd% call test2.bat echo test1-2 : %cd% endlocal echo test1-3 : %cd% 

[test2.bat]

echo test2-1 : %cd% cd \ echo test2-2: %cd% 

enter image description here

PS. setlocal/endlocal may be nested.

Open cmd.exe and do the following queries.

PUSHD/? POPD/? 

Problem solved.

A lot of answers can be found by doing queries on commands Listed when you use the help /? query.

PUSHD allows you to change directory in a similar way to CD, so:

PUSHD "DirectoryPath" 

However, PUSHD stores the previous directory for recall by POPD.

So in the situation you described, you PUSHD to the directory used by or needed for your other batch file, then when you need to return to the previous directory, just use

POPD 

And voilá, you're there.

To resolve this using PUSHD / POPD:

In your main batch file:

PUSHD "PathFor2ndBatch" call 2nd_batch.bat POPD echo %cd% 

If the aim is to have the second batch file execute in the directory of your main batch file:

PUSHD "PathFor2ndBatch" CALL 2nd_Batch.bat 

In the start of your 2nd batch:

POPD 
0

You can also start your batch file with /D switch:

start /? START [/D path] path Starting directory 

And so, running the below code will make the batch file run inside C:\users directory.

start /D C:\users C:\Windows\System32\RCWM\rmdir.bat 

Although, like Akina mentioned in the comments, start /w is shorter for this purpose.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy