batch file to delete the oldest folders, please help

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Amino

Thread Starter
Joined
Nov 18, 2011
Messages
8
hi. i need a batch file that can check if there are more than 2 folders in specified directory and if so, delete only the oldest one and check again. the task i'm trying to solve is keep at least 2 backups, even if they are obsolete.
so far i've seen examples that remove only files older than x days, but that's not exactly the case here. thanks in advance.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Code:
for /f "skip=2 tokens=*" %%A in ('dir /ad /b /o-d') do rmdir /s /q %%A
 

Amino

Thread Starter
Joined
Nov 18, 2011
Messages
8
Thank you very much, it does exactly what i need. while testing i found little problem with folder names containing spaces but easily corrected by adding double quotes to the last variable like this:
Code:
for /f "skip=2 tokens=*" %%A in ('dir /ad /b /o-d') do rmdir /s /q "%%A"
 

Amino

Thread Starter
Joined
Nov 18, 2011
Messages
8
I found problem passing the parameter with specified directory in which i want this script to run. y default it runs in user homedir and using CD or CHDIR is not an option. is it possible without those commands? i tried calling it with parameter like
Code:
script.bat c:\needed_dir
and changed %%A to %%1, but it didn't succeed.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Two options.
1. Use PUSHD before the For Loop
2. Change the DIR command in the For loop to use the command line input.
 

Amino

Thread Starter
Joined
Nov 18, 2011
Messages
8
rmdir doesn't understand relative path and tries to delete found oldest folder in home dir.
now i'm trying to run this:
Code:
for /f "skip=2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q %%A
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
You need to use PUSHD before the for loop.

If you were using the /S switch with the DIR command and traversing sub directories then you wouldn't have to use pushd.
 

Amino

Thread Starter
Joined
Nov 18, 2011
Messages
8
now my boss wants to pass number of skipped folders as a parameter along with target directory. i tried using SET but did not succeed. how to do it correctly?
current version is
Code:
for /f "skip=2 tokens=*" %%A in ('dir %1 /s /ad /b /o-d') do rmdir /s /q "%%A"
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
If your problem is solved it is common courtesy to post your final code and then use the Thread Tools to mark your problem solved.
 

Amino

Thread Starter
Joined
Nov 18, 2011
Messages
8
found a problem with subfolders. if they exist inside folders i want to delete they are deleted instead of folders containing them. i figured this is from the key /s in dir command, but without it rmdir somehow doesn't get full path, only correct folder name and tries to delete this folder from where the script is run, which is obviously not there. and i'm NOT allowed to use CD. how to pass full path to rmdir instead of only name?
current version:
Code:
for /f "skip=%2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q "%%A"
 

Amino

Thread Starter
Joined
Nov 18, 2011
Messages
8
again, managed myself. inserted path from %1 to rmdir's %%A:
Code:
for /f "skip=%2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q "%1\%%A"
script is safe to run without any number of parameters so misclick will not destroy your data.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Like I said earlier. Use the PUSHD command before your FOR loop.

While I guess you could technically say the script is safe to run without parameters I never rely on that thought process. I make my scripts as bullet proof as possible.

Code:
If "%~1"=="" Goto _Usage
If "%~2"=="" Goto _Usage
for /f "skip=%2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q "%1\%%A"
:_Usage
Echo.This batch file requires two command line parameters.
Echo.The first parameter is the directory path.
Echo.The second parameter is the number of directories to skip
I would even do a test on the second parameter to make sure it is a number and not the directory path in case they put one before the other. A lot of programming is about validating the user input.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Another issue you are going to have is if some one needs to check a directory path with a space in it.
 
Joined
Dec 12, 2011
Messages
1
Hi can anyone help me with similar script / with modification of this one ? It is supposed to do the same {delete the oldest folder } but I need 5 folders to be kept ... not sure why but i cant figure out how to :-]
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top