I am currently operating a master batch file from the main directory, and this file calls a second batch file located in a subdirectory of the main directory. Once the second batch file finishes running, I want to stay in the main directory:
@echo off
set root_dir=C:\Users\milan\Desktop
cd "%root_dir%\2nd_dir"
call 2nd_batch.bat
echo %cd%
I attempted to display the path of the main directory with the last “echo” command.
cd "\%root_dir%\2nd_dir\"
However, the command was unsuccessful in producing the desired result.
2 Answers
Introduction
Batch files are a great way to automate repetitive tasks on a Windows computer. They allow you to run a sequence of commands without having to manually enter them each time. However, when working with batch files, it’s important to understand the directory structure of your computer. In this blog post, we will explore whether there is a root directory from where you are running a batch file and how to remain in that directory after calling another batch file.
What is a Root Directory?
A root directory is the top-level directory in a file system. It is the starting point for all file paths on a computer. In Windows, the root directory is typically represented by the drive letter followed by a backslash (e.g., C: for the C drive). When you open a command prompt, you are typically in the root directory of the current drive.
Is There a Root Directory From Where You Are Running a Batch File?
Yes, when you run a batch file, it is executed from a specific directory. This directory is known as the current working directory. By default, the current working directory is set to the directory where the batch file is located. However, it can be changed using the “cd” command.
How to Remain in Root Directory After Calling Another Batch File?
To remain in the root directory after calling another batch file, you can use the “cd” command to change the directory back to the root directory. Here’s an example:
@echo off
set root_dir=C:UsersmilanDesktop
cd "%root_dir%2nd_dir"
call 2nd_batch.bat
cd "%root_dir%"
echo %cd%
In this example, we first set the “root_dir” variable to the path of the root directory. We then change the current working directory to the subdirectory “2nd_dir” and call the “2nd_batch.bat” file. After the second batch file has finished executing, we change the current working directory back to the root directory using the “cd” command. Finally, we use the “echo” command to display the current working directory, which should be the root directory.
What Happens if You Don’t Change Back to the Root Directory?
If you don’t change back to the root directory after calling another batch file, the current working directory will remain in the subdirectory where the second batch file was located. This can cause issues if you need to access files or run commands that are located in the root directory. It’s always a good practice to change back to the root directory after calling another batch file.
Conclusion
In conclusion, understanding the directory structure of your computer is important when working with batch files. There is a root directory from where you are running a batch file, and you can use the “cd” command to change the current working directory. To remain in the root directory after calling another batch file, you can change the directory back to the root directory using the “cd” command. It’s always a good practice to change back to the root directory after calling another batch file to avoid issues with accessing files or running commands.
The issue you are experiencing is related to how the “call” instruction works. When you use the “call” instruction in a batch file, it incorporates the referenced batch file into the current script and runs its code. This means that any modifications the new script makes to the environment will be carried over to the previous batch file, as you have already noticed.
To avoid this, you can use the “start” command instead of “call” to initiate the new batch file in a separate process, thereby preventing the transfer of changes to the previous batch file.
Alternatively, you can save the current directory before executing the new script and return to it after the script completes.
Below are two examples. The “::” line is a comment that you can include or omit in your script.
:: 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%