Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

For tokens - date and time

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
tonelm54
Asked:
tonelm54
1 Solution
 
Steve KnightIT ConsultancyCommented:
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
 
Bill PrewCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now