Page 1 of 1

How to rename a file with a date filename

Posted: 11 Feb 2015 20:15
by MeSH
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

Re: How to rename a file with a date filename

Posted: 11 Feb 2015 22:21
by Samir
Where do you get the date for the file name?

Re: How to rename a file with a date filename

Posted: 11 Feb 2015 23:16
by MeSH
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?

Re: How to rename a file with a date filename

Posted: 11 Feb 2015 23:27
by ShadowThief
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.

Re: How to rename a file with a date filename

Posted: 12 Feb 2015 08:26
by Squashman
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:\>

Re: How to rename a file with a date filename

Posted: 12 Feb 2015 08:42
by foxidrive
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

Re: How to rename a file with a date filename

Posted: 12 Feb 2015 10:26
by Samir
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.