Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 



Sign in to follow this  
phillyman2004

Rename File With Current Date? (Batch File?

Recommended Posts

I am looking for a way to rename a file with the current date

Basically...

Take MasterTracker.xls

and make a copy , and rename it to June 6th.xls

and have this batch file execute each night and move the old date to an archive folder and recreate the file for that day

Is it possible ??

Rob

Share this post


Link to post
Share on other sites

Here you go (didnt have a chance to try it, so I am waiting for your feedback)

@Echo off

rem Reading input file
If "%1" EQU "" (
 Echo You must use parameter - name of file to rename.
 Goto :EOF
) ELSE (
 Set strFile2Rename=%1
)
If not exist %strFile2Rename% (
Echo %strFile2Rename% was not found!
Goto :EOF
)
For /f "usebackq delims=. tokens=1,2" %%i IN (`echo %strFile2Rename%`) Do Set strFileExtension=%%j

rem Main code
call:ConvertDate
call:RenameFile
Goto:EOF

:ConvertDate
rem Reading date format, converting and saving date
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 set A=%%a&set B=%%b&set C=%%c)
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
 for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
   set dd=%%d&set mm=%%e&set yy=%%f))
Goto:EOF

:RenameFile
Echo File %strFile2Rename% will be renamed to %dd%-%mm%-%yy%.%strFileExtension%
ren %strFile2Rename% %dd%-%mm%-%yy%.%strFileExtension%

Share this post


Link to post
Share on other sites
jaclaz    943

There was another thread about this, with examples posted, here:

http://www.msfn.org/board/index.php?showtopic=37572

and this is a file I use normally:

@echo off
if not exist turiweb md turiweb
if not exist turiweb\log.log date /t > turiweb\log.log

date /t > turiweb\date.now
time /t > turiweb\time.now

Set CURRDATE=%TEMP%\CURRDATE.TMP
Set CURRTIME=%TEMP%\CURRTIME.TMP

DATE /T > %CURRDATE%


Set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
For /F %PARSEARG% %%i in (%CURRDATE%) Do SET DDMMYYYY=%%j_%%k_%%l



Echo Copying file turiweb.xml in turiweb\turiweb_%DDMMYYYY%.xml
if exist turiweb\turiweb_%DDMMYYYY%.xml goto error
if not exist turiweb\turiweb_%DDMMYYYY%.xml copy turiweb.xml turiweb\turiweb_%DDMMYYYY%.xml > nul





:OK

echo OK,File turiweb_%DDMMYYYY%.xml correctly created
echo OK,File turiweb_%DDMMYYYY%.xml correctly created > turiweb\temp.now
Goto end

:error

Echo Error, file turiweb_%DDMMYYYY%.xml already existing, file NOT copied
echo Error, file turiweb_%DDMMYYYY%.xml already existing, file NOT copied > turiweb\temp.now

:end
copy turiweb\log.log+turiweb\date.now+turiweb\time.now+turiweb\temp.now turiweb\log.log > nul
del turiweb\*.now > nul
pause

(the good thing about this one is that keeps a log file)

Just change the occurrencies of turiweb and turiweb.xml

jaclaz

Share this post


Link to post
Share on other sites
I will write it for you...

Would you be satisfied with script for parameters, like

Rename2Date.bat file_to_rename.exe

?

Very Very Close to what i need , I just need it to leave a the original file alone

my folder should look like this

---------------------------

MasterTracker.xls <--- Main

June6th.xls

June7th.xls

June8th.xls

.

.

.

.

i dont care about the date format , I plan on executing this batch file every morning at 7am (Windows Scheduler) , to make a new file for that day , and open it

Share this post


Link to post
Share on other sites

Here is one that will rename a text file to the date, just change the

extention to suit your needs.

It is based on the script i made for folders by date.

This will do only 1 file per folder or directory.

@echo off

cls

color F2

mode con: Cols=55 Lines=3

Title What Year Is It Test 2

NLSFUNC

SET D1=%date:~-4,10%

SET D2=%date:~7,-5%

SET D3=%date:~4,-8%

SET D4=%date:~0,-10%

if exist %D3%-%D2%-%D1%.Txt goto E1

if not exist %D3%-%D2%-%D1%.txt goto W1

:W1

::mkdir %D3%-%D2%-%D1%

if exist *.txt goto W2

:w2

ren *.txt %D3%-%D2%-%D1%.txt

echo.

echo File Was Rename to %D3%-%D2%-%D1%.txt Was Made!

ping -n 3 127.0.0.1>nul

exit

:e1

echo.

echo File Was Already Made!

ping -n 3 127.0.0.1>nul

This I added A time Code So it Changes
@echo off

cls

color F2

mode con: Cols=55 Lines=3

Title What Year Is It Test 2

NLSFUNC

SET D1=%date:~-4,10%

SET D2=%date:~7,-5%

SET D3=%date:~4,-8%

SET D4=%date:~0,-10%

set t1=%time:~-2,8%

if exist *.txt goto W2

if exist %D3%-%D2%-%D1%-%t1%.Txt goto E1

:w2

ren *.txt %D3%-%D2%-%D1%-%t1%.Txt

echo.

echo File Was Rename to %D3%-%D2%-%D1%-%t1%.Txt Was Made!

ping -n 3 127.0.0.1>nul

exit

:e1

echo.

echo File Was Already Made!

ping -n 3 127.0.0.1>nul

Share this post


Link to post
Share on other sites
Noise    2

@smokinggunman,

That's very nice use of the date variable, that will come in extremely handy in the future! Thanks. Couple of questions though. Will your script work on Windows 2000? Why are you running nlsfunc?

Share this post


Link to post
Share on other sites
net_user    0

try this to renameing a file. from there you can copy to where every you want to.

rename "c:\Take MasterTracker.xls" %Date:~-10,2%_%Date:~-7,2%_%Date:~-4,4%.xls

Share this post


Link to post
Share on other sites

NLSFUNC I use this from what i read so it work in other langauges.

I have never been able to figure out exacly what it does,

It doesnt cause any erors so I left it in.

As to windows 2000 I am not sure if it will work, but since it a cmd file,

it in therory should work.

Maybe some of the more gifted members can explain this NLSFUNC function

better then I can.

Share this post


Link to post
Share on other sites
jaclaz    943

...also this might be interesting:

http://winadmin.sourceforge.net/warmzip.html

waRmZip.wsf v1.6 (Jul 2004) - http://winadmin.sourceforge.net  _____

Utility to clean up/free up space in a folder (and its subfolders),

* compressing files after a certain number of days (with external program)

* rotating files after a certain number of days/above a certain size

* deleting files and/or empty folders after a certain period of time

* moving files to other folders/disks

jaclaz

Share this post


Link to post
Share on other sites
jonathan987    0

Yes offcourse is possible you can change date, extensions or anything you want by using Batch Rename Files Tool. You can easily found hier BatchRenameFiles.org.

Edited by jonathan987

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×