I have the following batch code which executes all SQL files of a folder.
for /r %%G in (*.sql) do sqlcmd /S localhost /d superDB /U sa /P topsecret -i"%%G"
pause
But now I want to execute only the files which start with a two digit number in name like:
01_file.sql
02_file.sql
05_file.sql
43_file.sql
But not files like:
optional_01_file.sql
XYZ04_file.sql
Can someone tell me how to change the code to filter only files with a file name which starts with 2 digits?
Findstr's rudimentary RegEx capabilities are sufficient to match the files.
UnTested:
:: Q:\Test\2018\07\21\SO_51456661.cmd
@echo off
for /r %%G in (*.sql) do (
Echo=%%~nG|Findstr "^[0-9][0-9]_" 2>&1>Nul && (
echo sqlcmd /S localhost /d superDB /U sa /P topsecret -i"%%G"
)
)
In a folder with these files
> dir /B *.sql
01_file.sql
0815_file.sql
55_file.sql
abc0855_file.sql
I get these commands (only echoed for demonstration)
> SO_51456661.cmd
sqlcmd /S localhost /d superDB /U sa /P topsecret -i"Q:\Test\2018\07\21\01_file.sql"
sqlcmd /S localhost /d superDB /U sa /P topsecret -i"Q:\Test\2018\07\21\55_file.sql"