AutoHotkey Community

It is currently May 27th, 2012, 4:22 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: February 23rd, 2010, 6:11 am 
Offline

Joined: December 6th, 2007, 12:48 pm
Posts: 364
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/Miscell ... 14402.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/Operati ... 11296.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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 23rd, 2010, 7:56 am 
Offline

Joined: April 19th, 2005, 10:26 am
Posts: 2249
Location: switzerland
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 23rd, 2010, 7:38 pm 
Offline

Joined: December 6th, 2007, 12:48 pm
Posts: 364
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? :)

_________________
AHK is perfect.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 23rd, 2010, 8:57 pm 
Offline

Joined: April 19th, 2005, 10:26 am
Posts: 2249
Location: switzerland
I don't know, it works as batch (xy.bat)
easiest seems to be with ahk
Code:
filemove,hope.txt,hope_%A_now%.txt


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 12:30 am 
Offline

Joined: December 6th, 2007, 12:48 pm
Posts: 364
Humm, indeed! Except that the output format is:
test_20100223203227.txt ! How to make the format neat but preserving the A_now function? :)

_________________
AHK is perfect.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 9:15 am 
Offline

Joined: April 19th, 2005, 10:26 am
Posts: 2249
Location: switzerland
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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group