DOS批处理常用脚本

项目文件(gif另存为zip):

http://hi.csdn.net/attachment/201107/25/0_131159522822z4.gif


:: 关闭提示
@echo off

:: 截取字符串(从0开始的5个字符)
set fullstring=hello,world
set substr=%var:~0,5% 
echo %substr%

:: 算数运算
set  a=1
set  b=2
set /a c=%a% + %b%
echo %c%

:: 提示输入语句
set /p var="设置var变量的值="

:: 判断语句 (字符串相等判断)
if "x%var%x" == "xx" (
	echo var is empty.
:: 跳转语句
	goto :lable_var_is_empty
) else (
	echo var is NOT empty.
)
:lable_var_is_empty

:: 判断语句 - 扩展的
if /i not "abc" lss "ABC" (
	echo abc is NOT less than ABC
) else (
	echo abc is less than ABC ?
)

:: 判断语句 - 文件(夹)是否存在
if exist config.ini (
	echo config.ini is present.
) else (
	echo config.ini is lost.
)

:: 判断语句 - 检查最后执行命令的结果
echo this command will set errorlevel to zero.
if not errorlevel 0 (
	echo errorlevel is %errorlevel%
) else (
	echo errorlevel is %errorlevel%
)

:: for 循环语句 - 打印文件(夹)列表
for %%i in (*) do (
	echo %%i
)

:: for 循环语句 - 递归搜索"路径G:"中的所有pdf文件
for /r g: %%i in (*.pdf) do (
	echo %%i
)

:: for 循环语句 - 读取文本文件中每一行记录
for /f %%i in (config.ini) do (
	echo %%i
)

:: for 循环语句 - 变量递增
for /L %%i in (0,1,10) do (
	echo %%i
)

:: for 循环语句 - 变量递减
for /L %%i in (100,-1,90) do (
	echo %%i
)

:: for 循环语句 - 解析字符串,提取第二个字符串
for /f "tokens=2 delims=," %%i in ("hello,kitty,hello,world") do (
	echo %%i
)

:: for 循环语句 - 执行命令的结果作为集合
for /f "delims==" %%i in ('set') do (
	echo %%i
)

:: 结束标签
:lable_end_of_program

:: 调试暂停
pause

:: 打开提示
@echo on


2011-9-13更新一个片段

:: for 循环语句 - 获取当期时间戳(注意:需要管理员权限)
for /f "skip=1 delims=." %%i in ('wmic os get LocalDateTime') do (
    set raw_string=%%i
)

:: 提取 年 月 日 时 分 秒
set year=%raw_string:~0,4%
set month=%raw_string:~4,2%
set day=%raw_string:~6,2%
set hour=%raw_string:~8,2%
set minute=%raw_string:~10,2%
set second=%raw_string:~12,2%

:: 测试结果
echo %year%-%month%-%day% %hour%:%minute%:%second%

2011-9-28 更新一个片段

:: 更改当前目录到脚本所在位置
cd %~dp0

2011-11-1 更新一个片段

:: WinSCP 命令行复制文件到远程机器(winsshd, sshd)
set scp=H:\workspace\winscp435\WinSCP.com
set netuser=foo
set netpass=bar
set netip=10.0.0.1
set netport=2222
if exist "%scp%" ( 
	%scp% /command^
        "open %netuser%:%netpass%@%netip%:%netport%"^
        "put a.txt /D/remote_dir/"^
        "close"^
        "exit"
) else (
	echo Error, No Such File: %scp%.
)

2011-11-3 更新片段

:: forfiles 遍历目录执行命令,类似linux下的find -exec echo '{}' \;
forfiles /p ".\test"^
		 /d +2011-11-03^
		 /c "cmd /c echo Deleting @file && del /f /s @path" 

2011-11-5 更新片段

:: 添加网络打印机
net use \\172.16.73.110\ipc$ tiger /user:administrator
rundll32 printui.dll,PrintUIEntry /in /n \\172.16.73.110\HPLaserJ /q /y

愚蠢的xcopy /t用法

rem 连同文件夹结构一起复制文件的命令
rem linux一个cp -rf 搞定
rem windows下那个叫别扭啊
xcopy /Y /e /s src dest
rem 一定要确保dest已经创建哦

使用forfiles(win server 2003 支持)执行命令的时候要注意 执行路径

forfiles /s /m include /c "%comspec% /c echo %cd% && xcopy /Y /e /s . %cd%\include"

删除svn文件夹
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN]
@="Delete SVN Folders"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN\command]
@="cmd.exe /c \"TITLE Removing SVN Folders in %1 && COLOR 9A && FOR /r \"%1\" %%f IN (.svn) DO RD /s /q \"%%f\" \""

白领定时休息脚本(复制下来另存为一个timer.vbs的文件,双击会自动调用wscipt.exe运行), 每隔30分钟提醒一次, 点"确定"会继续提醒,"取消"会关闭定时器.

while true
ret=msgbox("working for 30 minutes, have some water, now", 1)
if ret <> 1 then 
wscript.quit 
end if
WScript.Sleep 30*60*1000
wend

生成java网页工作环境

::@ECHO OFF
@cd  /d  %~dp0
@set WEBDEVROOT=%CD%
@for /d %%T IN (JDK*) DO @SET JAVA_HOME=%CD%\%%T
@set CATALINA_HOME=%WEBDEVROOT%\apache-tomcat-7.0.27
@set M2_HOME=%WEBDEVROOT%\apache-maven-3.0.4
@set MAVEN_OPTS=-Xms64m -Xmx256m
@set path=%APPENGINE_HOME%;%JAVA_HOME%\bin;%CATALINA_HOME%\bin;%M2_HOME%\bin;%PATH%

@ECHO #####################################################
@ECHO #WELCOME TO USE JAVA WEB DEV ENVIRONMENT#
@ECHO #####################################################
@ECHO WEBDEVROOT     --^> %WEBDEVROOT%
@ECHO JAVA_HOME      --^> %JAVA_HOME%
@ECHO CATALINA_HOME  --^> %WEBDEVROOT%\apache-tomcat-7.0.27
@ECHO M2_HOME        --^> %WEBDEVROOT%\apache-maven-3.0.4
@ECHO #####################################################
@%comspec%
@ECHO ON



你可能感兴趣的:(dos,批处理,script)