# Solved: DOS delete all folders that begin with...



## camaroer87 (Nov 10, 2007)

How can I use wildcards in folder names for a DOS batch file? I need to clean out a folder full of user names that are the same but some have additions on the end.

An example: (the names listed are folders)

-Profiles
---derek
---derek.02
---derekLP
---mike
---mikeLP
---karenLP
---karen02

Thanks!


----------



## Squashman (Apr 4, 2003)

You didn't state which ones you wanted removed.


----------



## camaroer87 (Nov 10, 2007)

Oh, it would be what ever name I enter at a prompt. If I put in derek, I want all the folders that start with derek in them to be deleted.

Thanks


----------



## Squashman (Apr 4, 2003)

```
for /f "tokens=*" %%F in ('dir /ad /b derek*') do rmdir /s /q %%F
```


----------



## camaroer87 (Nov 10, 2007)

Can someone explain the above line to me, I have to make it fit my batch file and I would like to understand what its doing instead of just copy and pasting things. Thanks!
If it helps any here is the batch program I have almost ready:

@echo off

goto end_comment
This file will remove all the user folders on the Citrix servers.

How to use:

After starting the program...

1. Find out how many Citrix servers there are, enter it
2. Find the name of the user's folder by checking what it is currently named on a Citrix server, enter it

Press enter and let the program remove the folders.

If the folder does not exist, it throws up an error stating this, but keeps on going until the end.

:end_comment

setLocal EnableDelayedExpansion

set /p max_servers=How many servers are there: 
set /p username=Enter a username: 

:loop
set /a current_server+=1
if !current_server! gtr !max_servers! goto end
::rd "\\CX%current_server%\C$\Documents and Settings\%username%" /s /q
for /f "tokens=*" %AF in ('dir /ad /b username*') do rmdir /s /q %%F
goto :loop

:end
exit


----------



## Squashman (Apr 4, 2003)

The for loop gets a list of folders and parses the output of the DIR command one line at a time. You can't use Wildcards with removing directories so you have to use the For Loop to get a list of directories that match the username.

When you post code on the forums could you please surround the code in CODE tags. It is hard to tell where your code starts and stops.

You don't need to use Delayed expansion for your Current_Server and Mas_Servers variables. Never use Delayed Expansion if you don't have to.

Do not use %username% for your own variable. Username is an environmental variable and you could end up deleting your own profile if you use that variable. It is Good Practice to lead your Variable names with an underscore if you want to use a variable name like that.


```
set /p _username=Enter a username:
```
You will need to add the server path to the RMDIR command or use the PUSHD command to make it the current working directory.

I would not put comments in your batch file like that. either use the REM command or use double colons.

It also good coding practice to initialize your variables before they are used. You may end up with undesirable results without realizing it.


```
@echo off

goto end_comment
:: This file will remove all the user folders on the Citrix servers.

:: How to use:

:: After starting the program...

:: 1. Find out how many Citrix servers there are, enter it
:: 2. Find the name of the user's folder by checking what it is currently named on a Citrix server, enter it

:: Press enter and let the program remove the folders.

:: If the folder does not exist, it throws up an error stating this, but keeps on going until the end.

:end_comment

set /p max_servers=How many servers are there:
set /p _username=Enter a username:
set current_server=0

:loop
set /a current_server+=1
if %current_server% gtr %max_servers% goto :EOF
pushd "\\CX%current_server%\C$\Documents and Settings"
for /f "tokens=*" %%F in ('dir /ad /b "%_username%"*') do rmdir /s /q "%%F"
popd
goto :loop
```


----------



## camaroer87 (Nov 10, 2007)

Thank you Squashman the code worked perfectly!

Also thanks for the coding tips. This is my first batch file, so any tips are greatly appreciated.

What is the syntax for surrounding code in tags? CODE/ or what...

Thanks again for your help!


----------



## Squashman (Apr 4, 2003)

Click the Advanced Tab when posting. You will see all the BB tags you can use. If you use the Quick Reply you don't get all the fancy features.


----------



## camaroer87 (Nov 10, 2007)

```
Code Test...
```
Thanks!


----------

