Batch to delete folders with a specific name
adgm1 Posted messages 306 Status Member -
Hello
I'm looking to delete folders that have a specific name. I would like to first search for all folders named "toto", "bibi"... then delete them.
My idea is to use the IF EXIST function, but I don't know how to indicate that it should search in the subfolders as well. Because the folder "toto" can be located in different places within the target directory.
For example, if exist "contact" rmdir "contact", if exist "liens" rmdir "liens" etc...
The goal is to remove certain unnecessary folders in user profiles. I will then run it as a scheduled task every x days so that with each new profile, unnecessary folders like contact, saved games, favorites... disappear.
Thank you
12 answers
You have a way of knowing all the "objects" in the current directory.
Do you know how to check if the object is a folder/directory?
If it's a folder, you check if it's the name or names you're looking for.
If so, you delete it (and its subfolders recursively).
If it's not one of the names sought, you call your script recursively, providing the correct parameter or making the appropriate cd.
I think the find command could do what you want:
find . -name toto -or -name bibi ... -type d -exec rm -rf {}\;
The '.' means the current directory. You can replace it with the desired directory name.
If there are spaces or special characters in the name, enclose it in " ", like "Parties enregistrees".
If you only want to delete empty directories, you could add the -empty option and replace "rm -rf" with "rmdir".
The \ before the ; at the end is necessary.
Hello,
Could you do something like this:
dir /b /s /ad "target directory" > "%TMP%\folder_list.txt" for /f "delims=" %%A in ('findstr /i "toto bibi" "%TMP%\folder_list.txt"') do ( rmdir /s /q "%%~A" ) I didn't realize that it's Autoit, the rmdir misled me.
Indeed, rmdir does not exist in cmd.
@barnabe0057:
This script could probably eliminate folders like "toto" in the current directory, but probably not in one of its subfolders?
I found this:
https://forum.hardware.fr/hfr/Programmation/Shell-Batch/dos-for-imbriquees-sujet_92855_1.htm
but as mentioned, delayed variable expansion is complicated.
It would be less complicated to write another script that checks if the name is in the list and returns a status accordingly.
For example:
for /f "tokens=*" %%p in ('dir /ad /b') do (
; call another script that returns status if file is in a list
; if status 0 (
rd /S /Q %%p
) else (
; recursive call
)
)
Hello,
It is unclear whether it is Autoit or Batch; most often, it's the latter where the RMDIR command exists but is usually summarized as RD.
The /s switch in DIR indeed specifies to search within the subfolders of the "Target Directory"; nothing excludes that the latter is the root of a partition, except that the search will take ages.
The "subtleties" of delayed expansion and nested FOR can often be circumvented by calling a subroutine with CALL, less elegant but easier to write.
Hi, thanks for your reply.
With Barnabe0057's script, I can delete everything except "groups," which gives me an access denied message. In the properties and security of the folder, it says, "You must have read access permission to view the properties...".
I tried adding the following command to my script to give elevated rights, but it doesn't work.
takeown /f "D:\...\groups" /a /r /d o
icacls "D:\...\groups" /setowner "Administrators" /T /C
Any idea on how to allow the deletion of the groups folder?
Hello,
Are you running your script as an administrator, and who "owns" this folder?
About the syntax, are you sure that the takeown syntax is O and not Y? The quotes are not necessary unless there are special characters in the path, why /a instead of assigning the rights to yourself?
Regarding icacls, the user does not have quotes and again, why would you want to appropriate it rather than confer rights through /grant; be careful in any case with the effective application of permissions (the folder itself and subfolders), check the first two lines of examples:
https://ss64.com/nt/icacls.html
This is a shared folder among several users. I run the script from the domain controller so presumably, it's admin yes. In any case, no problem at this level to delete all the other folders.
For takeown, it's O for Yes and not y for Yes. I’m not sure if /a has any importance, but the script needs to execute correctly at regular intervals, so I would say it needs to be system or domain admin.
Here’s what I get when I try the commands to elevate rights:
takeown /f "D:\...\groups" /a /r /d o
Error: the currently logged in user
does not have administrator privileges.
icacls "D:\...\groups" /setowner Administrateurs /T /C
D:\...\groups: Access denied.
D:\...\groups\*: Access denied.
0 files processed successfully; failed to process 2 files.
icacls "D:\...\groups" /grant:r Administrateurs:(OI)(CI)F /T
D:\...\groups: Access denied.
0 files processed successfully; failed to process 1 file.
It's okay after all; during the launch via scheduled task, the commands are executed correctly. A higher permission must therefore be applied compared to manual execution.
On the other hand, the desktop folder keeps coming back whenever the user session is connected, probably because the desktop location needs to be somewhere. I will try to move it to a shared drive.
In principle, on Windows, there is always a Desktop folder associated with each user:
C:\Users\<user_identifier>\Desktop
This is true for the one issuing the command and for those for whom you are cleaning up.</user_identifier>
The idea is more about cleaning up the file explorer to leave only the essentials. And the desktop is not to be manipulated by users in our case. It's the admin who enforces such shortcuts on the desktops. So if the desktop is accessible, we lose the non-customizable aspect of the desktop.
In fact, for the issue with the Desktop, I explained my idea in another post that hasn't received a response here.
But it seems feasible to use GPOs to redirect user desktops to a shared drive so that the admin is the only one managing the desktop icons.
I don't know if this is what you're looking for:
https://www.pcastuces.com/pratique/windows/autorisations_fichier/page2.htm
or in command mode:
https://www.malekal.com/comment-utiliser-icacls-pour-voir-et-modifier-les-autorisations-de-fichiers-et-dossiers/