Script to Search and Remove All Hidden Folders

December 2016




Issue



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.

Solution


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:

http://technet.microsoft.com/en-au/library/bb491071.aspx

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

Note


Thanks to Aadhi for this tip on the forum.

Related :

This document entitled « Script to Search and Remove All Hidden Folders » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.