0

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

任何帮助将非常感激。谢谢

@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
4

1 回答 1

1

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

将第一行更改为:

@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
于 2012-11-29T17:57:07.217 回答