Solved

For tokens - date and time

Posted on 2014-10-27
2
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 56

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

622 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