1.

Solve : Rename folders?

Answer»

We have 5 sets of backups, each in a different folder, IE;

BackupFolder\Server1Name
BackupFolder\Server1Name.1
BackupFolder\Server1Name.2
BackupFolder\Server1Name.3
BackupFolder\Server1Name.4

BackupFolder\Server2Name
BackupFolder\Server2Name.1
BackupFolder\Server2Name.2
BackupFolder\Server2Name.3
BackupFolder\Server2Name.4

Each night the folder with no number will be renamed to .1, all other folder numbers must inc by 1 and .4 to be deleted

Another condition is that if the folder with no number is empty...the backup failed...it shouldn't rename any of the folders for that server.

My CODE so far is...

Code: [SELECT]@echo off
setlocal enabledelayedexpansion

::Need to create another FOR to CHECK if any files in "Server1Name"
:: Count BACKWARDS from 4 to 1 and list all folders that end with the specific number
for /L %%N IN (4,-1,1) do (
for /D %%I in ("\backup testing\*.%%N") do (
set input=%%I
::Get Folder name only
set filename=%%~nxI
::Remove last 2 chars from filename
set output=!filename:~0,-2!
:: INC number ready for new name
set /A newnum=%%N+1
::Echo the output, will REN the folders...
echo From !input!
echo To !output!.!newnum!
)
)
::This is what I'm stuck on...need to rename folder with no number to "ServerName.1"
for /D %%I in ("\backup testing\*") do (
set input=%%I
set filename=!input:~0,-2!
::Check if new folder is same as old...if it's not echo the folder...will use ren when it's in use
if '!prev! NEQ '!filename! echo !filename!
set prev=!filename!
)
::Delete .5 folders
for /D %%I in ("\backup testing\*.5") do rd "%%I" /s/q

If we have folders named;
ddd
ddd.1
ddd.2
ddd.3
ddd.4
eee
eee.1
eee.2
eee.3
eee.4

The 2nd FOR will display...
d
ddd
e
eee

...any reccomendations to make it work?

I could use a list file, but I'd rather fully automate it so if any folder is created in the backup folder it will be renamed automatically...



Discussion

No Comment Found