How to create and run a batch file on Windows

Create a simple batch file

Step 1: Create a .txt file and type the following lines to this .txt file.

@ECHO OFF
ECHO Congratulations! Your first batch file executed successfully.
PAUSE

Step 2: Change the suffex of .txt file to .bat file.

Step 3: Double click this .bat file.

OKay, Now you have already created a simple batch file and run it. Please refer to figure 1 for the result.

Figure 1

How do you loop in a Windows batch file

@ECHO OFF
for /L %%a in(1, 1, 10) do (
    echo %%a
)
PAUSE

Note:
1. Must add /L, if you don’t add /L, the %%a value are 1, 1, 10, It means three times.

Please refer to figure 2 for the result.

Figure 2

How to make batch file wait?

Using “timeout /t 20” to wait 20 seconds.

How to run a exe in batch file?

Using “start /B xxx.exe” to run the xxx.exe.

How to redirect output from command prompt to txt file?

Using “command > xxx.txt” or “command >> xxx.txt” to save the output to xxx.txt.
“>” means that it will cover the content. The original content is gone.
“>” means that it will add the content to the file. The original content is still there.

Note:
1. There is a space between key words. For example, for /L %%a in (1, 1 10) do (). It must have a space between “in” and “(“.
2. It shouldn’t have comment in for loop. The below is incorrect.

for /L %%a in (1, 1, 10) do (
    echo %%a
    :: hello world
)

Have fun.

Reference:
https://www.windowscentral.com/how-create-and-run-batch-file-windows-10

Share this article to your social media
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments