Count folders before running batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DXF
Posts: 3
Joined: 02 May 2013 09:51

Count folders before running batch file

#1 Post by DXF » 24 May 2013 05:11

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Count folders before running batch file

#2 Post by Squashman » 24 May 2013 06:18

Code: Select all

set dircount=0
for /d %%G in (*) do set /a dircount=dircount+1
IF %dircount% GTR 3 robocopy.......

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Count folders before running batch file

#3 Post by foxidrive » 24 May 2013 09:33

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")

Post Reply