i do something exactly like this in PowerShell; with that, i can capture the output of the script, whether it was an info message, a data set or an error message.
I think that is a much better practice, with more control.
$DirectoryToProcess = "F:\StandardTakeOver\"
foreach ($CurrentFileName in Get-ChildItem -path $DirectoryToProcess -Filter SQL_*.sql | sort-object )
#add an Output sub directlry,and then the file name
$out = [System.IO.Path]::Combine([System.IO.Path]::Combine($DirectoryToProcess,"Output") , $CurrentFileName.name.split(".") + ".txt") ;
#invoke-sqlcmd -InputFile $f.fullname | format-table | out-file -filePath $out
# -S server\instancename
# -E = trusted connection
# -i input file
# -I Enable Quoted Identifier: critical fro stored proc creations
$CurrentFileName.name #debug/echo out the current filename so I can see what is being processed
sqlcmd -S "(local)" -E -I -i $CurrentFileName.fullname | format-table | out-file -filePath $out
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!