General discussion

Locked

Renaming files

By LanMan Phil ·
Is there a way from a command prompt to copy or rename a file and add the date as part of the file name automatically? I'm wanting to use this in a batch file. Thanks

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Renaming files

by nichorton In reply to Renaming files

By: Nick

==============================
copy and paste
==============================

@echo off
rem Determine which pass we are making
if "%1"=="" goto _1stpass
if "%2"=="" goto _wdpass
if "%3"=="" goto _daypass
if "%4"=="" goto _mmpass
rem Extract the year
find "/20%4" tmp$$$.$$$ > nul
if not errorlevel==1 if errorlevel==0 set yearnr_=%4
find "/20%4" tmp$$$.$$$ > nul
if not errorlevel==1 if errorlevel==0 set yearnr_=%4
goto _end
rem Extract the month
:_mmpass
find "%3/" tmp$$$.$$$ > nul
if not errorlevel==1 if errorlevel==0 set monthnr_=%3
goto _end
rem Extract the day
:_daypass
find "/%2/" tmp$$$.$$$ > nul
if not errorlevel==1 if errorlevel==0 set daynr_=%2
goto _end
rem As an extra demo also get the name ofthe weekday
:_wdpass
find "%1" tmp$$$.$$$ > nul
if not errorlevel==1 if errorlevel==0 set weekday_=%1
goto _end
rem Put the date in a file so that find can be applied on it
:_1stpass
echo.|date > tmp$$$.$$$
rem Go through all the alternatives. Note the dummy x parameters
rem to determine which part of the date is being processed
for %%d in (Sun Mon Tue Wed Thu Fri Sat) do call %0 %%d
for %%d in (01 02 03 04 05 06 07 08 09 10) do call %0 x %%d
for %%d in (11 12 13 14 15 16 17 18 19 20) do call %0 x %%d
for %%d in (21 22 23 24 25 26 27 28 29 30 31) do call %0 x %%d
for %%d in (01 02 03 04 05 06 07 08 09 10 11 12) do call %0 x x %%d
for %%d in (97 98 99 00 01 02) do call %0 x x x %%d
rem Show the results
echo %weekday_%
echo%yearnr_%%monthnr_%%daynr_%
rem change this part
copy "c:\old\old.mdb" "d:\new\new_%weekday_%_%monthnr_%%daynr_%%yearnr_%.mdb"
rem Clean up
set weekday_=
set daynr_=
set monthnr_=
set yearnr_=
del tmp$$$.$$$
:_end

Collapse -

Renaming files

by Shanghai Sam In reply to Renaming files

Just what I was looking for, thanks

Collapse -

Renaming files

by DKlippert In reply to Renaming files

You could use the VBA statement "Name".
OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName ' Move and rename file.

While the "Name" statement can't contain wild cards, the variable can contain concatenate elements. Oldfile & Now

Collapse -

Renaming files

by Shanghai Sam In reply to Renaming files

Poster rated this answer

Collapse -

Renaming files

by BeerMonster In reply to Renaming files

Hi,
I'm assuming you know the filename before hand, if not you can easily code around it. For an NT4 and above system the main line you want is


for /F "tokens=2" %%A in ('date /t') do set myvar=%%A

This sets myvar to the current date. Ifyou want to cut out any back or forward slashes (depending on your locale) then add

set myvar=%myvar:/=%
set myvar=%myvar:\=%

now you can rename as -

ren myfile.txt newname%myvar%.txt

Hope this helps...

Collapse -

Renaming files

by Shanghai Sam In reply to Renaming files

Works great, thanks

Collapse -

Renaming files

by LanMan Phil In reply to Renaming files

This question was closed by the author

Back to Windows Forum
7 total posts (Page 1 of 1)  

Related Discussions

Related Forums