0

I have a .bat file that I have scheduled to run daily at 1am using task scheduler on a windows server 2008 r2. The final output file gets overwritten every time so I need to add a date after the output file name.

Here is the code in .bat file :

SQLCMD -S ServerName -d DbName-E -i C:\loc\script.sql -o C:\loc\scriptOutputtemp.txt -s"|" -W
findstr /B /V /C:"----" "C:\loc\scriptOutputtemp.txt" > "C:\loc\finaloutput.txt"
del "C:\loc\scriptOutputtemp.txt"

I tried to add %date% like "C:\loc\finaloutput%date%.txt" but then nothing gets generated as output. It's my first time creating this so any guidance will be helpful.

Thank you!

FrankPl
  • 573
  • 4
  • 19
sky_limit
  • 133
  • 2
  • 11
  • 1
    Possible duplicate of [How do I get current datetime on the Windows command line, in a suitable format for using in a filename?](https://stackoverflow.com/questions/203090/how-do-i-get-current-datetime-on-the-windows-command-line-in-a-suitable-format) – Squashman Jun 29 '18 at 18:38
  • Solution in this thread worked! :) Someone please mark this as an answer. Thank you! https://stackoverflow.com/questions/9074407/how-do-i-name-the-output-textfile-to-yyyymmdd-based-on-the-system-date – sky_limit Jun 29 '18 at 19:02
  • 1
    Delete your question then as you can see it is a duplicate. – Squashman Jun 29 '18 at 19:05
  • I don't think it's a duplicate. This thread will help other users since it has additional information where the code is deleting "----". – sky_limit Jun 29 '18 at 19:15
  • You want me to show you all the questions that address the issue with the SQLCMD outputing that dashed line underneath the header. – Squashman Jun 29 '18 at 19:28
  • Show me the exact similar logic on any other thread please then I will delete mine. – sky_limit Jun 29 '18 at 19:30
  • Here is the final solution that I was really looking for that I couldn't exactly find anywhere!!!! "C:\loc\finaloutput%DATE:~-4%%DATE:~4,2%%DATE:~7,2%.txt" – sky_limit Jun 29 '18 at 19:32
  • 1
    Perhaps [this question](https://stackoverflow.com/q/22103637), addresses the dashed line issue! – Compo Jun 30 '18 at 12:07

0 Answers0