Search code examples
sqlwindowsbatch-filecommand-linesqlcmd

Command Line, File Name for each script executed. SQL


I have the following files in my folder:

Script1.sql
Script2.sql
Script3.sql

The below command executes each script successfully:

@echo off

set /p sname= Please enter the servername:
set /p dbname= Please enter the databasename:

ECHO started the batch process at %TIME%  >output.txt

for %%f in (*.sql) do (
sqlcmd.exe  -S %sname% -d %dbname% -i %%f  >>output.txt

%~f0"

    )
pause

Results are

(1 rows affected)

(3 rows affected)

(2 rows affected)

What I would like to know is how can i input the file name after each file is executed and this name stored in the output.txt file ie

(1 rows affected)

Script1.sql

(3 rows affected)

Script2.sql

(2 rows affected)

Script3.sql

Any help appreciated


Solution

  • Just output the file name, what's problem?

    for %%f in (*.sql) do (
       echo %%~nf >> output.txt
       sqlcmd.exe  -S %sname% -d %dbname% -i %%f  >>output.txt
    )