How to Run Multiple Files in Batch Script

MD Aminul Islam Feb 02, 2024
How to Run Multiple Files in Batch Script

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:

CALL YourScript.bat

Below, we shared a large example of this topic. Suppose we created three codes shared below.

Inside the 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

Inside the 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.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Batch File