4

How do I name the output textfile to YYYYMMDD based on the system date?

sqlcmd -S DataBBB -i c:\scripts\followup.sql
    -o %DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%.txt -s ; -W -u

Now the output text file is 01_31_2012.txt. How can I change it to 2012_01_31.txt?

sarnold
  • 102,305
  • 22
  • 181
  • 238
waterbottle
  • 85
  • 2
  • 11

1 Answers1

3

Tried it under Windows 7 Premium (German), may be dependent upon OS and Local Time Format.

sqlcmd -S DataBBB -i c:\scripts\followup.sql 
                            -o %date:~-4%_%date:~3,2%_%date:~0,2%.txt -s; 

You have to edit this part for your system

%date:~-4%_%date:~3,2%_%date:~0,2%.txt

The statement used the command line internal %date% - variable with the extension :~start,length. So you can create the filename with different parts from the date- variable.

sarnold
  • 102,305
  • 22
  • 181
  • 238
Andreas Rohde
  • 613
  • 5
  • 15
  • Completely unrelated: "addicted" is really for drugs and similar: _I am addicted to Stack Overflow._ – sarnold Jan 31 '12 at 23:09
  • I came across similar issue so thought will copy the solution on this thread as well: -o %DATE:~-4%%DATE:~4,2%%DATE:~7,2%.txt – sky_limit Jun 29 '18 at 20:07