AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

HElp me appending date to a file in the command line

 
Reply to topic    AutoHotkey Community Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
Da Rossa



Joined: 06 Dec 2007
Posts: 361

PostPosted: Tue Feb 23, 2010 5:11 am    Post subject: HElp me appending date to a file in the command line Reply with quote

Hi
I'd like to know how to rename a file to itself, just adding the current date as a suffix, without using AHK. I mean, in the command line itself.

Like:
Code:
ren C:\test\test.txt C:\test\test_23-2-10__02-12.txt



I Googled a bit, and found some weird and complicated stuff, which didn't work in here I don't know why:

Quote:
~~ Suffix a date: ~~
http://www.experts-exchange.com/Miscellaneous/Games/DOS_Games/Q_10314402.html
ren c:\temp\abc.txt abc%date:~4,2%-%date:~7,2%-%date:~10%.txt
::temadan


and

Quote:
~~ Copy, adding system date: ~~
http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20611296.html
@ECHO OFF
FOR /F "TOKENS=2-4 DELIMS=/ " %%F IN ('DATE /T') DO (SET TODAY=%%F%%G%%H)
COPY /Y /B C:\TEMP\*.VCH C:\Upload\Combined.vch
REN C:\Upload\Combined.vch Combined-%TODAY%.vch
::pbarrette


Please help!
_________________
AHK is perfect.
Back to top
View user's profile Send private message
garry



Joined: 19 Apr 2005
Posts: 2212
Location: switzerland

PostPosted: Tue Feb 23, 2010 6:56 am    Post subject: Reply with quote

found this , worked for me with date
http://www.computerhope.com/issues/ch000987.htm
see delimiter, token , variables d e f
xy.bat=
Code:
for /f "tokens=1-3 delims=." %%d in ("%date%") do rename "hope.txt" hope_%%d-%%e-%%f.txt
pause

Quote:

for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "hope.txt" %%e-%%f-%%g.txt

* for /f - The for command and the /f switch.
* "tokens=1-5 delims=/ " - How many tokens the incoming data (in this case the date) will be broken into; 1-5 is five different tokens. Finally, delims is short for delimiters and is what is used to break up the date, in this example the / (forward slash) and a space (space before the quote).
* %%d - The beginning character used for the token. Since there are 5 tokens in this example it would be d,e,f,g, and h.
* in ("%date%") - The data being used, in this case the %date% is the current date of the computer.
* do - What the for command will do. The rename command can be substituted for anything else.
* rename "hope.txt" %%e-%%f-%%g.txt - rename the file "hope.txt" to the tokens e,f, and g with a .txt file extension. This example also has a - (hyphen) in-between each token to separate the month, day, and year in the file name.

When %date% is used in a batch file it displays the date in the following format: Sun 09/02/2007 this command breaks this date into the tokens: "Sun" (%%d), "09" (%%e), "02" (%%f), and "2007" (%%g).

In this example using the above date mentioned hope.txt would be renamed to 09-02-2007.txt.

Time

for /f "tokens=1-5 delims=:" %%d in ("%time%") do rename "hope.txt" %%d-%%e.txt

This command is very similar to the above example. However, instead of using the forward slash and space to break up the data we're using a : (colon) because the time is split up with this character. Finally, because we're renaming the file to only the hour and minute this example is only using the d and e token. Additional information about what everything in this line means is found in the above date example.

When %time% is used in a batch file it displays the time in the following format: 19:34:52.25, this command breaks this time into the tokens: "19" (%%d), "34" (%%e), and "52.25" (%%f).

In this example using the above time mentioned hope.txt would be renamed to 19-34.txt.
Back to top
View user's profile Send private message
Da Rossa



Joined: 06 Dec 2007
Posts: 361

PostPosted: Tue Feb 23, 2010 6:38 pm    Post subject: Reply with quote

Well, thanks! But so far it didn't work..
Does the fact that I didn't use a batch file, but the command line directly interfere? I tried both examples and from both I got this message returned: "%d was unexpected at this time".
Why? Smile
_________________
AHK is perfect.
Back to top
View user's profile Send private message
garry



Joined: 19 Apr 2005
Posts: 2212
Location: switzerland

PostPosted: Tue Feb 23, 2010 7:57 pm    Post subject: Reply with quote

I don't know, it works as batch (xy.bat)
easiest seems to be with ahk
Code:
filemove,hope.txt,hope_%A_now%.txt
Back to top
View user's profile Send private message
Da Rossa



Joined: 06 Dec 2007
Posts: 361

PostPosted: Tue Feb 23, 2010 11:30 pm    Post subject: Reply with quote

Humm, indeed! Except that the output format is:
test_20100223203227.txt ! How to make the format neat but preserving the A_now function? Smile
_________________
AHK is perfect.
Back to top
View user's profile Send private message
garry



Joined: 19 Apr 2005
Posts: 2212
Location: switzerland

PostPosted: Wed Feb 24, 2010 8:15 am    Post subject: Reply with quote

can also use a loop to rename all files or all files.txt
Code:
;-- rename file C:\test\test.txt
A=%A_now%
stringmid,YY,A, 1,4
stringmid,MM,A, 5,2
stringmid,DD,A, 7,2
stringmid,HR,A, 9,2
stringmid,MI,A,11,2
AA=_%DD%-%MM%-%YY%__%HR%-%MI%
F1=C:\test\test.txt
   SplitPath,F1, name, dir, ext, name_no_ext, drive
F2=%dir%\%name_no_ext%%aa%.%ext%
filemove,%F1%,%F2%
ifexist,%dir%
    run,%dir%
return


rename all text files in c:\test
Code:
gosub,time1
FOLD=c:\test
Loop, %fold%\*.txt, 0, 1
   {
   SplitPath,A_LoopFileFullPath,name, dir, ext, name_no_ext, drive
   F2=%dir%\%name_no_ext%%aa%.%ext%
   filemove,%A_LoopFileFullPath%,%F2%
   }
ifexist,%fold%
    run,%fold%
return

time1:
A=%A_now%
stringmid,YY,A, 1,4
stringmid,MM,A, 5,2
stringmid,DD,A, 7,2
stringmid,HR,A, 9,2
stringmid,MI,A,11,2
AA=_%DD%-%MM%-%YY%__%HR%-%MI%
return
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> General Chat All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group