日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

bat 实现批量备份文件

發(fā)布時間:2025/3/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bat 实现批量备份文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本人之前用powershell 寫了1個備份文件的腳本


http://blog.csdn.net/nvd11/article/details/58375519


但是由于巨量問下搜索的性能問題..(原來powershell是.net寫出來的,真是垃圾), 不得不用bat重寫了..


需求跟原來的是一樣的:

需求1: 檢查每個文件的修改時間, 如果小于日期參數(shù), 則備份(和刪除);

需求2:保留被備份文件的目錄結(jié)構(gòu)。



windows bat下的forfiles還能一用, 當然用起來不能跟linux的find比


archive.bat

@echo off setlocal enabledelayedexpansion set sPathFrom=%1 set sPathTo=%2 set iDaysBefore=%3set 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%.logif 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 這個被上面的調(diào)用

@echo off setlocal enabledelayedexpansionset sFileFullName=%~1 set sFileName=%~n1%~x1 set sPathTo=%2 set lenGap=%3rem 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 folderif exist %sFilePathTo% (echo path is existed!) else (echo creating folder %sFilePathTo%md? %sFilePathTo%) ) else (echo copying %sFileFullName% To %sPathTo%rem pausexcopy /y %sFileFullName% %sPathTo%echo removing %sFileFullName%del %sFileFullName% )

最后吐個槽, bat下字符串長度的方法要自己寫, 真是mdzz!

還有1個日期處理是坨屎

總結(jié)

以上是生活随笔為你收集整理的bat 实现批量备份文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。