1. 传入参数
假设我们有一个test.bat,代码如下:
test.exe "abc.txt"
我们希望在cmd中传入自定义文件名称,batch file的代码可以改成
test.exe %1
然后cmd中可以输入test.bat abc.txt或者test.bat me.data,即可作为参数传递给test.exe
2.Call
如果调用非exe,要加上call,比如call abc.bat,否则如果有如下命令:
abc.bat
dd.exe
没有call的话,dd.exe不会执行,所以 应该这样:
call abc.bat
dd.exe
[1] http://stackoverflow.com/questions/4825746/cmd-exe-when-to-use-call-to-run-external-programs
3.常用设置
@rem表示注释行还可以用::
@echo off表示不输出cmd自带结果,除非你显示调用echo函数
@setlocal enableextensions enabledelayedexpansion设置环境变量可见性
endlocal结束设置环境变量可见性
4. 局部变量
Set Filename="abc.txt"
set aaa = %Filename%
for 循环的变量可以用%%x详见7
5. 循环、if and goto以及动态赋值
for:
for /l %%x in (1,1,100) do (
echo %%x
copy %%x.txt z\whatever\tect
)
左括号要在第一行,不能在新行
in cmd use %x instead of %%x
不能用::作注释只能用@rem
set newest=%a%
echo !newest!由于for是循环,赋值大部分都是动态,所以用动态取值(execution time rather than at parse time)!newest!而不是%newest% 还例如findstr /m /c:"my" !newest!连%errorlevel%也要改成!errorlevel!
要打印%today%要加引号"%today%"而!newest!无需引号
if:
if %aa%==5 echo 6
if %aa%==5 (
echo 6
)
没有else,要用多重if来实现
goto:
@echo off
setlocal enabledelayedexpansion
IF 5==5 IF 4==3 (
GOTO END
)
echo 2
:END
ECHO DONE
这里goto相当于break或者return
6. 等效and/or操作符的代码
AND:
if %age% geq 2 if %age% leq 12 set class=child
OR:
set res=false
if %hour% leq 6 set res=true
if %hour% geq 22 set res=true
if "%res%"=="true" (
set state=asleep
)
NOT:
if not a==5 echo 6
9. 文件和路径
执行非当前路径的命令---执行后返回到之前目录
pushd D:\a
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
copy "%newest%" D:\b
popd
复制最新文件到D:\b然后回到原来目录
移动所有文件
move "*.txt" c:\
判断文件夹是否未空或者不含某类文件:
@echo off
for /F %%i in ('dir /b "c:\test directory\*.*"') do (
echo Folder is NON empty
goto :EOF
)
echo Folder is empty or does not exist
10. 字符串操作
可以参考
http://scripts.dragon-it.co.uk/scripts.nsf/docs/batch-search-replace-substitute!OpenDocument&ExpandSection=3&BaseTarget=East&AutoFramed
http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
10.1 在文档中找给定的字符串
findstr /c:"Job finished" abc.txt
if %errorlevel%==0 (
echo Given string is found
)
10.2 是否含有给定字符串
@setlocal enableextensions enabledelayedexpansion
@echo off
set str1=%1
if not %str1:bcd=%==%str1% echo It contains bcd
endlocal
str1是否含有bcd,用的方法是replace给定字符串再比较原来的,%str1:bcd=%是在str1中用空字符替换bcd如果给定字符串是变量,参见10.3
10.3 替代变量字符串
@echo off
setlocal enabledelayedexpansion
set string=This is my string to work with.
set find=my string
set replace=your replacement
call set string=%%string:!find!=!replace!%%
echo %string%
这里用了延迟赋值来替换
所以10.2如果bcd是变量可以变成if not %string%==%str1%
11. 睡眠sleep 30秒,ctrl+c中断
timeout /t 30 /nobreak
12. taskkill
taskkill /im calc.exe 终止所有计算机(一个或多个)
taskkill /f /fi "Windowtitle eq 5554:test" /im *(终止某一个指定程序)
13. Forfiles
http://ss64.com/nt/forfiles.html
--get files whose last modified date is older than 90 days
forfiles -p "C:\Users\KK\Documents\Test" /d -90
--loop in a dir and echo file name ("cmd /c echo @file" is default cmd)
forfiles -p "C:\Users\KK\Documents\Test" /d -90 /c "cmd /c echo @file"
--loop in a dir and see if any file exists in it(if not, error occurs)
forfiles /s -p "D:\ETLSourceFiles\Carmax\DEAR"
--loop in a dir and delete each directory whose last modified date is older than 90 days
forfiles -p "C:\Users\KK\Documents\Test" /d -90 /c "cmd /c IF @isdir == TRUE rd /s /q @path & echo @path >> abc.txt"
No comments:
Post a Comment