windows bat batch program memo


Close the single command echo
command before adding @

Close all command
echo @echo off

Get parameter
% 1% 2% *
% ~ 1 remove quotes

Use shift / 1 when there are more than 9 parameters

Set the directory where the bat is located to the current directory

% 0 is the path of the current bat

cd /d %~dp0

Set (environment) variable
set PARAM = ppp

echo %PARAM%

注释
::this is comment
rem this is comment too

Call another bat
call other.bat arg1 arg2

Determine the return value of calling other programs or batch processing (note the spaces and parentheses and else position)
if errorlevel 0 (
... success
) else (
... failure
)
if not% errorlevel% == 0 ... fail

call other.bat arg1 arg2 && ... execute after success

call other.bat arg1 arg2 || ... execute after failure

The
best way to exit is to use the following lable + goto method
goto exit
: exit
can also use
exit exitcode to exit the cmd interpreter
exit / b exitcode to exit the current bat

Create directory
if not exist DIR mkdir DIR
copy directory
xcopy SRC DEST / s / e / y

Traverse the directories in the directory
for / d %% i in (DIR \ *) do (echo %% i)
Traverse the files in the directory
for %% i in (DIR \ *) do (echo %% i)
Recursively traverse the directories File
for / r DIR %% i in (*) do (echo %% i)
for / f "" %% i in ('dir / a: -d / b / s% 1') do (echo %% i)

%% ~ fi: indicates to obtain the absolute path information of the file
%% ~ di: indicates to obtain the drive letter where the file is located
%% ~ pi: indicates to obtain the path of the file, not including the drive letter information
%% ~ ni: indicates Get the file name of the file, without extension information
%% ~ xi: means to get the file's extension
%% ~ ti: means to get the file's last modification time
%% ~ zi: means to get the file's size
% % ~ nxi: means to get the file name and extension of the file, excluding drive letter and path information

One by one
for %% i in (aa bb cc) do (
echo %% i
)

Determine whether the string contains
echo% 1 | findstr "^ abc"> nul
echo% errorlevel%
or
if "% PP: .svn =%" == "% PP%" echo not contain .svn

Delaying the expansion of environment variables, pay attention when using variables in the loop body, if you do not delay expansion, the value of this variable will not change
setlocal EnableDelayedExpansion

for ... (
  call ...
  if !errorlevel! == 1 (
    echo !errorlevel!
  )
)

 Use the bat file to start cmd, and set the directory where the bat is located to the current directory
File: start_cmd_from_here.bat
------------------------------ ----------
cmd / k cd / d% ~ dp0

 

Guess you like

Origin www.cnblogs.com/lgc2003/p/12713491.html