I was looking for some help in setting up a batch script to go through an entire directory and copy all .sql files to a single folder. I can do this easy enough with a FOR
loop but I hit a snag if two or more files in different directories have the same name. I'm attempting to get around this using a counter but I'm having trouble getting it to be specific to individual file names.
My code so far:
set /A n=0
for /R %source% %%G in (*.sql) do (
if exist %destination%\sql_files\%%~nxG (
set /A n+=1
set "full=%%G"
set "name=%%~nG"
set "ext=%%~xG"
setlocal enabledelayedexpansion
copy "!full!" "!destination!\sql_files\!name!(!n!)!ext!" >nul
endlocal
) else (
copy "%%G" "%destination%\sql_files\" >nul
)
)
For example if I have:
%source%\dir1\file1.sql %source%\dir1\file2.sql %source%\dir2\file1.sql %source%\dir2\file2.sql %source%\dir3\file1.sql
I end up with:
file1.sql file1(1).sql file1(3).sql file2.sql file2(2).sql
What I would like to see is:
file1.sql file1(1).sql file1(2).sql file2.sql file2(1).sql
I thought that my answer might lie in a subroutine but I can't quite figure out the logic to make that work. Any help is greatly appreciated.