本人之前用powershell 写了1个备份文件的脚本
http://blog.csdn.net/nvd11/article/details/58375519
但是由于巨量问下搜索的性能问题..(原来powershell是.net写出来的,真是垃圾), 不得不用bat重写了..
需求跟原来的是一样的:
需求1: 检查每个文件的修改时间, 如果小于日期参数, 则备份(和删除);
需求2:保留被备份文件的目录结构。
windows bat下的forfiles还能一用, 当然用起来不能跟linux的find比
archive.bat
@echo off
setlocal enabledelayedexpansion
set sPathFrom=%1
set sPathTo=%2
set iDaysBefore=%3
set cnt=0
:loop
if "%1"=="" (echo %cnt%) else (set /a cnt+=1&shift /1&goto :loop)
if %cnt% lss 3 (
echo Usage: %0 ^<^<Source Path^>^> ^<^<Destination Path^>^> ^<^<beforeDate^>^>
goto errorEnd
)
set sDateToday=%DATE:~0,10%
set sLogPath=%cd%\logs
set sLogFile=%sLogPath%\archive%sDateToday%.log
if exist %sLogPath% (
echo log path is existed!
) else (
md %sLogPath%
)
rem replace / to -
set sLogFile=%sLogFile:/=-%
echo log file is %sLogFile%
echo source path is %sPathFrom% >> %slogFile%
echo Destination path is %sPathTo% >> %sLogFile%
echo archive files whose modified date is before %iDaysBefore% days>> %slogFile%
rem get the string len...
set n=0
:her
set u=!sPathFrom:~%n%,1!
if not "!u!"=="" (set/a n+=1
goto her
)
forfiles /p %sPathFrom% /s /d %iDaysBefore% /c "cmd /c %cd%\archiveCore.bat @path %sPathTo% %n% >> %slogFile%"
rem exit 0;
:errorEnd
rem exit -1;
archiveCore.bat 这个被上面的调用
@echo off
setlocal enabledelayedexpansion
set sFileFullName=%~1
set sFileName=%~n1%~x1
set sPathTo=%2
set lenGap=%3
rem get the string len...
set n=0
:her
set u=!sFileName:~%n%,1!
if not "!u!"=="" (set/a n+=1
goto her
)
set iNameLen=%n%
echo %sFileFullName%
echo %sPathTo%
rem echo %lenGap%
set sFilePathTo=%sPathTo%\!sFileFullName:~%lenGap%!
set sPathTo=!sFilePathTo:~0,-%iNameLen%!
rem echo %sFilePathTo%
rem echo %sPathTo%
if exist %sPathTo% (
echo path is existed!
) else (
echo creating folder %sPathTo%
md %sPathTo%
)
if exist %sFileFullName%\nul (
rem it's a folder
if exist %sFilePathTo% (
echo path is existed!
) else (
echo creating folder %sFilePathTo%
md %sFilePathTo%
)
) else (
echo copying %sFileFullName% To %sPathTo%
rem pause
xcopy /y %sFileFullName% %sPathTo%
echo removing %sFileFullName%
del %sFileFullName%
)
最后吐个槽, bat下字符串长度的方法要自己写, 真是mdzz!
还有1个日期处理是坨屎