0

I want to copy all files in all subfolders and prefix them with a user defined input using a batch script. The code below works, but ends up copying the copied files, thus resulting in a large list of files like

Test - Lastname.txt

Test - Test - Lastname.txt

Test - Test - Test - Lastname.txt

etc.

How do I stop it from repeating the procedure on already copied files?

@Echo OFF
setlocal enabledelayedexpansion
set /p input=Enter File Prefix:
for /r %%a in (*.txt) do for %%b in ("%%~dpa\.") do copy "%%~a". "%cd%\%%~nxb\%input% %%~nxa"

i think an if Findstr %input% already exists in the filename, then skip to next file should work, similar to Batch Script Renaming File Prefix but no luck so far....

T.V.
  • 1
  • 1
    I'm not sure why you're using a nested loop, _is `"%%~dpa\."` not effectively the same as `"%cd%\%%~nxb"`?_ What is `"%%~a".` supposed to represent? Please also note, that `Set /P` offers no control over the end users input, they can effectively enter anything or nothing. You should therefore perform some validation to ensure that they've entered something and whatever that is does not contain invalid characters for filenames. – Compo Dec 05 '19 at 23:06
  • I've mishmashed code into the above. You're right, the ```%%~dpa\."``` should be the same ```"%cd%\%%~nb"``` per [link](https://stackoverflow.com/questions/3667026/meaning-of-dpa). As for ```"%%~a".```, it is supposed to be the variable created by the for loop, which is each of the filename(s) as the for loop cycles recursively through each subfolder. Again, i think my problem is that the for loop creates a new file and then includes that in the next for loop [link](https://stackoverflow.com/questions/31975093/at-which-point-does-for-or-for-r-enumerate-the-directory-tree/31976922#31976922). – T.V. Dec 06 '19 at 14:50

0 Answers0