WayneS (6/13/2009)
Okay, here's some code to demonstrate what I'm talking about.Save this code as a batch file, and run it:
for %%a in ("%windir%\*.exe") do SQLCMD -S ServerName -d master -E -Q"set nocount on;select '%%a'" -o "%temp%\output.txt"
type %temp%\output.txt
pause
cls
echo %date% %time% > "%temp%\output.txt
for %%a in ("%windir%\*.exe") do SQLCMD -S ServerName -d master -E -Q"set nocount on;select '%%a'" >> "%temp%\output.txt"
type %temp%\output.txt
pause
del %temp%\output.txt
At the first pause, the contents of the output file are displayed. All that is in it is the last *.exe.
At the second pause, the contents of the output file are again displayed. The prior contents are erased, and all exe files are listed.
This demonstrates the point the OP was making.
You've added a line of code the OP didn't have in the original problem and that new line is the one responsible for the incorrect operation.
echo %date% %time% > "%temp%\output.txt
You've neglected to use ">>" in that line of code and that's what resets file. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.