JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Running multiple files in a batch script

Author:JIYIK Last Updated:2025/03/20 Views:

Large scripts contain multiple files because it is easy to maintain the code. When working with larger scripts, you may want to divide them into modules to make it easier to detect any coding errors or problems.

However, Batch does not have object-oriented features. You can create different files for different modules and run all of them from the core file as per your need and requirement.

This article will show you how to run multiple files from a single batch script. In addition, we will provide some examples and explanations to make the topic easier.


Running multiple files in a batch script

To do this, we will use a built-in command called CALL. The general syntax for calling other script files from the current file is:

CALL YourScript.bat

Below, we share a big example of this topic. Suppose we create the three codes shared below.

In file1.bat, we have the following code:

ECHO This is from the first file

In file2.bat, we have the following code:

ECHO This is from the second file

In file3.bat, we have the following code:

ECHO This is from the third file

Now, in the core file that calls these files, we have the following 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 the following 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

Note that if you run the .bat file outside of these file directories, you will need to make some edits to the main file's code. The code would 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 full directory of the file.

Keep in mind that the commands we discuss here only work in the Windows Command Prompt, or CMD environment.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Difference between % and %% in batch files

Publish Date:2025/03/20 Views:200 Category:OPERATING SYSTEM

Batch programmers often confuse single percent signs (%) and double percent signs (%%) when used in batch files. The FOR command uses %f when executed on the command line, but in a batch file, it uses %%f instead of a single percent sign. T

Error handling in batch scripts

Publish Date:2025/03/20 Views:127 Category:OPERATING SYSTEM

Every scripting and programming language contains error handlers, for example Java contains try-catch for error handling. In batch scripts, there is no direct way to do this, but we can create error handlers in batch scripts using the built

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial