Recursively find and replace files

前端 未结 1 2042
名媛妹妹
名媛妹妹 2021-02-11 07:32

What I want to do is following. I want to create some bat file, that will recursively search for files starting from current directory and replace with the file that I provided

相关标签:
1条回答
  • 2021-02-11 08:17

    The Batch file below start from current directory, recursively search the file given in the first parameter and copy over it (with same name) the file given in second parameter:

    @echo off
    set targetName=%~NX1
    set replacementFile=%~F2
    call :processFolder
    goto :EOF
    
    :processFolder
    rem For each folder in this level
    for /D %%a in (*) do (
       rem Enter into it, process it and go back to original
       cd %%a
       if exist "%targetName%" (
          copy "%replacementFile%" "%targetName%" /Y
       )
       call :processFolder
       cd ..
    )
    exit /B
    

    For example:

    app test1.txt c:\data\replacementfile.txt
    
    0 讨论(0)
提交回复
热议问题