• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

how to declare a variable and assign it currentdate time in DOS

I have batch files like this

\pushsqlimporter\sqlimporter2 87.246.105.166:16100 gpcpd_live.xml \logs\sqlimporter_gpcpd_live5.txt

on the above command this is the name of the log file "sqlimporter_gpcpd_live5.txt" I want to generate the name of this log file at runtime  e.g.  it could be current system datetime like this (sqlimporter_gpcpd_11-10-2011 17:15)how can we declare a variable and assign it current datetime and use in that command
0
mmalik15
Asked:
mmalik15
  • 9
  • 5
  • 4
  • +1
2 Solutions
 
Bill PrewCommented:
Great article by Steve (dragon-it) here, let us know if you have further questions.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

~bp
0
 
Steve KnightIT ConsultancyCommented:
damn he got in first.... Yes that is my suggested way, though lots of people get by using %date% and %time% variables and pulling bits of tem out but as it causes issues if run  with different date formats such as with different users.

As bill says have a read and ask if unsure.

Steve
0
 
Bill PrewCommented:
Steve,

I thought you had added a reference to DOFF in there, or did I imagine that?

~bp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Steve KnightIT ConsultancyCommented:
i thought there was a commentabout it... Dont think i can edit it now.  if there isnt feel free to add one!
0
 
Bill PrewCommented:
I did too, I know I was looking at something where it was added, but I don't see it there, odd.

~bp
0
 
Bill PrewCommented:
@mmalik15

If you jut want to do something simple you could use %DATE% and %TIME% in the naming of the file.  These are built in system variables in a BAT or CMD file that will have the date and time in them.  The issue can be that the formatting of the date and time in these variables varies depending on your locale.  So it's hard to take them apart in a script without knowing the format on your system.

If you want to do:

ECHO %DATE%
ECHO %TIME%

at a command prompt, and post it here we can help further.  Also, what format do you want the date/time stamp on the file, typically things like this make it easier to sort:

filename_YYYYMMDD_hhmmss.txt

~bp
0
 
Bill PrewCommented:
I suspect your date format is dd/mm/yyyy, in which case this would work:

set Stamp=%DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
set File=sqlimporter_gpcpd_%Stamp%.txt
echo %File%

Open in new window

~bp
0
 
ReneGeCommented:
I prefer using WMIC cause it is independent to your regional settings.

Cheers
@ECHO OFF

CALL :GetDateTime

SET FileName=sqlimporter_gpcpd_%Month%-%Day%-%Year% %Hour%:%Min%
ECHO %FileName%
PAUSE
EXIT


:GetDateTime
REM READ DATE
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
	IF %%A GTR 0 (
	SET DayVal=%%A
	SET HourVal=%%B
	SET MinVal=%%C
	SET MonthVal=%%D
	SET SecVal=%%E
	SET YearVal=%%F
	)
)

REM ADDING LEADING "0"
	IF %DayVal% LSS 10   (SET Day=0%DayVal%) 		ELSE (SET Day=%DayVal%)
	IF %HourVal% LSS 10  (SET Hour=0%HourVal%) 		ELSE (SET Hour=%HourVal%)
	IF %MinVal% LSS 10   (SET Min=0%MinVal%) 		ELSE (SET Min=%MinVal%)
	IF %MonthVal% LSS 10 (SET Month=0%MonthVal%) 	ELSE (SET Month=%MonthVal%)
	IF %SecVal% LSS 10   (SET Sec=0%SecVal%) 		ELSE (SET Sec=%SecVal%)
	SET Year=%YearVal%

	SET TheDate=%Year%-%Month%-%Day%
	SET TheTime=%Hour%.%Min%.%Sec%


EXIT /b

Open in new window

0
 
mmalik15Author Commented:
Thanks for all the comments

I am trying this  bit

set Stamp=%DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
set File=sqlimporter_gpcpd_%Stamp%.txt
echo %File%

and the format of the date is as below

sqlimporter2_people_20111012_105730.txt

can we have it in the format
sqlimporter2_people_12102011_10:57:30.txt
0
 
Steve KnightIT ConsultancyCommented:
you cant have colons in the name sorry.  you just need to reDorder the bits you want, i.e. The bit saying %date:~-4% is the yyyy part.

Would strongly recommend you don't do that though if there is any chance this will be run or scheduled as a different user, or on different machine in case date formats are different.  see my article mentioned by bill in first post

steve
0
 
mmalik15Author Commented:
thanks dragon-it

I m new to DOS commands and variables do you know any good free editor where i can test these commands
0
 
mmalik15Author Commented:
I managed to change the date format by using the command below

set Stamp=%DATE:~-10,2%%DATE:~-7,2%%DATE:~-4%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
set File=sqlimporter_gpcpd_%Stamp%.txt
echo %File%

if we can't have colons between time, can we possibly have only hh mm part with a space between them
0
 
Bill PrewCommented:
==> if we can't have colons between time, can we possibly have only hh mm part with a space
==> between them


You can add anything you want to the "text" below, what you are asking for would look like:

set Stamp=%DATE:~-10,2%%DATE:~-7,2%%DATE:~-4%_%TIME:~0,2% %TIME:~3,2%
set File=sqlimporter_gpcpd_%Stamp%.txt
echo %File%

Open in new window

The %DATE% and %TIME% are built in system variables holding the date and time as strings.  When referencing variables you can do "substring operations" using the format %VAR:~x,y% where x is the position to start extracting characters (zero based) and y is the number of characters to extract.  Do SET /? at a command prompt and page down a bit to see details on that syntax.

~bp
0
 
Steve KnightIT ConsultancyCommented:
yes you can... Or - or _ etc.  you cant use : / or \ because they are part of file names and directories if you think about it.  other chars are not allowed or bad ideas too in general depending upon if you need to be able to process them in the future. : ><|^~ ()%!?

Would really suggest yyyymmdd is much easier than ddmmyyyy for ease of sorting as has been said as sort them in name order and also is date order then.

Yyyymmdd.cmd in one of the last comments added to my article shows you the one line, reliable way....

As to testing you are easiest testing in a temorary batch file or from cmd.esxe prompt you can do, for instance,

echo %date:~-4% .... Which says take the last R chars from date variable.   see set/? And for /? And others for more info.

%date:~-7,2C means go 7 chars from end and take 2 chars
%date:~3,2% means after 3 chars take next 2

so as you can see relies heavily on if your date format is

ddd dd/mm/yyyy
dd-mm-yyyy
mm-dd-yyyy
etc.

Steve
0
 
mmalik15Author Commented:
Excellent!
0
 
ReneGeCommented:
If you are more looking for a one liner, try this...

@ECHO OFF
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Year /Format:table') DO IF %%A GTR 0 SET FileName=sqlimporter_gpcpd_%%D-%%A-%%E %%B:%%C
ECHO The file name is:%FileName%
PAUSE

Open in new window

0
 
Bill PrewCommented:
Glad that helped, thanks.

~bp
0
 
mmalik15Author Commented:
the command works okay but except when the hour part of the time is less than two digits e.g. when the time is 93841(9:38:41 , the hour part is one digit), in this case the file name is appended with a space which is not allowed in the file name so I get an error

set Stamp=%DATE:~-10,2%%DATE:~-7,2%%DATE:~-4%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
set File=sqlimporter2_sharedspace_%Stamp%.txt

0
 
Bill PrewCommented:
There are a number of ways to work around that, here is one approach.

Make sure you have this line near the top of your BAT file, I tend to put it right after the ECHO OFF:

setlocal EnableDelayedExpansion

Open in new window

then, use this to replace the leading space with a zero before extracting the pieces of the time:

set zTime=%TIME: =0%
set Stamp=%DATE:~-10,2%%DATE:~-7,2%%DATE:~-4%_!zTime:~0,2!!zTime:~3,2!!zTime:~6,2!
set File=sqlimporter2_sharedspace_%Stamp%.txt

Open in new window

~bp
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 9
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now