I want to rename a file
Let's say we have myFile.txt
I want to rename to myFile.300714.1900.txt
300714 - this is the date of the file (in UK date format ddmmyy)
1900 - this is the current time upto the minutes
Can this be done?
Is it possible to get the date of a file?
If not, I'd settle for just putting in the current date
I'm not sure where to start!
Any pointers would be great
Thanks
OM
Need help renaming a file - adding the date at the end
Moderator: DosItHelp
Re: Need help renaming a file - adding the date at the end
Code: Select all
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "stamp=%DD%%MM%%YYYY%.%HH%%Min%"
ren "myfile.txt" "myfile.%stamp%.txt"
pause
Re: Need help renaming a file - adding the date at the end
@foxidrive
thanks a million for the reply. i didn't know u had replied
else, i would have thanked much sooner
what i wanted was to right click on one or more files and rename them with the date inserted before the suffix
so if i had:
file.txt
picture.jpg
another-picture.gif
my-program.exe
and i right clicked and set my batchfile on these, they would get renamed to something like:
file.31-10-14.2.45pm.txt
picture.31-10-14.2.45pm.jpg
another-picture.31-10-14.2.45pm.gif
my-program.31-10-14.2.45pm.exe
i looked at your code, and can't figure out why you would use a loop?
>> for /f "tokens=2 delims==" %%a in
what does that do?
i've got other dos code that i use to run imagemagik with:
i would have planned to copy the code above and adapt what u have given to make it work on files that i right click
but... given that you already use delims== in your code... I'm thinking will this cause some sort of clash??
thanks in advance
thanks a million for the reply. i didn't know u had replied
else, i would have thanked much sooner
what i wanted was to right click on one or more files and rename them with the date inserted before the suffix
so if i had:
file.txt
picture.jpg
another-picture.gif
my-program.exe
and i right clicked and set my batchfile on these, they would get renamed to something like:
file.31-10-14.2.45pm.txt
picture.31-10-14.2.45pm.jpg
another-picture.31-10-14.2.45pm.gif
my-program.31-10-14.2.45pm.exe
i looked at your code, and can't figure out why you would use a loop?
>> for /f "tokens=2 delims==" %%a in
what does that do?
i've got other dos code that i use to run imagemagik with:
Code: Select all
for /f "delims=" %%a in (' dir *.jpg *.png /b /a-d ') do composite -gravity SouthEast E:\imagemagik\logo-big.png "%%a" "%%a"
i would have planned to copy the code above and adapt what u have given to make it work on files that i right click
but... given that you already use delims== in your code... I'm thinking will this cause some sort of clash??
thanks in advance
Re: Need help renaming a file - adding the date at the end
This can be put in your sendto folder and then you can use it on one or more files.
test it on some sample files first:
The for /f loop allows you to set a variable to some text in a file, or in the output of a command as it does here.
test it on some sample files first:
Code: Select all
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "stamp=%DD%%MM%%YYYY%.%HH%%Min%"
:loop
ren "%~1" "%~n1.%stamp%%~x1"
shift
if not "%~1"=="" goto :loop
OM3 wrote:i looked at your code, and can't figure out why you would use a loop?
>> for /f "tokens=2 delims==" %%a in
what does that do?
The for /f loop allows you to set a variable to some text in a file, or in the output of a command as it does here.
Re: Need help renaming a file - adding the date at the end
@foxidrive u are JUST AWESOME!
thank you! works a treat!
tokens=2?
%~1 - is this the arguement?
~n1 and ~x1 - where do these come from?
shouldn't they be declared somewhere?
1? why do these have 1? what does it mean? could i use 2, 3, 4 instead?
thank you
thank you! works a treat!
Code: Select all
"tokens=2 delims=="
tokens=2?
Code: Select all
:loop
ren "%~1" "%~n1.%stamp%%~x1"
shift
if not "%~1"=="" goto :loop
%~1 - is this the arguement?
~n1 and ~x1 - where do these come from?
shouldn't they be declared somewhere?
1? why do these have 1? what does it mean? could i use 2, 3, 4 instead?
thank you
Re: Need help renaming a file - adding the date at the end
OM3 wrote:%~1 - is this the arguement?
~n1 and ~x1 - where do these come from?
shouldn't they be declared somewhere?
1? why do these have 1? what does it mean? could i use 2, 3, 4 instead?
thank you
The documentation is in the help for the CALL command.
Here is a few links for you to read up on.
http://ss64.com/nt/call.html
http://www.robvanderwoude.com/parameters.php
Or you can just open up a cmd prompt and type: call /help
This may help you understand TOKENS. Read the parts about DELIMS and TOKENS.
ss64.com/nt/for_cmd.html
ss64.com/nt/for_f.html
Re: Need help renaming a file - adding the date at the end
OM3 wrote:%~1 - is this the arguement?
~n1 and ~x1 - where do these come from?
shouldn't they be declared somewhere?
1? why do these have 1? what does it mean? could i use 2, 3, 4 instead?
Squashman's given you good hints on how to find out a lot of things - you can then ask further questions if you need some more clarification.
~n and ~x are listed on the last page of the FOR /? help along with other modifiers.
The 1 refers to parameter 1 on the command line, and there are also parameters 2 to 9 (plus 0 which has a special purpose).
It can be %~na to %~nz too when using FOR loop variables (lower case alpha ones here).