簡體   English   中英

將以前的版本復制並重命名為上次修改

[英]Copy and rename previous version to last modified

對於批量復制的目的,我需要創建我認為簡單的腳本,但沒有運氣。

用例是:將所有文件從sourcedirectory復制到targetdirectory ,其中sourcedirectory文件在targetdirectory中不存在,但如果targetdirectory中已經存在該文件,則在復制/覆蓋之前將其重命名為original_name.B<timestamp_last_modified>

我開始的:

xcopy %sourcedirectory% %targetdirectory% /f /i /s /h /d /y >> W:\Xcopy%datetimefile%.log 2>&1

但它不能重命名部分所以我開始玩游戲到達這里:

@echo off
rem configuration
set sourcedirectory="d:\Ice"
set targetdirectory="W:\Ice"
set logdirectory="W:"

rem following part only creates timestamp for log file (datetimefile) and log record (datetimecopy)
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimefile=%%I
set datetimecopy=%datetimefile:~0,8% %datetimefile:~8,2%:%datetimefile:~10,2%:%datetimefile:~12,2%
set datetimefile=%datetimefile:~0,8%_%datetimefile:~8,6%
set "logfile=%logdirectory:~0,-1%\Backup%datetimefile%.log^""

rem logging
echo Log File is %logfile%
echo -----     BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy%     -----
echo -----     BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy%     ----- >> %logfile% 2>&1

rem execution
for /F "tokens=*" %%A in ('xcopy %sourcedirectory% %targetdirectory% /f /i /s /h /d /y /l') do (call :subroutine "%%A")

rem log timestam creation and logging
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimecopy=%%I
set datetimecopy=%datetimecopy:~0,8% %datetimecopy:~8,2%:%datetimecopy:~10,2%:%datetimecopy:~12,2%
echo -----     BACKUP FINISHED AT: %datetimecopy%     -----
echo -----     BACKUP FINISHED AT: %datetimecopy%     ----- >> %logfile% 2>&1

goto :finish

:subroutine
 set "f=%1"
 set "f=%f:~1,-1%"
 set "filefrom=%f: -> =" & set "fileto=%"
 set "filefrom=^"%filefrom%^""
 set "fileto=^"%fileto%^""
 if NOT EXIST %fileto% GOTO :cpyonly
 set "g=%fileto%"
 call set "newg=%%g:\=\\%%"
 for /f "tokens=2 delims==" %%N in ('wmic datafile where name^=%%newg%% get LastModified /format:list') do set lastmod=%%N
 set "i=%fileto%"
 set "target="%%~ni%%~xi.B%lastmod:~0,14%^""
 echo rename %g% %target%
 echo "rename %g% %target%" >> %logfile% 2>&1
 rename %g% %target% >> %logfile% 2>&1
 goto :cpyonly

:cpyonly
 echo "xcopy %filefrom% %fileto% /f /h /d /y"  >> %logfile% 2>&1
 echo xcopy %filefrom% %fileto% /f /h /d /y
 xcopy %filefrom% %fileto% /f /h /d /y >> %logfile% 2>&1
 GOTO :finish

rem :testing
rem wmic datafile where name="d:\\CopyFrom\\drziaky.txt" get LastModified /format:list
rem d:\Ice\Private\StartBatch\Backup_new.bat

:finish

然而,它沒有起作用,無法前進,觀察到的問題:

  1. 文件名包含空格,即使我使用了"tokens=*" ,它也會在文件中創建拆分我還沒有找到如何將整個%%A傳遞給子程序的方法
  2. 通過文件操作,我有時需要刪除或添加“”,這適用於某些位置,而某些位置不行,我再也沒有發現何時/為什么(例如這個set "f=%f:~1,-1%"旨在刪除”和此set "fileto=^"%fileto%^""旨在添加它們

到以前的版本(不是在這篇文章中,但將它們放在我的硬盤上):

  1. 我嘗試使用副本,但副本沒有創建目標目錄
  2. 由於局部差異而使用wmic命令並且似乎有效(用於獲取時間和最后修改日期)
  3. xcopy作為列表​​生成器工作(最后一行列出文件數量的小問題)
  4. 解析先嘗試通過for /f然后通過set "filefrom=%f: -> =" & set "fileto=%" (從這里復制)但沒有工作

下面是最后一個工作代碼(帶副本)但是:

  • 無法創建目標目錄
  • 打破文件名中的空格
  • 在xcopy的最后一行中斷(復制了X個文件)

也許它在其他一些情況下也有所突破,但這是我迄今為止所能達到的最好的情況,但是如果陷入困境並無法從這一點向前推進,有人可以幫助我嗎? 謝謝你,並為長篇大論道歉。 如果它已經在這里,那么我真的很盲目,因為你花了很多時間搜索。

PS: enabledelayedexpansion不能用:-(也不能使用第三方使腳本只能是1個文件

set sourcedirectory="d:\Ice"
set targetdirectory="W:\Ice"
set logdirectory="W:"
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimefile=%%I
set datetimecopy=%datetimefile:~0,8% %datetimefile:~8,2%:%datetimefile:~10,2%:%datetimefile:~12,2%
set datetimefile=%datetimefile:~0,8%_%datetimefile:~8,6%
set "logfile=%logdirectory:~0,-1%\Backup%datetimefile%.log^""
echo -----     BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy%     ----- >> %logfile% 2>&1

for /f "tokens=1,3" %%A in ('xcopy %sourcedirectory% %targetdirectory% /f /i /s /h /d /y /l') do (call :subroutine "%%A" "%%B")

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimecopy=%%I
set datetimecopy=%datetimecopy:~0,8% %datetimecopy:~8,2%:%datetimecopy:~10,2%:%datetimecopy:~12,2%
echo -----     BACKUP FINISHED AT: %datetimecopy%     ----- >> %logfile% 2>&1

goto :eof

:subroutine
 set "f=%1"
 if NOT EXIST %2 GOTO :cpyonly
 set "g=%2"
 call set "newg=%%g:\=\\%%"
 for /f "tokens=2 delims==" %%N in ('wmic datafile where name^=%%newg%% get LastModified /format:list') do set lastmod=%%N
 set "target="%~n2%~x2.B%lastmod:~0,14%^""
 echo "rename %g% %target%" >> %logfile% 2>&1
 rename %g% %target% >> %logfile% 2>&1
 goto :cpyonly

:cpyonly
 echo "copy %1 %2" >> %logfile% 2>&1
 copy %1 %2 >> %logfile% 2>&1
 GOTO :eof

:eof

所以最后幾個小時后我找到了解決方案,現在它按照我的預期工作,還添加了ziping的舊文件。 如果有人想要使用感覺自由。 它是如何工作的,它檢查所有文件,並將目標上不存在的文件復制到目標,如果文件存在於目標上,則將其重命名為上次修改日期和ziped(這里需要設置自己的拉鏈和參數),然后復制新文件,最后再次執行xcopy作為安全措施,以防我的腳本對某些文件失敗。

@echo off

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimefile=%%I
set datetimecopy=%datetimefile:~0,8% %datetimefile:~8,2%:%datetimefile:~10,2%:%datetimefile:~12,2%
set datetimefile=%datetimefile:~0,8%_%datetimefile:~8,6%
set "logfile=%logdirectory:~0,-1%\Bcopy%datetimefile%.log^""
set "logxcpy=%logdirectory:~0,-1%\Xcopy%datetimefile%.log^""

set sourcedirectory="d:\myDir"
set targetdirectory="w:\myDir"
set logdirectory="W:"
set whereiszip="C:\Program Files\7-Zip\7z.exe"

echo -----   BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy% LOG: %logfile% ZIP: %whereiszip% START   -----
echo -----   BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy% LOG: %logfile% ZIP: %whereiszip% START   ----- >> %logfile% 2>&1

for /f "tokens=*" %%A in ('xcopy %sourcedirectory% %targetdirectory% /f /i /s /h /d /y /l') do (call :subroutine "%%A")
xcopy %sourcedirectory% %targetdirectory% /f /i /s /h /d /y >> %logxcpy% 2>&1

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimefile=%%I
set datetimecopy=%datetimefile:~0,8% %datetimefile:~8,2%:%datetimefile:~10,2%:%datetimefile:~12,2%
echo -----   BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy% LOG: %logfile% ZIP: %whereiszip% FINISH   -----
echo -----   BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy% LOG: %logfile% ZIP: %whereiszip% FINISH   ----- >> %logfile% 2>&1

if "%1"=="" goto :finish

if "%1"=="S" shutdown /s
if "%1"=="M" goto :outlook
if "%1"=="O" goto :outlook


:subroutine
if "%~1"=="" goto :cannotparse
set str1=%1
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimefile=%%I
set datetimecopy=%datetimefile:~0,8% %datetimefile:~8,2%:%datetimefile:~10,2%:%datetimefile:~12,2%
if not defined str1 goto :cannotparse
if x%str1: -> =%==x%str1% goto :cannotparse
echo string %str1% contains -^>
set str=%1
@setlocal enableextensions enabledelayedexpansion
set "find= -> "
call set last=%%str:*!find!=%%
call set first=%%str:!last!=%%
call set first=%%first:!find!=%%
call set first=!first!^"
call set last=^"!last!
set nqlast=!last:~1,-1!
@setlocal enableextensions disabledelayedexpansion
for %%A in ("%nqlast%") do (
    call set folder=%%~dpA
    call set file=%%~nxA
)
echo %datetimecopy% INPUT: [%str%] source file: [%first%] destination file: [%last%] path: [%folder%] name: [%file%]
echo %datetimecopy% INPUT: [%str%] source file: [%first%] destination file: [%last%] path: [%folder%] name: [%file%] >> %logfile% 2>&1
if NOT EXIST %last% GOTO :cpyonly
set "g=%last%"
call set "newg=%%g:\=\\%%"
for /f "tokens=2 delims==" %%N in ('wmic datafile where name^=%%newg%% get LastModified /format:list') do set lastmod=%%N
set "target="%file%.B%lastmod:~0,14%^""
echo %datetimecopy% RENAME rename %last% %target%
echo %datetimecopy% RENAME rename %last% %target% >> %logfile% 2>&1
rename %last% %target% >> %logfile% 2>&1
set "target="%folder%%file%.B%lastmod:~0,14%^""
set "ztarget="%folder%%file%.B%lastmod:~0,14%.zip^""
if EXIST %whereiszip% (
    echo %datetimecopy% 7ZIP: %whereiszip% a -tzip -mx9 %ztarget% %target%
    echo %datetimecopy% 7ZIP: %whereiszip% a -tzip -mx9 %ztarget% %target% >> %logfile% 2>&1
    %whereiszip% a -tzip -mx9 %ztarget% %target% >> %logfile% 2>&1
    if %ERRORLEVEL% EQU 0 (
        echo %datetimecopy% DELETE: del %target%
        echo %datetimecopy% DELETE: del %target% >> %logfile% 2>&1
        del %target% >> %logfile% 2>&1
    )
)
goto :cpyonly
endlocal
goto :finish

:cpyonly
set "quotedfolder="%folder%^""
echo %datetimecopy% XCOPY: xcopy %first% %quotedfolder% /f /i /s /h /d /y
echo %datetimecopy% XCOPY: xcopy %first% %quotedfolder% /f /i /s /h /d /y >> %logfile% 2>&1
xcopy %first% %quotedfolder% /f /i /s /h /d /y >> %logfile% 2>&1
goto :finish

:cannotparse
echo %datetimecopy% ERROR: string %str1% did NOT contains -^>
echo %datetimecopy% ERROR: string %str1% did NOT contains -^> >> %logfile% 2>&1
goto :finish

:outlook
c:
cd "\Program Files\Microsoft Office\Office12\"
start OUTLOOK.EXE /recycle
goto :finish

:finish
endlocal
endlocal

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM