0

I found code here:Windows batch file to copy and keep duplicates and it's successfully copying all .csv files from their source folders to a separate folder created at the beginning of the code. What I would like to do is not only copy the files but also rename them after the folder from where they were copied. So:

Folder 1 
   xxx.csv
Folder 2
   yyy.csv
Folder 3
   zzz.csv

Should result in:

Folder called CSV_Files

Folder 1.csv
Folder 2.csv
Folder 3.csv

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set TESTFOLDER=CSV_Files
md "%TESTFOLDER%"

set /a counter=0
FOR /F "tokens=*" %%i IN ('DIR /S /B /A-D .\*.csv') DO FOR /F "tokens=*" %%j IN ('DIR /B "%%i"') DO IF EXIST ".\%TESTFOLDER%\%%j" (
        set /a counter=!counter!+1
        echo folder: %TESTFOLDER%
        copy "%%i" ".\%TESTFOLDER%\%%j_!counter!"
    ) ELSE copy "%%i" ".\%TESTFOLDER%\%%j"
:eof

I tried variations of:

copy "%%i" ".\%TESTFOLDER%\%%~nxi"

to no avail. Keep getting bad syntax error. Any ideas?

Compo
  • 36,585
  • 5
  • 27
  • 39
Anubix
  • 17
  • 4
  • I found a previously answered question that does what I need with minimum revisions. https://stackoverflow.com/questions/38650536/batch-file-to-copy-and-rename-files-from-multiple-directories – Anubix Jun 03 '23 at 18:50

0 Answers0