SoFunction
Updated on 2025-04-07

Basic commands and usage methods for bat batch processing

Batch commands are a tool used to automate tasks in a Windows operating system. Batch commands usually have .bat or .cmd as extensions. Some commonly used batch processing commands are as follows:

  • echo: Used to display text information on the console.
  • set: used to create or modify environment variables.
  • if: used to make conditional judgment in batch files.
  • for: Used to loop through data in a file or list.
  • goto: Used to jump to the specified tag in the batch file.
  • call: Used to call another batch file.
  • md: Used to create a new directory.
  • cd: used to switch to the current directory.
  • del: used to delete files.
  • copy: Used to copy files.
  • xcopy: Used to copy directories.
  • start: Used to start an application or file.
  • taskkill: Used to terminate running tasks.
  • ping: used to test network connections.
  • netstat: Used to display network status.

echo: Use the echo command to display text information on the console, for example:

echo Hello World!

set: Use the set command to create or modify environment variables, for example:

set name=John
echo My name is %name%

If: Use the if command to make conditional judgments in batch files, for example:

set num=10
if %num% lss 20 (
  echo %num% is less than 20
) else (
  echo %num% is greater than or equal to 20
)

for: Use the for command to loop through data in a file or list, for example:

for /f "tokens=1" %i in () do (
  echo %i
)

goto: Use the goto command to jump to the specified tag in the batch file, for example:

goto start
echo This line will not be executed
:start
echo This line will be executed

call: Use the call command to call another batch file, for example:

call 
echo This line will be executed after 

md: Use the md command to create a new directory, for example:

md 123456

cd: Use the cd command to switch to the current directory, for example:

cd newfolder

del: Use the del command to delete files, for example:

del 

The copy command can copy files, for example:

copy  

Example: Copy to all subdirectories of the current directory

Before copying "" to the subdirectory, use the if exist command to determine whether "" already exists in the subdirectory. If it exists, copy the file. Otherwise, no copying. And make a corresponding prompt. Before running the script, make sure "" is in the current directory.

@echo off
for /d %%d in (*) do (
  if exist "%%d\" (
    echo %%d"...........exist..............."
  ) else (
    copy  "%%d\"
  echo %%d "...........ok..............."
  )
)
pause

xcopy: Use the xcopy command to copy directories, for example:

xcopy sourcefolder destinationfolder /s

pause: Use the pause command to pause the execution of batch files and wait for the user to press any key, for example:

pause

Here are some other examples: from answering questions from netizens in Tieba

Copy the data in multiple txt files at fixed two lines of location and put the read data together in the same new txt file.

@echo off
set "outputfile="
 REM Clear output file
 echo. > %outputfile%
 REM traversal of all txt files
 for /f "delims=" %%i in ('dir /b *.txt') do (
  REM Read the second and third lines of each file
  for /f "skip=1 tokens=*" %%j in ('type "%%i"') do (
    echo %%j >> %outputfile%
    goto :next
  )
  :next
  for /f "skip=2 tokens=*" %%j in ('type "%%i"') do (
    echo %%j >> %outputfile%
  )
)
echo Data has been extracted to the file %outputfile% middle。

Automatically connect to large network printers with batch files, which is common to win7 and win10

@echo off
set printer=\\network-printer\printer-name
rundll32 ,PrintUIEntry /in /n "%printer%"

This script uses the rundll32 command to connect to a network printer. You just need to modify the value of the printer variable and replace it with the name and path of the network printer. Before running the script, make sure you already have access to the network printer.

Eliminate the end of the file name -number and rename it. For example, modify it to , modify it to

@echo off
for %%i in (*.*) do (
  set "filename=%%~ni"
  set "extension=%%~xi"
  set "newname="
 REM If the last four characters of the file name are "-number" Format, remove this suffix
 if "!filename:~-5!"=="-?????" (
    set "newname=!filename:~0,-6!!extension!"
  ) else (
    set "newname=%%i"
   )
 REM If the new file name is different from the original file name, rename the file
 if not "!newname!"=="%%i" (
    echo Rename "%%i" for "!newname!"
    ren "%%i" "!newname!"
  )
)

This script uses for loop to iterate through all files in the current directory, comparing the last four characters of each file name to the "-number", and if it is in that format, it will be removed. If the new file name is different from the original file name, rename the file.

This is the article about the basic commands and usage methods of bat batch processing. For more related basic commands of bat batch processing, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!