Solved

For tokens - date and time

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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