Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have this mini Windows Script which is execute every hour it basically create a file every hour but I need to rename the file using date (which is already done) but I am not able to add the time

.....code that creates my perfstats01.txt file:

SET date="%date:~0,2%-%date:~3,2%-%date:~6,6%"
ren C:\perfstats01.txt "%date%".txt

so I will be renaming the file perfstats01.txt like this:

07-03-2012_10:30am.txt

or

07-03-2012_13:01.txt

any idea how to accomplish that guys?

thanks a lot

share|improve this question
add comment

2 Answers

The colon is not valid in a file name. So, taking the colon out, I would do the following:

(if you want 12 hour clock with AM or PM)

:GetDate
 FOR /F "DELIMS=" %%T IN ('TIME /T') DO SET @TIME=%%T
 FOR /F "TOKENS=2" %%D IN ('DATE /T') DO SET @DATE=%%D
 FOR /F "TOKENS=1-4 DELIMS=-/ " %%D IN ('DATE /T') DO (
     SET @DAY=%%D
     SET @DD=%%F
     SET @MM=%%E
     SET @YYYY=%%G
 )
 SET @HOUR=%@TIME:~0,2%
 SET @SUFFIX=%@TIME:~6,2%
 SET @NOW=%@HOUR%%@TIME:~3,2%
 SET @NOW=%@NOW: =0%
 SET @TODAY=%@YYYY%-%@MM%-%@DD%
ren c:\perfstats01.txt %@DD%-%@MM%-%@YYYY%_%@NOW%%@SUFFIX%.txt

Or this instead:

(If you want a 24 hour clock)

:GetDate
 FOR /F "DELIMS=" %%T IN ('TIME /T') DO SET @TIME=%%T
 FOR /F "TOKENS=2" %%D IN ('DATE /T') DO SET @DATE=%%D
 FOR /F "TOKENS=1-4 DELIMS=-/ " %%D IN ('DATE /T') DO (
     SET @DAY=%%D
     SET @DD=%%F
     SET @MM=%%E
     SET @YYYY=%%G
 )
 SET @HOUR=%@TIME:~0,2%
 SET @SUFFIX=%@TIME:~6,2%
 IF /I "%@SUFFIX%"=="AM" IF %@HOUR% EQU 12 SET @HOUR=00
 IF /I "%@SUFFIX%"=="PM" IF %@HOUR% LSS 12 SET /A @HOUR=%@HOUR% + 12
 SET @NOW=%@HOUR%%@TIME:~3,2%
 SET @NOW=%@NOW: =0%
 SET @TODAY=%@YYYY%-%@MM%-%@DD%

ren C:\perfstats01.txt %@DD%-%@MM%-%@YYYY%_%@NOW%.txt
share|improve this answer
    
This :GetDate block is almost always part of the standard header in my batch files... I almost always need a date of some format, and this breaks out all the individual pieces (except minute, I just realized) –  Glenn Sullivan Mar 7 '12 at 16:54
    
in both scripts I get a file name like this 2012-03-_0130p..txt missing the day and the second letter for pm or am –  karlochacon Mar 7 '12 at 19:33
    
What operating system? Different version of Windows have different results from the date command. Type "date /t" at a command prompt and let me know what it looks like. –  Glenn Sullivan Mar 9 '12 at 16:20
add comment

I don't think you can do this within a batch file. You can, however, use Windows Script Host. Here is an example:

'// Initialize variables
Dim oShell, oFSO, oFile
set oShell = CreateObject("WScript.Shell")
set oFSO = CreateObject("Scripting.FileSystemObject")

'// The ExpandEnvironmentStrings function allows you to pull
'// and save an environment variable!
Set currentDate = oShell.ExpandEnvironmentStrings("%date%")

'// There is no 'rename' function in Windows Script, so use this instead.
set oFile = oFSO.GetFile("c:\oldname.txt")
file.name = "newName.txt"

'// This ensures all file resources are released.
set file = nothing
set fso = nothing

Be sure to customize the date string to however you need it. This should go without saying (but I've seen people get bit by it time and again), that the identity that this script runs under needs to have appropriate permissions for the file.

share|improve this answer
    
I created a file named scriptWB.vbs and when I double click on it I get this screencast.com/t/OKM8myByVoIf –  karlochacon Mar 7 '12 at 16:44
1  
This was designed to supliment your existing script... it would be called by your original script after the %date% environment variable is created... but it will still fail because a colon in not valid in a filename. –  Glenn Sullivan Mar 7 '12 at 16:56
    
But it would be called like this in place of your REN command in the original script: cscript scriptWB.vbs –  Glenn Sullivan Mar 7 '12 at 16:57
    
Glen is correct (thanks Glen)... this is an incomplete example, just to give you an idea of how you could solve your problem. But I think I like Glenn's solution better :P –  newmanth Mar 7 '12 at 17:42
    
in both scripts I get a file name like this 2012-03-_0130p..txt missing the day and the second letter for pm or am –  karlochacon Mar 7 '12 at 19:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.