动态定位所有文件并替换为特定文件脚本

时间:2014-08-16 23:47:05

标签: file batch-file cmd locate

我一直试图让语法正确,但我遇到了问题。我不仅没有正确的语法,而且我希望这个脚本:

  1. 在网络中的所有驱动器上找到特定条件的所有文件
  2. 检查文件是否为更新文件(或新文件)
  3. 如果不是新文件或更新文件,请找到新文件并将其替换
  4. 也!如果我可以按照计划工作,例如每6小时......这将是一个真正的帮助

    我让这段代码工作了一次,但我改了几次并保存了它。

    @echo off
    SETLOCAL
    cls
    
    :locate_old
    for /d /r Z:\ %%a in (*) do if exist "%%~fa\old.file" set "oldFile=%%~fa\old.file"
    if not defined oldFile (
    echo old file not found...
    ) else (
    echo old file found&GOTO oldFileCheck
    )
    
    :oldFileCheck
    find "old file text" "%oldFile%" && echo old file is already updated || GOTO findNewFile
    
    :findNewFile
    for /d /r Z:\ %%a in (*) do if exist "%%~fa\new.file" set "newFile=%%~fa\new.file"
    if not defined newFile (
    echo no new file detected...
    ) else (
    echo new file located...&GOTO fileSwap
    )
    
    :fileSwap
    copy /y "%newFile%" "%oldFile%" && echo file updated || file not updated
    

1 个答案:

答案 0 :(得分:0)

如果我了解您的要求,您希望将Z:\上的所有“old.file”文件替换为“new.file”文件(如果它们尚未更新)。这是未经测试的:

@if not defined debug_batch_files echo off
REM You can set debug_batch_files to 1 and quickly see verbose execution
pushd Z:\
for /f "delims=" %%a in ('dir /b /s /a-d new.file') do echo xcopy /D /Y "%%~fa" "%%~dpaold.file"
REM Remove "echo" from the above line if it displays the right paths.
popd