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%_%TIM E:~0,2%%TI ME:~3,2%%T IME:~6,2%
set File=sqlimporter2_sharedsp ace_%Stamp %.txt
set Stamp=%DATE:~-10,2%%DATE:~
set File=sqlimporter2_sharedsp
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
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_sharedsp ace_%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
@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_sharedsp
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
Toggle HighlightingOpen in New WindowSelect All
~bp
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
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
~bpToggle HighlightingOpen in New WindowSelect All
~bp
ASKER
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
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.
set t=0%TIME:~-11,2%%TIME:~-8,
Notice the leading '0' and the negative values.
Oops! not sure why I posted here! Please ignore!
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_sharedsp
I htink that is what you have there... if not ask:
yyyymmdd_hhmm
Steve