# Solved: .bat directory loop with y/n question for deletion



## indieman (Jul 7, 2010)

What I have is code to identify all non-system profiles in Documents and Settings and then deletes them automatically. 

```
:: Delete Profile folders 
Set _Profiles=d:\test\test
PushD "%_Profiles%"
If /I Not "%CD%"=="%_Profiles%" (Echo.Unable to find %_Profiles%, exiting)&Pause&Goto :EOF
For /F "Delims=" %%I In ('Dir /AD /B ^|Findstr /I /V /C:"Administrator" /C:"All Users" /C:"Default User" /C:"LocalService" /C:"NetworkService" /C:"%UserName%"') Do RD /Q /S "%%I"
PopD
Echo Profiles DELETED
```
but what i need is for the script to prompt for deletion of those directories instead of automatically deleting. I have attempted this but get the error The syntax of the command is incorrect.

```
:: Delete Profile folders 
Set _Profiles=d:\test\test
PushD "%_Profiles%"
If /I Not "%CD%"=="%_Profiles%" (Echo.Unable to find %_Profiles%, exiting)&Pause&Goto :EOF
For /F "Delims=" %%I In ('Dir /AD /B ^|Findstr /I /V /C:"Administrator" /C:"All Users" /C:"Default User" /C:"LocalService" /C:"NetworkService" /C:"%UserName%"') Do 
 
SET /P ANSWER=Do you want to continue (Y/N)?
echo You chose: %ANSWER%
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
goto :no
:yes
echo profile deleted
RD /Q /S "%%I"
PopD
:no
echo You pressed no!
PopD
```
can anyone point out where i am going wrong?

thanks,


----------



## Squashman (Apr 4, 2003)

You are missing some paranthesis for the FOR loop and you can't use GOTO inside a for loop. It breaks the FOR loop. You need to use the CALL command inside FOR loops so that it returns to the FOR loop to continue processing.


----------



## Squashman (Apr 4, 2003)

Just one other thought.
Why bother with asking the question to delete the folder when you could just let the remove directory command take care of prompting you to delete it?


----------



## indieman (Jul 7, 2010)

hi, ill give that a go. i never thought of to be honest.

thanks,


----------



## indieman (Jul 7, 2010)

the following works a treat. I just took the /Q switch off the RD command.

thanks,


```
:: Delete Profile folders 
Set _Profiles=d:\test\test
PushD "%_Profiles%"
If /I Not "%CD%"=="%_Profiles%" (Echo.Unable to find %_Profiles%, exiting)&Pause&Goto :EOF
For /F "Delims=" %%I In ('Dir /AD /B ^|Findstr /I /V /C:"Administrator" /C:"All Users" /C:"Default User" /C:"LocalService" /C:"NetworkService" /C:"%UserName%"') Do RD /S "%%I"
PopD
```


----------



## TheOutcaste (Aug 8, 2007)

If you do need to have a more specific prompt, you can either call a subroutine to ask and then delete if the answer is yes, or enable Delyaed Expansion and do it all within the For loop. You need delayed expansion to be able to test the ANSWER variable inside the loop.
I've also used a different method to verify that you've successfully switched to the folder you specified, and expanded it so it shows on several lines instead of just one per a suggestion by Squashman.
You can also leave off the /Q switch as well, which will require a double confirmation.
Subroutine:

```
:: Delete Profile folders
Set _Profiles=C:\Users
PushD "%_Profiles%"
If ErrorLevel 1 (
  Echo.An error occurred changing to the %_Profiles% folder, Program will exit
  Pause
  Goto :EOF
)
For /F "Delims=" %%I In ('Dir /AD /B ^|Findstr /I /V /C:"Administrator" /C:"All Users" /C:"Default User" /C:"LocalService" /C:"NetworkService" /C:"%UserName%"') Do Call :_Verify "%%I"
PopD
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Verify
Set /P ANSWER=Do you want to Delete the %~1 Profile (Y/N)?
Echo You chose: %ANSWER%
If /I {%ANSWER:~0,1%}=={y} (
  Echo profile %~1 deleted
  RD /Q /S "%~1"
) Else (
  Echo You pressed no!
)
Goto :EOF
```
Use Delayed Expansion:

```
:: Delete Profile folders
SetLocal EnableDelayedExpansion
Set _Profiles=C:\Users
PushD "%_Profiles%"
If ErrorLevel 1 (
  Echo.An error occurred changing to the %_Profiles% folder, Program will exit
  Pause
  Goto :EOF
)
For /F "Delims=" %%I In ('Dir /AD /B ^|Findstr /I /V /C:"Administrator" /C:"All Users" /C:"Default User" /C:"LocalService" /C:"NetworkService" /C:"%UserName%"') Do (
  Set /P ANSWER=Do you want to Delete the %~1 Profile (Y/N)?
  Echo You chose: !ANSWER!
  If /I {!ANSWER:~0,1!}=={y} (
    Echo profile %%I deleted
    RD /Q /S "%%I"
  ) Else (
    Echo You pressed no!
  )
)
PopD
```


----------

