tags:

views:

28

answers:

1

Hi,

How can I customize the the FOR command below to loop through the files inside the Database folder following the order of the number prefixed in the file name?

FOR /R ../Database %%f IN (*.sql) DO sqlcmd -S %1 -d %2 -U %4 -P %5 -i "%%~f" >> Logs/%2_DBInstall.log || goto errors

Database folder contains: 001_usp_procedure1.sql 002_ups_procedure2.sql

Thanks very much,

A: 

Please try the following

del temp.txt
del temp1.txt
for  /F "usebackq "  %%F IN (`dir /b /s /o:n "../Database/*.sql"`)  DO (
echo %%~nF  / %%F >> temp.txt
)
sort temp.text temp1.text
for /F "tokens=1,2 delims=/" %%i IN (temp1.txt) DO sqlcmd -S %1 -d %2 -U %4 -P %5 -i "%%j" >> Logs/%2_DBInstall.log || goto errors

It should work for recursive directories too (process is sorted by global filename without taking account of the path ... did I understand the problem?) Please note that two temp files are being used. Be careful with the names (or use real temp files)

belisarius
Thanks million belisarius. That's exactly what i needed.
Marcelo