Large scripts contain several files as it’s easy to maintain the code. When working with bigger scripts, you may need to divide them into modules to make detecting any coding errors or problems easier.
However, Batch does not have an object-oriented feature. You can create different files for different modules and run all of them based on your need and requirements from a core file.
This article will show how we can run multiple files from a single Batch script. Also, we will provide some examples and explanations to make the topic easier.
Run Multiple Files in Batch Script
We will use a built-in command called
CALL for this purpose. The general syntax for calling other script files from the current file is:
Below, we shared a large example of this topic. Suppose we created three codes shared below.
file1.bat, we have the below code:
ECHO This is from the first file
And inside the
file2.bat, we have the below code:
ECHO This is from the second file
file3.bat, we have the below code:
ECHO This is from the third file
Now, inside our core file that calls these files, we have the below code:
@echo off ECHO This is the core file that calls all three files... CALL "file1.bat" CALL "file2.bat" CALL "file3.bat"
Now when you run the file, you will get this output:
This is the core file that calls all three files... This is from the first file This is from the second file This is from the third file
Please note that if you are running the
.bat file outside these files’ directory, you need to make some edits to the code of the main file. The code will look like this:
@echo off ECHO This is the core file that calls all three files... CALL "G:\BATCH\file1.bat" CALL "G:\BATCH\file2.bat" CALL "G:\BATCH\file3.bat"
You only need to provide the complete directory of the files.
Remember that the commands we discussed here are only for the Windows command prompt or CMD environment.
- Rename Part of Filename in Batch Script
- Run Batch File Remotely
- Transfer File to Raspberry Pi Using Batch Script
- New Line in Batch Script
- Use Batch File to Write to a Text File