How to rename a file with a date filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

How to rename a file with a date filename

#1 Post by MeSH » 11 Feb 2015 20:15

hello everyone! at last I remember my password here since our place was hit by Super Typhoon Haiyan...

here's my question now

how can I rename "report.xlsx" to current date today?

if i run this code:

COPY "report.xlsx" "%SystemDrive%\Users\%UserName%\Desktop\currentDate.xlsx"

the file name must not currentDate.xlsx. It must be 02-12-2015.xlsx or February-02-2015.xlsx

anyone can make this happen and can you I ask favor again?

directory pathing for windows XP and Win 7 is different right? in XP there is Document and Settings

I want is when the .bat file run it will detect first what OS the user is using

If WindowsXP then this code will run:
COPY "report.xlsx" "%SystemDrive%\docume~1\%UserName%\Desktop\currentDate.xlsx"

Else
COPY "report.xlsx" "%SystemDrive%\Users\%UserName%\Desktop\currentDate.xlsx"

thanks in advance

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: How to rename a file with a date filename

#2 Post by Samir » 11 Feb 2015 22:21

Where do you get the date for the file name?

MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

Re: How to rename a file with a date filename

#3 Post by MeSH » 11 Feb 2015 23:16

Samir wrote:Where do you get the date for the file name?


it's only a holder (currentDate.xlsx) it means that if anyone here will supply a code for that then the currentDate will be his/her reference where he/she will put the code.

it's like

currentDate as Date = Date.Now

MessageBox.Show("Date for today: " + currentDate)

same also in batch code:
COPY "report.xlsx" "%SystemDrive%\docume~1\%UserName%\Desktop\(currentDate).xlsx"

is it possible that when copy and paste a file it will change the name of the file to date?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to rename a file with a date filename

#4 Post by ShadowThief » 11 Feb 2015 23:27

MeSH wrote:COPY "report.xlsx" "%SystemDrive%\docume~1\%UserName%\Desktop\currentDate.xlsx"
COPY "report.xlsx" "%SystemDrive%\Users\%UserName%\Desktop\currentDate.xlsx"

For XP and above, both can be shortened to simply %USERPROFILE%\Desktop\currentDate.xlsx

The %date% variable contains today's date. I can't give you exact code because I don't know what format your date string is in, but you can use http://www.dostips.com/DtTipsStringMani ... .MidString to extract the substrings you need.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to rename a file with a date filename

#5 Post by Squashman » 12 Feb 2015 08:26

There is a %DATE% and %TIME% variable you can access with Batch files. How those variables are output are regionally specific. There are ways to get the date and time without having to know what its output format is but if you just want a straight up way to manipulate the date variable with batch look at these examples.
My date format is Day of Week Month/Day/Year
So to get the date I could put these commands in a batch file.

Code: Select all

H:\>echo %date%
Thu 02/12/2015

H:\>set cdate=%date:~4%

H:\>echo %cdate%
02/12/2015

H:\>set cdate=%cdate:/=%

H:\>echo %cdate%
02122015

H:\>

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to rename a file with a date filename

#6 Post by foxidrive » 12 Feb 2015 08:42

If you can use wmic.exe then this is a good way to get the date variables and which will work on other machines in the same way.

When using the %date% variable it can fail spectacularly on other machines because the format can be changed by the user,
as well as the format changing by country/region.

Code: Select all

:: The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

@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 "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: How to rename a file with a date filename

#7 Post by Samir » 12 Feb 2015 10:26

Samir wrote:Where do you get the date for the file name?
Sorry, I missed that you wanted the current date.

The other guys have given excellent suggestions. I've used the %date% and %time% variables in my batches.

Post Reply