Replace old file with new file

后端 未结 3 1285
野性不改
野性不改 2021-01-29 12:19

I am trying to write a script to replace old files content with new files content which is appearing in the following format:

Old file : something.txt
New file : s

3条回答
  •  遇见更好的自我
    2021-01-29 12:54

    @echo off
    setlocal
    set FOLDER_PATH=D:\test
    for %%f in (%FOLDER_PATH%\*.new) do if "%%~ff" neq "%~f0" (
        ECHO move /Y "%%~ff" "%%~dpnf"
    )
    PAUSE
    

    or

    @echo off
    setlocal
    set FOLDER_PATH=D:\test
    for %%f in (%FOLDER_PATH%\*.new) do if "%%~ff" neq "%~f0" (
        if exist "%%~dpnf" ECHO del "%%~dpnf"
        ECHO rename "%%~ff" "%%~nf"
    )
    PAUSE
    

    Note that operational move (del and rename) commands are merely ECHOed for debugging purposes.

    Also note that if "%%~ff" neq "%~f0" could be omitted iterating *.new files as %~f0 has an extension of .bat or .cmd (try echo %~x0 %~f0).

    Resources (required reading):

    • (command reference) An A-Z Index of the Windows CMD command line
    • (additional particularities) Windows CMD Shell Command Line Syntax
    • (%~ff etc. special page) Command Line arguments (Parameters)
    • (>, 2>&1 etc. special page) Redirection

提交回复
热议问题