# Delete folder with different name in user profiles.



## Ricka182 (Nov 11, 2015)

Greetings! I've found this site very useful, and I figured it was time to sign up and try to help as I can..

But for now, I'm in need of help. I've read two threads with replies from Squashman, both very helpful in what I'm trying to do. Here are both thread links..

https://forums.techguy.org/threads/dos-batch-file-to-delete-certain-folders-based-on-name.1035509/
https://forums.techguy.org/threads/...specific-folder-in-all-user-profiles.1054311/

I'm trying to do something similar to the second link. Here is a breakdown of information..

Accidentally(of course..lol), a folder named TEST was deployed to our environment, mixed XP and Windows 7. The folder has nothing in it, and is showing up on the Start menu, which is causing users to call our Help Desk to ask what it is. So we need to remove it.

The problem is the deployment was set to affect logged in users, and it repeated for an hour before we noticed it. So on affected devices, the folder shows on multiple profiles. Because the Start menu is personalized, the folder path is different for each user, and the folder took on a name reflecting the user profile it was part of...

"TEST(user name)" is what they see...no quotes

So I'm trying to figure out how to use the Squashman code below, if possible, or whatever works to remove the folder. I tired adding in an asterisk after TEST, but I think the quotes make it seem verbose instead of using * as a wildcard...

Any ideas on what I'm not doing right?

*************
IF EXIST "C:\Documents and Settings\" PUSHD "C:\Documents and Settings\"
IF EXIST "C:\Users\" PUSHD "C:\Users\"

FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') do (
IF EXIST "%%G\Start Menu\Programs\TEST(user name)" rmdir /q /s "%%G\Start Menu\Programs\TEST(user name)"

IF EXIST "%%G\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\TEST(user name)" rmdir /q /s "%%G\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\TEST(user name)"
)
**************


----------



## Squashman (Apr 4, 2003)

Ricka182 said:


> https://forums.techguy.org/threads/dos-batch-file-to-delete-certain-folders-based-on-name.1035509/


Re-read posts 9, 10 & 11 from that link.


----------



## foxidrive (Oct 20, 2012)

I went there 

Here's what I modified, based upon Squashman's code and your code.
It should just list the folders to be removed to the console screen atm.


```
@echo off
IF EXIST "C:\Documents and Settings\" PUSHD "C:\Documents and Settings\"
IF EXIST "C:\Users\" PUSHD "C:\Users\"
for /F "delims=" %%I in ('dir /a:d /b /s ^| findstr /I /L /C:"TEST(" ') do echo rd /q /s "%%I"
pause
```


----------



## Ricka182 (Nov 11, 2015)

Thank you both. 

Squashman, that is what I've been focused on, playing with different options, just trying to find the right one.

Foxi, your code did not do anything for me for some reason. If I run it verbatim, it doesn't list anything, even with echo on. If I remove all echo statements, it runs, but does not delete the folder, but also does not error out...still trying... I know there has to be a way...


----------



## foxidrive (Oct 20, 2012)

Ricka182 said:


> Foxi, your code did not do anything for me for some reason. If I run it verbatim, it doesn't list anything, even with echo on.


If you create a folder on your desktop called "TEST(Peter)" then does it list it?


----------



## Ricka182 (Nov 11, 2015)

Hi Foxi...I created a TEST folder on the Desktop and the root of C:, and both were still there as well after I ran the code. I think I know why, but I am doing more testing today. But your code has me closer than I have.. I'm still reading up on some of the commands listed in the thread Squashman and I referenced above so I know why it's working, not just the specific words to make it work..


----------



## foxidrive (Oct 20, 2012)

If it shows you the correct command, then remove the echo statement to enable the command.


----------



## Ricka182 (Nov 11, 2015)

I already removed all echo statements. I'm headed into my lab now to work on it...


----------



## Ricka182 (Nov 11, 2015)

Ok, so here is the code I got to work, sort of:

```
if exist "%USERPROFILE%\..\" pushd "%USERPROFILE%\..\" & goto RemoveDirectories
if exist "C:\Documents and Settings" pushd "C:\Documents and Settings" & goto RemoveDirectories
if exist "C:\Users" pushd "C:\Users" & goto RemoveDirectories
goto :EOF
:RemoveDirectories
for /F "tokens=*" %%G in ('dir /A:D /B /S "Test - (*)" 2^>nul') do rmdir /Q /S "%%~G" 2>n
```
**************************************************************************************************
Results: When I run that from a batch file on the local Desktop, regardless of who is logged in at the time, it works just fine. The folder is removed from all users profiles Start Menu folder.

Problem: I've been told this must be run at night, when no users are logged in. I am trying to fight that, but there is a policy to not deploy anything during the day, even if it is a correction to a previous error like this was. When I push the file to a device with no current user, it does not work, presumably because without a user logged in, the variable for the user profile is not active....

I know there is a way, I hope.. If not, I can use this as backing for a daytime push exception..


----------

