Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename file by appending file name with todays date MMYY 2

Status
Not open for further replies.

awaria

IS-IT--Management
Sep 16, 2010
100
US
I want to rename a file by appending today's date in the formay of MMYY.

Example:

Current File = file.csv
Desired File = file1011.csv

I have the below script, however, it renames the file with a four digit year file102011.csv.

Appreciate any tips and help.

Thanks,

AW

-------------
@Echo Off
Set MMyy=%DATE:~4,2%%DATE:~10,4%
ren \\FHGP1\finance\andrew\medassets\file.csv file%date:~4,2%%date:~10,4%.csv
-------------
 
This code is highly dependant on the Date/Time format selected in Control Panel. You should be able to tailor it to your needs.
Code:
@Echo Off

FOR /F "tokens=2-4 delims=/ " %%i in ('Date /t') DO ( 
  SET Month=%%i
  SET Day=%%j
  SET Year=%%k
)

SET ShortYear=%Year:~2,2%

echo Day = %Day%
echo Month = %Month%
echo Year = %Year%
echo ShortYear = %ShortYear%

echo %Month%%ShortYear%
 
Thanks for your post.

I am not able to get it t work. I am not a programmer so my gap in knowledge is probably the obstacle to success.

How do I integrate your code with my ren statement?

AW
 
The supplied code is dependant on your Regional settings in Control Panel. My system is set to English (United States). If your's is not then it is likely the FOR command will not parse the Date command correctly.

Assuming the same regional settings:
Code:
@Echo Off

FOR /F "tokens=2-4 delims=/ " %%i in ('Date /t') DO (
  SET Month=%%i
  SET Day=%%j 
  SET Year=%%k
)

ren \\FHGP1\finance\andrew\medassets\file.csv file%Year:~2,2%.csv
 
eh, change the last line to read:
Code:
ren \\FHGP1\finance\andrew\medassets\file.csv file%Month%%Year:~2,2%.csv
 
Thanks for your post.

I am not able to get it t work. I am not a programmer so my gap in knowledge is probably the obstacle to success.

How do I integrate your code with my ren statement?

AW
 
I've shown your REName statement in the provided code, once where is append only the year then in the amended statement where it appends the month and year as you desired.

What is not working?
 
Freestone,

that does not work on foreign language XP (or there is a typo in your code)... output is as follows:

Code:
C:\test1>shorttest
Day =
Month =
Year =
ShortYear = ~2,2
~2,2

C:\test1>

the following will work on a German XP (date is in the DD.MM.YYYY format):
Code:
@echo off
set fy=%date:~-4%
set sy=%date:~-2%
set mo=%date:~-7,2%
set da=%date:~-10,2%

echo FullYear/Jahr: %fy%
echo ShortYear/Jahr(zweistellig): %sy%
echo Month/Monat: %mo%
echo Day/Tag: %da%

and for the American version (date is in the MM/DD/YYYY format), just switch the variables MO and DA respectively...
Code:
@echo off
set fy=%date:~-4%
set sy=%date:~-2%
set da=%date:~-7,2%
set mo=%date:~-10,2%

echo ShortYear: %sy%
echo FullYear: %fy%
echo Month: %mo%
echo Day: %da%

have fun...

Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 
Here is the code, that I have borrowed wholely from you. Thx
I have the code in a .cmd file, and when it runs, nothing happens.
Just a quick dos cmd window flash, then no change to the file.
The .CMD file is in the same directory as the file.csv, file.
----------
@Echo off

FOR /F "tokens=2-4 delims=/ " %%i in ('Date /t') DO (

SET Month=%%i
SET Day=%%j
SET Year=%%k
)

ren \\FHGP1\finance\andrew\medassets\file.csv file%Month%%Year:~2,2%.csv
-----------
 
As I had warned, the code is dependant on Regional settings and it appears yours is not English (United States).

If you replace the @echo off with @echo on and add a pause at the end of the file, you will see more of what is going wrong.

@BadBigBen - Many thanks for jumping in here. Your code is easier to understand and I will keep that tucked away.

awaria, I hope BBB's example helps you. Run his code and see if the output is in the correct format. If so then you can use his code and adapt your REName statement using his variables MO and SY, e.g.

ren \\FHGP1\finance\andrew\medassets\file.csv file%mo%%sy%.csv
 
with my CODE above, yours should look like this:

Code:
@echo off
set sy=%date:~-2%
set mo=%date:~-10,2%

ren ren \\FHGP1\finance\andrew\medassets\file.csv file%mo%%sy%.csv

to see if the above throws an error, just place a PAUSE command at the end, or have it wait for an input (set /P input), alternatively you could open a CMD window (run box, and type CMD, then ENTER), then navigate to said folder you have your script and file in and then execute the file from the CMD line in that window...

e.g. CD C:\FHGP1\finance\andrew\medassets\ and then enter MyBatFile.bat
or directly from the DOS box, e.g. C:\FHGP1\finance\andrew\medassets\MyBatFile.bat...




Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 
Freestone,

no problem, you know that I like to simplify things, I guess it goes with the territory... ;-) (KISS- Keep It Simple Stupid)...

for instance, on an American English XP, the %DATE% output would be: Thu 10/20/2011

on the German version, the output is: 20.11.2011

note the differences in the output? not only are the month and day reversed, but also the American adds the day name...

thus if it counts from the front, it will not work correctly...

my code counts it from the back, so set sy=%date:~-2% only counts two places and that gets the ShortYear, and for the month, set mo=%date:~-10,2% starts from the tenth place (counted from behind, including the separators) and enters the two characters from there, which is 10...

hope this explains it a bit for the OP...


Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 
Thanks, both, for sharing your expertise.

My system is set to United States (English), however, still no file name change. The dos window shows the file name changed, as desired, however, it ends with a message "the syntax of the command is incorrect", leading to no change.

AW
 
I had a typo, now the message says "the system acnnot find the path speciied".

Thanks again for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top