To do occasional automation needs,
past each write batch files are now checked.
Today the batch common operations used to be a record.
1. calls and parameter passing batch file
a.bat
./b.bat Hello Wrold REM 或 REM call b.bat Hello Wrold
b.bat
set param1=%1 set param2=%2 echo %param1% echo %param2%
The above were shot
Hello
andWorld
.
Note: The
above command set, there are no spaces around the equal sign
2. string concatenation interception
b.bat
set greeting=%param1%-%param2% echo %greeting%
Above will output:
Hello-World
3. string interception
Interception command format
SomeText:~FromIndex:Count
Example taken:
set name=CoderMonkey echo %name:~0,4%
Above will output:
Code
NOTE: intercept may be reversed (FromIndex <0)
4. Alternatively string
Replace command format
SomeText:source=target
Replace the sample:
set name=CoderMonkey echo %name:Monkey=Gorilla%
Above will output:
CoderGorilla
Note:
when there are spaces in the string, you need to add quotation marks
without a space, then do not add quotes
The common variable
%cd% current directory,当前目录 %date% 日期 %time% 时刻,包含毫秒
6. Create and delete folders
mkdir "foldername"
Create a folder
rmdir "folder_path"
Delete the specified folder
rmdir /s /q "folder_path"
/s
Means to delete the specified directory and its subdirectories and files in the
/q
representation silent treatment, do not ask
7. Output files, such as log log
echo "【要输出的文字信息,比如当前日期时刻】%date% %time%" > log.txt
The date will appear as:
2019/12/17
format,
time will output:13:58:28.24
If you want the date and time as a log file name,
because the/
heel:
are non-character file name is used,
here we use the Replace function mentioned above.Example:
REM 将`/`跟`:`替换为空 set logfilename=%date:/=%_%time::=_%.txt echo "%date% %time%Log信息" > "%logfilename%"
Here's log file is generated in the path of the current execution.
Needs to be generated in the specified path, then the log file name with full path to