# Find folder and delete



## mrtep (Mar 5, 2014)

Hello. I am new to this forum. I would like some help on creating a batch file for Windows 7. I have a computer lab in a high school with about 30 computers that several users use each computer everyday. Some of the users have been copying a game to their desktop. I need to find that folder that could be on the any of the users desktop. I would like to find the folder and files and delete them, then create a log file of each user that the folder and files were found and deleted. I will be doing this remotely using PSEXEC. I would really appreciate any help. Thanks... Tim


----------



## foxidrive (Oct 20, 2012)

Do we know what the folder is called, or what files are involved?


----------



## mrtep (Mar 5, 2014)

Thank you for your response! Yes it is the game Halo. Once they play the game it stores other files and saved games.


----------



## foxidrive (Oct 20, 2012)

The folder may or may not be called Halo, right?
What files need to be located inside a folder, before you want the folder deleted?


----------



## mrtep (Mar 5, 2014)

Most of the time it is called Halo, it could be called something else but all the files pertain to Halo. I did find one the student named School Work! Hopefully this will not be TOO complicated! Sometimes some of the files are in the appdata folder.


----------



## foxidrive (Oct 20, 2012)

Which files need to be located inside a folder, before you want the folder deleted?
I don't have Halo here so you have to provide the info.

The student could use a folder with actual schoolwork inside it, so deleting it will destroy anything else that is inside it. It could be the "documents" folder for example.


----------



## mrtep (Mar 5, 2014)

I guess then any folder just on the desktop containing files for Halo be deleted. The students have a network drive they are supposed to save any files for school work so it is OK to delete anything on the desktop with Halo in it. Thanks!!


----------



## foxidrive (Oct 20, 2012)

1) Which files need to be located inside a folder, before you want the folder deleted?
I don't have Halo here so you have to provide the info.

2) will you be running the batch file using PSEXEC on the remote machines?


----------



## mrtep (Mar 5, 2014)

I will be using PSEXEC. I already have that ready. I just need the script file to load. Can it be that if it finds any one file named Halo that it will delete the entire folder and sub folders? I don't know all the file names. Plus there are sub folders inside the folder they copy to their desktop all pertaining to Halo.


----------



## foxidrive (Oct 20, 2012)

EDITED: to avoid removing the desktop folder if halo.exe is on the desktop. It will just report the fact.

Test this - it will report the folders that contain halo.exe below the desktop folders, and 
merely echo the rd command to remove it.

Remove the echo before rd to make it actually perform the folder removal if it is all ok.


```
@echo off
for /d /r "%systemdrive%\users" %%a in (deskto?) do (
    for /f "delims=" %%b in (' dir /b /s /a-d "%%a\halo.exe" 2^>nul ') do (
        echo found "%%b" on "%computername%"
        if /i "%%a\"=="%%~dpb" echo found halo.exe on the desktop at "%%~dpb"
        echo if /i not "%%a\"=="%%~dpb" rd /q/s "%%~dpb"
    )
)
pause
```


----------



## mrtep (Mar 5, 2014)

Thank you very much! I will be able to fully test it on Monday. One question? If the batch file should find Halo.exe alone on the desktop will it remove the Desktop folder? Thanks again!


----------



## foxidrive (Oct 20, 2012)

Check my edited post above.

if there is a folder such as \users\fred\desktop\maths\desktop with halo.exe inside it then that will also just be reported. So it now checks for a folder called *desktop* with halo.exe inside and will only report on that folder.


----------



## mrtep (Mar 5, 2014)

I think this will work for me! You are awesome! If I could just ask one more thing? Could you please have it create a report log of where it found and removed the Halo directories and save that to my network drive so I can follow up with the students? Thank you so much for your help!!


----------



## Squashman (Apr 4, 2003)

You should probably look at setting up some Group Policies to restrict what programs can run on the computers. You should also have all the students and parents sign an acceptable computer use policy.


----------



## mrtep (Mar 5, 2014)

Thanks Foxidrive. I figured out the log file. I do appreciate you doing this!!


----------



## mrtep (Mar 5, 2014)

Hey Squashman. All the group policies are handled by our district IT director and there is a ton of group policies in place. Everybody has to sign a technology agreement. You know kids. They will try most anything. I will mention this game to them.


----------



## foxidrive (Oct 20, 2012)

I pity the poor kids. No more halo fixes. *grins*

Glad to have helped. I was having browser issues here and thought the site was down, which is why I didn't reply.


----------



## mrtep (Mar 5, 2014)

Darn browsers! Yeah the poor kids! Maybe they will get some school work done!!  The script works great. Only found it on 15 out of 32 computers!! Anyway thanks again for your help!!


----------

