Solved

For tokens - date and time

Posted on 2014-10-27
2
175 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 51

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now