Rename in batch file

May 31, 2010 at 19:20:05
Specs: Windows 7 Ultimate 32 bit, 3 GHz / 1919 MB
I wish to automaticlly rename a backup file to include the current date. The following script is what I have tried.

SET LOCAL
cd /d D:\Images
dir
if exist MyBackup rename D:\Images\ MyBackup: "%date:/=.%"
pause
exit

The pause and dir commands are i\n there for troubleshooting.
The backup MyBackup.tib is in folder Images On drive D:. I have tried easter-egging different arrangements but have had no luck.
The file MyBackup.tib does, in fact,exist in folder D:\Images\.

William Lockie


See More: Rename in batch file

Report •

#1
May 31, 2010 at 19:32:12
the only thing i can't figure out is what the colon does:
rename D:\Images\ MyBackup: "%date:/=.%"
(but thenagin, i don't have win7 ?)
also, space between "Images\" and "Mybackup"
("Images\ Mybackup:")
mebbe try removing those two artifacts...

Report •

#2
May 31, 2010 at 19:46:05
@echo off > d.d & setLocal enableDELAYedeXPansion

cd /d D:\Images
call :YMD
if exist MyBackup ren MyBackup %YYYYMMDD%
goto :eof

:YMD

>> d.d echo E 0100 B4 2A CD 21 B4 4C CD 21
>> d.d echo N DAY.COM
>> d.d echo RCX
>> d.d echo 8
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 F0 B4 4C CD 21
>> d.d echo N MONTH.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 89 C8 B4 4C CD 21
>> d.d echo N YEAR.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 D0 B4 4C CD 21
>> d.d echo N DAT.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo Q

debug < d.d > nul
del d.d

dat
for %%a in (1 2 3 4 5 6 7 8 9                                                                  ) do if errorlevel %%a set DD=0%%a
for %%a in (                  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do if errorlevel %%a set DD=%%a

day
if errorlevel 0 set day=Sunday
if errorlevel 1 set day=Monday
if errorlevel 2 set day=Tuesday
if errorlevel 3 set day=Wednesday
if errorlevel 4 set day=Thursday
if errorlevel 5 set day=Friday
if errorlevel 6 set day=Saturday

month
for %%a in (1 2 3 4 5 6 7 8 9         ) do if errorlevel %%a set MM=0%%a
for %%a in (                  10 11 12) do if errorlevel %%a set MM=%%a

year
if errorlevel 215 set YYYY=2007
if errorlevel 216 set YYYY=2008
if errorlevel 217 set YYYY=2009
if errorlevel 218 set YYYY=2010
if errorlevel 219 set YYYY=2011
if errorlevel 220 set YYYY=2012

set   YYYYMMDD=%YYYY%%MM%%DD%

del day.com
del month.com
del year.com
del dat.com

::============================ DONE YMD ==========================

goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#3
June 3, 2010 at 08:21:51
Wow, that's a long one.
However my requirements have changed.
I now wish to rename a folder if it exists.

I have tried the following but the "if" statement does not see the folder that, in fact, is there.
---START--
SET LOCAL
cd /d D:\
ift exist D:\WindowsImagesMyBackup\ rename D:\WindowsImagesMyBackup "%date:/=.%"
exit
--END--

William Lockie


Report •

Related Solutions

#4
June 3, 2010 at 09:06:33
That syntax of if exist works in xp, unless "ift" wasn't just a typo added in at post time.

You may be able to use dir instead:

>nul 2>&1 dir /ad "D:\WindowsImagesMyBackup\" && ren "D:\WindowsImagesMyBackup" "%date:/=.%"

Provided your %date% doesn't contain any other illegal character, with the exception
of "/", and there isn't any files or folders named what %date:/=.% expands to.

Also:

Hi M2


Report •

#5
June 3, 2010 at 13:25:22
Hi Judago

Good luck with date substrings.


=====================================
Helping others achieve escape felicity

M2


Report •

#6
June 3, 2010 at 22:01:49
@M2

I know the issues with date in cmd as well as anyone, I'm done with trying to convince people not to use substrings/delim replacement. If they want their script not to be portable that's fine with me ;)


Report •

Ask Question