# batch file to delete the oldest folders, please help



## Amino (Nov 18, 2011)

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 (Apr 4, 2003)

```
for /f "skip=2 tokens=*" %%A in ('dir /ad /b /o-d') do rmdir /s /q %%A
```


----------



## Amino (Nov 18, 2011)

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:

```
for /f "skip=2 tokens=*" %%A in ('dir /ad /b /o-d') do rmdir /s /q "%%A"
```


----------



## Amino (Nov 18, 2011)

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

```
script.bat c:\needed_dir
```
 and changed %%A to %%1, but it didn't succeed.


----------



## Squashman (Apr 4, 2003)

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 (Nov 18, 2011)

rmdir doesn't understand relative path and tries to delete found oldest folder in home dir.
now i'm trying to run this:

```
for /f "skip=2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q %%A
```


----------



## Squashman (Apr 4, 2003)

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 (Nov 18, 2011)

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

```
for /f "skip=2 tokens=*" %%A in ('dir %1 /s /ad /b /o-d') do rmdir /s /q "%%A"
```


----------



## Amino (Nov 18, 2011)

managed myself. misused %, now works correctly.


----------



## Squashman (Apr 4, 2003)

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 (Nov 18, 2011)

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:

```
for /f "skip=%2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q "%%A"
```


----------



## Amino (Nov 18, 2011)

again, managed myself. inserted path from %1 to rmdir's %%A:

```
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 (Apr 4, 2003)

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.


```
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 (Apr 4, 2003)

Another issue you are going to have is if some one needs to check a directory path with a space in it.


----------



## mares88 (Dec 12, 2011)

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 :-]


----------

