Link to home
Start Free TrialLog in
Avatar of mmalik15
mmalik15

asked on

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

the command below 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. How can we amend this command to get the time as 093841 rather than 93841

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

Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

I assume this is the same q I saw the other day.  I think then it was recommended to look at my article here:

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

From the comment on there is one that gives you yyyy-mm-dd-hh-mm which is close to what you want:

@echo off
echo wscript.echo year(date) ^& "-" ^& right(100+month(date),2) ^& "-" ^& right(100+day(date),2)^& "@" ^& right(100+hour(time),2) ^& "-" ^& right(100+minute(time),2) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmddhhmm=%%a
echo Now you can use this in your filename: %yyyymmddhhmm%

pause

@echo off
echo wscript.echo year(date) ^& right(100+month(date),2) ^& right(100+day(date),2)^& "_" ^& right(100+hour(time),2) ^& right(100+minute(time),2) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set stamp=%%a
set File=sqlimporter2_sharedspace_%Stamp%.txt

I htink that is what you have there... if not ask:

yyyymmdd_hhmm

Steve
Avatar of mmalik15
mmalik15

ASKER

thanks for the comment steve

i have made slight amendment in your suggestion but this does not seem to work

echo wscript.echo year(date) ^& "-" ^& right(100+month(date),2) ^& "-" ^& right(100+day(date),2)^& "@" ^& right(100+hour

(time),2) ^& "-" ^& right(100+minute(time),2) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set ddmmyyyyhhmm=%%a
echo Now you can use this in your filename: %ddmmyyyyhhmm%

this is how i want may date ddmmyyyy hhmm
And if you want seconds on the end too, try this:

@echo off
echo wscript.echo year(date) ^& right(100+month(date),2) ^& right(100+day(date),2)^& "_" ^& right(100+hour(time),2) ^& right(100+minute(time),2) ^& right(100+second(time),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set stamp=%%a
set File=sqlimporter2_sharedspace_%Stamp%.txt

Again details of how to build that slightly unwieldly looking wscript line for different details is all in here:

https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Toggle HighlightingOpen in New WindowSelect All
~bp
thanks
Thanks... sorry for adding link to article in each post btw, but we get points for article when is in selected "answer" ... but not if next comment we make say :-(

Hope it helps

Steve
The time-part should be something like this:

   set t=0%TIME:~-11,2%%TIME:~-8,2%%TIME:~-5,2%

Notice the leading '0' and the negative values.
Oops! not sure why I posted here! Please ignore!