Replace old file with new file

后端 未结 3 1260
野性不改
野性不改 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:35

    1. You missed a \in %FOLDER_PATH%\*.
    2. You want to rename *.new files only, so why not using this pattern rather than *? If you are interested in files matching the pattern *.txt.new, then specify this instead of *.
    3. There is no need for variable basename; simply use %%~nf to remove the (last) suffix (.new).
    4. The ren command throws an error in case the target file already exists; since you want it to be overwritten, use move /Y instead. Add > nul to hide the summary 1 file(s) moved..
    5. Remove the if query after having resolved item 2.

    All in all, your script can be reduced to:

    @echo off
    set "FOLDER_PATH=D:\test"
    rem // Ensure to specify the correct pattern, `*.new` or `*.txt.new`:
    for %%f in ("%FOLDER_PATH%\*.txt.new") do (
        > nul move /Y "%%~f" "%%~nf"
    )
    pause
    

提交回复
热议问题