[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-13
9
Medium Priority
?
398 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:mmalik15
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 43

Expert Comment

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

http://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
0
 

Author Comment

by:mmalik15
ID: 36961344
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
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36961352
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:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 36961377
Ok then to get ddmmyyyy_hhmm do this.  Have just swapped over the day and year bits .

Steve

echo wscript.echo  right(100+day(date),2) ^& right(100+month(date),2) ^& year(date) ^& "_" ^& 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

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

Expert Comment

by:Bill Prew
ID: 36961399
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
0
 

Author Closing Comment

by:mmalik15
ID: 36961411
thanks
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36961444
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
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36961585
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.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36961589
Oops! not sure why I posted here! Please ignore!
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Starting up a Project

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question