Cleaning up a bat file

Don
Joined
21 Oct 2002
Posts
46,830
Location
Parts Unknown
Can someone give me a quick hand here

How do I clean this up?

Code:
set v=a
if exist "E:\backupa.7z" set v=b
if exist "E:\backupb.7z" set v=c
if exist "E:\backupc.7z" set v=d
if exist "E:\backupd.7z" set v=e
if exist "E:\backupe.7z" set v=f
if exist "E:\backupf.7z" set v=g
if exist "E:\backupg.7z" set v=h
if exist "E:\backuph.7z" set v=i
if exist "E:\backupi.7z" set v=j
if exist "E:\backupj.7z" set v=k
if exist "E:\backupk.7z" set v=l
if exist "E:\backupl.7z" set v=m
if exist "E:\backupm.7z" set v=n
if exist "E:\backupn.7z" set v=o
if exist "E:\backupo.7z" set v=p
if exist "E:\backupp.7z" set v=q
if exist "E:\backupq.7z" set v=r
if exist "E:\backupr.7z" set v=s
if exist "E:\backups.7z" set v=t
if exist "E:\backupt.7z" set v=u
if exist "E:\backupu.7z" set v=v
if exist "E:\backupv.7z" set v=w
if exist "E:\backupw.7z" set v=x
if exist "E:\backupx.7z" set v=y
if exist "E:\backupy.7z" set v=z

It's part of a 7z script I'm making for creating backups, I'll share it once I'm done.
 
If it's for backups, it might be better to use the date and time in the filename. You run out of letters quickly.
 
Dont know of this helps but I used to use this to create a new backup of my Minecraft Server every time it was started ...

rem - create backup folder based on date

for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C
)

set CurrentDate=%DateDay%-%DateMonth%-%DateYear%

md D:\MinecraftBackups\%CurrentDate%

xcopy "D:\Company\MineCraft Server\*.*" /e /s /y D:\MinecraftBackups\%CurrentDate%

cd "D:\Company\MineCraft Server\"
java -Xms1024M -Xmx1024M -jar minecraft_server.jar nogui
 
I've got a date thing, I took it out of the file just so it looks simpler.

Is there a way to tidy up what I've put? Like list a-z, so it scans through them all in a loop?
 
Could do it with a loop and an array but would be a pain in the behind with batch scripting and would end up more messy anyway.

Can you use a .vbs and wscript?
 
Last edited:
I take it you are trying to make it overwrite the oldest backup once a certain number is reached?

If so I would drop Batch and do it in Powershell..

$date = get-date -format dd-MM-yyyy
$dirListing = dir E:\*.7z
$oldestBackup = dir E:\*.7z | sort -property lastwritetime | select -first 1

if ($dirListing.Count -ge "20")
{
write-host "20 or more backups detected.. deleting oldest backup $oldestBackup" -BackgroundColor "RED"
del $oldestBackup -force
}

BackupUtility.exe -destination E:\Backup-$date.7z
 
Ok, never mind.

It works perfectly as it is. Just looks a bit big.


Currently it creates "backup-yyyy-mm-dd a.7z", if it is run a second time on that day, it creates b etc until it reaches z (I'm never going to create over 15 backups in a day)
 
Back
Top Bottom