bat 实现批量备份文件

本人之前用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个日期处理是坨屎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nvd11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值