批处理文件摆脱“在目标上指定文件名或目录名”消息

时间:2012-11-29 21:27:55

标签: for-loop batch-file subroutine xcopy

我的批处理文件存在问题,每次查看指定文件夹时都会有10秒的延迟,然后调用命名约定的子例程:...batch that copies modified files in directory to another directory

我根据Ken White修改了代码非常有帮助,但是我还有另一个与此批次相关的问题,即当批处理运行时我执行测试文件更新并保存在源代码中目录,批处理在cmd控制台中询问我“在目标上指定文件名或目录名”,以查看它所查看的每个文件。因为我希望它在后台自动运行,所以我不想让用户输入它才能正常运行。任何帮助或建议都会非常有用。

以下是我目前的情况:

@Echo Off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: variables
set drive=C:\Users\me\Desktop\Test Source Folder\
set backupcmd=xcopy /m /s /c /d /e /h /i /r /y
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

:_RevLoop
set basename=
for %%a in (*.*) 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 Destination Folder\!baseName!!baseExt!"
         set baseName=%%~Na
         set baseExt=%%~Xa
         set lastName=%%~Na
      )
   )      
)
rem Copy last set
%backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Destination Folder\!    baseName!!baseExt!"


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\me\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...


GOTO :_RevLoop


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)

假设您在复制文件时不需要重命名文件,请尝试

%backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Destination Folder\"

注意:我发现尾随\无需/i切换。

修改:新策略。首先使用上面的命令复制基本文件,然后在复制最终文件时,它将覆盖它而不提示。

for %%a in (*.*) do (
    if not defined baseName (
        rem Is first name of first set
        set baseName=%%~Na
        set baseExt=%%~Xa
        set lastname=%%~Na
        %backupcmd% "%%a" "C:\Users\me\Desktop\Test Destination Folder\"
    ) 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 Destination Folder\!baseName!!baseExt!"
            set baseName=%%~Na
            set baseExt=%%~Xa
            set lastName=%%~Na
            %backupcmd% "%%a" "C:\Users\me\Desktop\Test Destination Folder\"
        )
    )
)