Hello
I have problem that I wonder if anyone of you can help me with. I got a program that sends back-up of configuration-files to a HDD. Every time the backup runs it creat a new folder with a uniq name. The program runs every week.
I´d like to delete old backups, but always keep at least the 3 latest.
I´ve created a batch-file that uses robycopy to move all files older than 30 days to a folder, and then delete the folder. This will be good enough, as long if the backup never fails. If there isn´t a backup done the latest 30 days, then all my backup folders will be deleted.
Is there by any chance someone that know how to run a batch after a folder count?
Maybe with the command "IF" and "GoTo"?
Something like IF "Folder count>3" do "myrobocopy script"
The robocopy script is done and working
Regards
DXF
Count folders before running batch file
Moderator: DosItHelp
Re: Count folders before running batch file
Code: Select all
set dircount=0
for /d %%G in (*) do set /a dircount=dircount+1
IF %dircount% GTR 3 robocopy.......
Re: Count folders before running batch file
DXF wrote:Every time the backup runs it creat a new folder with a uniq name. The program runs every week.
I´d like to delete old backups, but always keep at least the 3 latest.
This is the task, right?
Try this: Change the "d:\backup folder\" to be the folder with the backup folders inside it.
It only echos the RD command to the console and sorts the folders by creation date. It skips the most recent 3, so you will always have those 3 folders.
If the correct folders are being skipped (when echoed to the screen) then you can remove the 'echo' to enable the RD command, and allow it to delete the excess folders.
Code: Select all
@echo off
cd /d "d:\backup folder\" && (for /f "skip=3 delims=" %%a in ('dir /ad /b /o:-d') do echo rd /s /q "%%a")