Search code examples
batch-filerenamexcopy

How to overwrite files with names starting from nl_ with sp_


I have folder A and B. Folder A has files like: a.mp3 and a.txt and folder B has: b.mp3 and b.txt. What I want to do here is copy and rename the content of the folder A to B so that the files can be overwritten.

Here is an example code on how to overwrite and keep the same file name in folder B:

XCOPY /HECY A\a.txt B\b.txt

ButI don't want to type all the file names to copy and overwrite the files in folder B.

Any help will be appreciated.


Solution

  • This should work. It will copy all A\nl_*.* files to B\, renaming the nl to sp and overwriting the files.

    setlocal enabledelayedexpansion
    for %%a in (A\nl_*.*) do (
    set file=%%~nxa
    set file=!file:~2!
    xcopy /hecyi "%%a" "B\sp!file!"
    )