首页 > 代码库 > 常用的Windows批处理

常用的Windows批处理

 
切换执行路径
如果不换盘的话:
cd xxx
换盘:
cd /d xxx
 
获取当前日期

编写Windows批处理时经常会需要使用到日期和时间作为文件名,所以是非常重要的。

如何获取日期呢?
格式:  %date%
结果:  2012-07-31

如何获取时间呢?
格式:  %time%
结果:  10:21:21.68

这是获取的系统标准格式的日期和时间,但是有时候我们需要使用规定的格式,怎么办呢?

格式:   %date:~x,y%以及%time:~x,y%
说明:   x是开始位置,y是取得字符数

比如说我要获取完整的日期和时间,
格式:   %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%
结果:   20120731111039
但是如果时间早于10点,那么只显示日期不显示时间,如:20120731,如何解决呢?
格式:   %date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%
结果:   20120731052539

这样就达到我们的目标,成功的得到日期和时间了。

批处理文件同时打开多个文件夹

2006-09-23 00:05:36|  分类: 电脑日常应用 |  标签:批处理   |举报 |字号大中小 订阅



打开记事本,输入以下内容: 
(先假定,你要同时打开 C盘,D盘下的flash文件夹,还有C盘的system32文件夹) 

explorer.exe /n,C: 
explorer.exe /n,C:\windows 

explorer.exe /n,D:\flash

删除文件 文件夹

@echo off

 

rem 如果是磁盘符,最好加上\符合

set DESC_PATH=E:\

 

rem 只需要部分字符

set file_str=rar

set folder_str=hi6620

 

rem 控制开关

set search_file=1

set search_folder=1

set del_flag=1

 

rem 临时文件,备份记录用,不用修改

set file_log=file.log

set folder_log=folder.log

set workspace=%CD%

 

rem 只查找文件

if %search_file%==1 (

    cd /d %DESC_PATH%

    rem 搜寻子目录,排除文件夹

    dir /b /s /a-d | find "%file_str%" > %workspace%\%file_log%

)

 

rem 只查找目录

if %search_folder%==1 (

    cd /d %DESC_PATH%

    rem 目的:希望不统计文件在内

    dir /b /s /a-a | find "%folder_str%" > %workspace%\%folder_log%

)

 

rem 待考虑引入输入判断yes or no 决定是否删除,这样更人性

echo Warning:%workspace%\%file_log% and %folder_log%, please check ...

pause

 

cd /d %workspace%

if %del_flag%==1 (

    if %search_file%==1 (

        for /f %%i in (%workspace%\%file_log%) do  (

            if exist %%i del %%i

        )

    )

    if %search_folder%==1 (

        for /f %%i in (%workspace%\%folder_log%) do  (

            if exist %%i rd /s /q %%i

        )

    )

)

 

cd /d %workspace%

pause  

 

 

 

 

 

 

-----------自动编译打包程序的Windows命令

SET dotNetFrameworkPath=C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319
SET dotNetFrameworkDrive=c:

REM if exist D:\WorkTemp\Release
del /f /s /q D:\WorkTemp\Release
rd D:\WorkTemp\Release
echo ----------------清理完毕,开始生成--------------------------


if not exist %ibasWorkSpace%\Release\Services\ md %ibasWorkSpace%\Release\Services
rem============================IISPublish工程===========================
cd %dotNetFrameworkPath%
%dotNetFrameworkDrive%
REM ----------创建对应的模块的目录 +svc文件
msbuild %ibasSourceCode_TieLong%\BizSys.ContainerManagement.Service\BizSys.ContainerManagement.Service.csproj /t:ResolveReferences;Compile /t:_WPPCopyWebApplication /p:Configuration=Release /p:_ResolveReferenceDependencies=true /p:WebProjectOutputDir=%ibasWorkSpace%\Release\Services\CMService >>%ibasWorkSpace%/Release/PublishLog.txt

msbuild %ibasSourceCode_TieLong%\BizSys.SystemCenter.Service\BizSys.SystemCenter.Service.csproj /t:ResolveReferences;Compile /t:_WPPCopyWebApplication /p:Configuration=Release /p:_ResolveReferenceDependencies=true /p:WebProjectOutputDir=%ibasWorkSpace%\Release\Services\SCService >>%ibasWorkSpace%/Release/PublishLog.txt

 

@echo off


REM ----------发布启动壳中的zip文件+xap文件+bin 文件

copy /y %ibasSourceCode_TieLong%\BizSys.SystemCenter.Service\ClientBin\BSUi.SystemApplicationCenter.Silverlight.zip %ibasWorkSpace%\Release\Services\SCService\ClientBin\ >%ibasWorkSpace%/Release/CopyFileLog.txt
copy /y %ibasSourceCode_TieLong%\BizSys.SystemCenter.Service\ClientBin\BSUi.ApprovalProcess.Silverlight.zip %ibasWorkSpace%\Release\Services\SCService\ClientBin\ >>%ibasWorkSpace%/Release/CopyFileLog.txt
copy /y %ibasSourceCode_TieLong%\BizSys.SystemCenter.Service\ClientBin\BSUi.BusinessSystemCenter.Silverlight.xap %ibasWorkSpace%\Release\Services\SCService\ClientBin\ >>%ibasWorkSpace%/Release/CopyFileLog.txt
copy /y %ibasSourceCode_TieLong%\BizSys.SystemCenter.Service\bin\*.* %ibasWorkSpace%\Release\Services\SCService\bin\ >>%ibasWorkSpace%/Release/CopyFileLog.txt


echo waiting for clear the webConfig files ...... >>%ibasWorkSpace%/Release/PublishLog.txt

del /f /s /q D:\WorkTemp\Release\Services\Web.config
del /f /s /q D:\WorkTemp\Release\Services\SCService\ClientBin\*.ClientConfig
del /f /s /q D:\WorkTemp\Release\Services\SCService\*.html
del /f /s /q D:\WorkTemp\Release\Services\SCService\*.aspx
del /f /s /q D:\WorkTemp\Release\Services\SCService\*.js
del /f /s /q D:\WorkTemp\Release\Services\SCService\*.bat
del /f /s /q D:\WorkTemp\Release\Services\SCService\*.config
del /q D:\WorkTemp\Release\Services\SCService\Handler
del /q D:\WorkTemp\Release\Services\SCService\DataService
rd D:\WorkTemp\Release\Services\SCService\Handler
rd D:\WorkTemp\Release\Services\SCService\DataService
del /f /s /q D:\WorkTemp\Release\Services\CMService\*.xml
del /q D:\WorkTemp\Release\Services\CMService\DataService
rd /q D:\WorkTemp\Release\Services\CMService\DataService
REM copy /y %ibasSourceCode_TieLong%\BizSys.SystemCenter.Service\Web.config %ibasWorkSpace%\Release\Services\SCService\Web.config >>%ibasWorkSpace%/Release/CopyFileLog.txt


cd /d D:\WorkTemp\Release\Services
"%ibasTools%\7zip\7z.exe" a -tzip Services%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%.zip -r *.*

explorer.exe D:\WorkTemp\Release\
echo webConfig files has been cleared and the start Ptoject‘s config File has copied.. >>%ibasWorkSpace%/Release/PublishLog.txt
echo. & pause

 

 

7z压缩工具

http://url.cn/bTu4W8

 

常用的Windows批处理