Solved

For tokens - date and time

Posted on 2014-10-27
2
197 Views
Last Modified: 2014-11-18
Good evening,
Im trying to split date and time up, so I can use it in a filename output. So far Ive got the following code:-
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C   
)    

@For /F "tokens=2,3,4 delims=: " %%A in ('time /t') do @( 
    Set Hour=%%A
    Set Minute=%%B
    Set Second=%%C
)  
@For /F "tokens=2 delims=. " %%A in ('time /t') do @( 
    Set Milsecond=%%A
) 

Open in new window


What I want to do it use the above before the following code so it will save the output to a filename with todays date and time:-
mysqldump -u root -pPassword myDatabase >"w:\myDatabase %Year%-%Month%-%Day% %Hour%%Minute%%Second%%Milsecond%.sql"

Open in new window


To debug the issue I've tried to do the following after the split:-
@echo Day = %Day%
@echo Month = %Month%
@echo Year = %Year%    

@echo Hour = %Hour%
@echo Minute = %Minute%
@echo Second = %Second%

@echo Milisecond = %Milsecond%

Open in new window


Any ideas what I'm doing wrong with my code?
0
Comment
Question by:tonelm54
2 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40406947
Really would suggest a different approahc, only because date and time is very individual and %date% and %time% are down to the users preferences, OS, language etc.  There are ways using WMIC and date /t output but I prefer a line of VBScript embedded in the batch file, see this:

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

Steve
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40414827
In terms of the code you started with, give this a try, I think it will do what you want.

@echo off
setlocal

For /F "tokens=2-4 delims=/ " %%A in ("%DATE%") do (
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)

For /F "tokens=1-4 delims=:." %%A in ("%TIME%") do (
    Set Hour=%%A
    Set Minute=%%B
    Set Second=%%C
    Set Milsecond=%%D
) 

REM mysqldump -u root -pPassword myDatabase >"w:\myDatabase %Year%-%Month%-%Day% %Hour%%Minute%%Second%%Milsecond%.sql"
echo "w:\myDatabase %Year%-%Month%-%Day% %Hour%%Minute%%Second%%Milsecond%.sql"

echo Month = %Month%
echo Day = %Day%
echo Year = %Year%

echo Hour = %Hour%
echo Minute = %Minute%
echo Second = %Second%

echo Milisecond = %Milsecond%

Open in new window

~bp
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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