批处理文件合并另一个批处理,将目录中的修改文件复制到另一个目录

时间:2012-11-29 17:49:33

标签: batch-file compare filenames last-modified save-as

以下是我提出的内容,但是我无法在我的代码中从Aacini在另一篇文章中提供的内容中获得正确的xcopy参考。 cmd控制台会说它找不到文件“!lastName !! baseExt!”然后显示已复制0个文件。这不是复制,因为我认为xcopy语法不允许在xcopy之后替换“source”“directory”关系。

非常感谢任何帮助。感谢

@Echo Off
:: variables
set drive=C:\Users\me\Desktop\Test Source Folder
set backupcmd=xcopy /m /s /c /d /e /h /i /r /y /exclude:AutoFileCopy_Rev1.bat

set basename=
for %%a in ("C:\Users\me\Desktop\Test Source Folder") do (
    if not defined baseName (
    rem Is first name of first set
    set baseName=%%~Na
    set baseExt=%%~Xa
    set lastname=%%~Na
    ) else (
    rem Check if this name begin with same baseName
    set name=%%~Na
    for %%b in (!baseName!) do set name=!name:*%%b=!
      if "!name!" neq "%%~Na" (
         rem Yes: Is next name of same set
         set lastName=%%~Na
      ) else (
         rem No: Is first name of next set: copy previous set and pass to next one
         %backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Source Folder\!    baseName!!baseExt!"
         set baseName=%%~Na
         set baseExt=%%~Xa
         set lastName=%%~Na
      )
   )      
)
rem Copy last set

Set _Delay=10
Set _Monitor=C:\Users\me\Desktop\Test Source Folder\
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop



echo ___ Backing up JobBoss files...
::%backupcmd% "C:\Users\john.weakley\Desktop\Test Source     Folder" "C:\Users\me\Desktop\Test Destination Folder\"

::CALL "C:\users\me\Desktop\Test Source Folder\Test.bat"

ECHO ___ Checking for new file revisions...
%backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Source Folder\!baseName!!    baseExt!"



Echo.Backup Complete!

Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF

1 个答案:

答案 0 :(得分:1)

您需要启用延迟扩展才能支持!VAR!语法。

将您的第一行更改为:

@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION