0

I Hae Create BAT File

@ECHO OFF
"C:\Program Files\Microsoft SQL Server\100\DTS\Binn\dtexec.exe" /FILE "D:\New folder (2)\xslttosql\SSIS-BSMS\SSIS-BSMS\SSIS-BSMS\Package6.dtsx" /REP P >> "D:\TRX_Value.log"
exit 

but i Want to make the Output which D:\TRX_Value.log path Changed Dynamically and give time information on the file name like D:\TRX_Value-20130307-144650.log which mean 2012/03/07 14:46:50

So is it possible? What kind of script should I write?

Sabilv
  • 602
  • 1
  • 15
  • 44

4 Answers4

3

Dealing with dates is somewhat tricky in BAT files.

A simple solution is to use WMIC LocalTime command which returns the current date and time in a convenient way to directly parse it with a FOR command. Try something similar to this:

@ECHO OFF
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
  SET /A DT=%%F*10000+%%D*100+%%A
  SET /A TM=%%B*100+%%C
  SET FDT=%DT%-%TM%
 )
ECHO "c:\progs\dtexec" /FILE "d:\fldr\pack.dtsx" /REP P "d:\logs\tr%FDT%.log"

check the result and correct the execution line to fit your reqs

PA.
  • 28,486
  • 9
  • 71
  • 95
  • +1 I love seeing more examples of the flexibility of `wmic`. It really is the Swiss Army Knife of Windows. – rojo Mar 07 '13 at 14:49
2

I like PA's suggestion to use WMIC, but there is a much simpler WMIC implementation.

The OS alias has the localDateTime property in almost the exact format you want:

YYMMDDhhmmss.ffffff-zzz

where ffffff is fractional seconds, and zzz is time zone information. Simple substring operations provide the desired date and time format.

@echo off
setlocal
set "ts="
for /f "skip=1" %%A in ('wmic os get localDateTime') do if not defined ts set "ts=%%A"
... your exe call ... >>"D:\TRX_Value-%ts:~0,8%-%ts:~8,6%.log"
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Neat! But if you format the output of `wmic` as a list you don't have to make sure `%ts%` is unset or check if not defined. `for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set "ts=%%I"` In any case, `wmic os get localdatetime` was an excellent find! – rojo Mar 07 '13 at 19:22
1

Just because I like a challenge, here's a batch script / JScript hybrid script that'll format your timestamp the way you want. Save this with a .bat extension.

@if (@X)==(@Y) @end /* (batch + jscript hybrid script init)

:: *** Batch script *****

@echo off
setlocal
for /f %%I in ('cscript /nologo /e:jscript "%~f0"') do set "ts=%%I"
"C:\Program Files\Microsoft SQL Server\100\DTS\Binn\dtexec.exe" /FILE "D:\New folder (2)\xslttosql\SSIS-BSMS\SSIS-BSMS\SSIS-BSMS\Package6.dtsx" /REP P >> "D:\TRX_Value-%ts%.log"
exit /b

:: *** JScript script *****/

var d = new Date();
WScript.echo(d.getFullYear() + /\d{2}$/.exec('0' + (d.getMonth() + 1)) + /\d{2}$/.exec('0' + d.getDate()) + '-'
+ /\d{2}$/.exec('0' + d.getHours()) + /\d{2}$/.exec('0' + d.getMinutes()) + /\d{2}$/.exec('0' + d.getSeconds()));

(dbenham is a master at this sort of stuff.)

rojo
  • 24,000
  • 5
  • 55
  • 101
1

Echo's the date and time into 2 temp files, then parses them into correct format, and uses them in your command.

@ECHO OFF
Set CURRDATE=%TEMP%\CURRDATE.TMP
ECHO %DATE% > %CURRDATE%
Set CURRTIME=%TEMP%\CURRTIME.TMP
ECHO %TIME% > %CURRTIME%

Set PARSEDATEARG="eol=; tokens=1,2,3,4* delims=/, "
Set PARSETIMEARG="eol=; tokens=1,2,3,4,5* delims=:,., "
For /F %PARSEDATEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%k%%j%%i
For /F %PARSETIMEARG% %%i in (%CURRTIME%) Do SET HHMMSS=%%i%%j%%k

"C:\Program Files\Microsoft SQL Server\100\DTS\Binn\dtexec.exe" /FILE "D:\New folder (2)\xslttosql\SSIS-BSMS\SSIS-BSMS\SSIS-BSMS\Package6.dtsx" /REP P >> "D:\TRX_Value-%YYYYMMDD%-%HHMMSS%.log" exit
RichardC
  • 794
  • 4
  • 13
  • 2
    This assumes the format of `%date%` is MM/DD/YYYY. Depending on the user's locale, this might not work. – rojo Mar 07 '13 at 14:45
  • @rojo It actually assumes DD/MM/YYYY (as I'm in the UK) but good point. Would need to swap the relevant line to `For /F %PARSEDATEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%k%%i%%j` (swapping the i and j variables in the output) I presume. – RichardC Mar 07 '13 at 14:47