Script to Search and Remove All Hidden Folders

January 2017


I need a Script (VB or BAT : Prefferebly BAT Script) That will search and delete all Files and Folders (Even the folders that contain subfolders and files) which has Hidden Attribute
in a particular drive or folder. That is the script, when run, will search for all files and folders in a drive and will delete all the files and folders which are hidden but will not affect of delete files which are not hidden.

I actually want this for getting rid of some nasty redundant virus which comes via USB drives and memory cards. Usually they don't show up in the explorer and I have to manually delete them from command line by changing their attributes.

The commands:

attrib filename.ext -h -r -s 

then del 

del filename.ext 

What i want actually is getting some script to make the process simpler for ignorant people here in my office who unknowingly infect the computers by running the virus codes. The antivirus does not helps much. I am planning to make an exe out of that batch script and add that in the Right Click Context Menu of Drives and Folders. I don't care if other important files with Hidden Attribute gets deleted in this procedure because Usually the Source are Memory cards and the files that are needed are only Image Files which are not hidden and therefore would not be affected by this procedure. Also if some filter can be created for certain extensions like jpeg,raw etc for not to be deleted, it would be better.

Please help me out.


To Delete whole tree you can use this:

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.gif*') DO RMDIR /S /Q %%G 

DIR /B /AD /S *.gif*
lists all files that are named ".gif"
/B makes the output "bare" with nothing but the file name
/AD only lists directories
/S recurses subdirectories to include their contents if they match the listing criteria

RMDIR /S /Q [path/name]
deletes the directory [path/dir] and all of it's children

FOR /F processes each item (in this case directories) in the set IN ('[command]') by executing the DO [command]

%%G is a parameter, which in this example is a directory name
"tokens=*" says that all the characters output in a line (i.e. by the dir command) are assigned to the parameter %%G

For the Hidden files selection you can refer the Microsoft Command Line Reference (below link) for more on FOR:

In addition these are my tips for Hidden files operation, compile yourself and try:

XCOPY /H copies the files, including the hidden files to a new destination

DEL /A-H deletes non-hidden files from the new folder

DEL /AH deletes hidden files from the original folder


Thanks to Aadhi for this tip on the forum.


Published by jak58. Latest update on November 8, 2009 at 06:12 AM by jak58.
This document, titled "Script to Search and Remove All Hidden Folders," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (