Bat-File: Command to change only folders attributes not recursively
There is a virus in our network that sets all root directories attributes hidden & system at usb flash drives and creates lnk-files, that run cmd.exe, virus itself and then open directories, so to cure such drives I use the commands:
attrib -s 开发者_开发问答-h -r /d /s
del /q /s *.lnk
rd /q /s recycler
But there is a problem: command "attrib -s -h -r /d /s" processes all files and directories recursively and if there are many of them it takes too long (it looks like Windows first creates full file list and then begins to processes everything).
Is there a possibility to process only directories NOT files and not recursively with a bat-file?
Like in perl:
opendir D, '.';
while($_ = readdir D){
if(-d $_){
#do something
}
}
closedir D;
Thank you.
-- UPD: 2012-01-31, the solution:
for /f "delims=" %i in ('dir /ad /ah /b') do @attrib -r -s -h -a "%i"
(replace %i with %%i to use in batch files)
Use the FOR command, with a DIR output as working list.
For example you start with this :
attrib /s /d
SHR C:\a\a.1
SHR C:\a\a.2
SHR C:\a\a.3
SHR C:\a
SHR C:\b
SHR C:\c
SHR C:\d
A C:\x.txt
A C:\y.txt
A C:\z.txt
Where a, b, c and d are directories, and a has subdirectories. Type this command (remember to use %% if you put in in a batch file) :
for /f %i in ('dir /ad /ah /b') do @attrib -r -s -h %i
Which will give you what you want :
attrib /s /d
SHR C:\a\a.1
SHR C:\a\a.2
SHR C:\a\a.3
C:\a
C:\b
C:\c
C:\d
A C:\x.txt
A C:\y.txt
A C:\z.txt
From your question, I understand that processing subdirectories should be avoided. If not, comment on my answer and I'll fix it.
So a bit late- but we just had a new variant of the same virus hit our systems - got into a 300+ folder shared drive. got everything cleaned out but to get the attributes back I did the following:
open a cmd prompt in the folder and run: dir /A:DHS /B > filename.txt
the "/A:DHS" outputs the list of only *D*irectory's, that are *H*idden and *S*ystem, then outputs *B*are info to filename.txt
copied the list of folder names from that file into excel column B. in column A type in "attrib -H -S" and drag it down to match number of folder names
then in column C put in: =A1 & " " & CHAR(34) & B1 & CHAR(34)
this outputs to: attrib -H -S "folder name"
then just drag the formula down to get the command for each foldername. select them all - copy - paste into notepad - save the file as a .cmd - put the file into the folder where you had the issue and run it.
This works becuase it's not trying to do anything recursively/etc it just runs the command, moves to the next line and so on. took about 3 seconds to fix over 300 folders for me.
I believe that you can't do what you'd like with the attrib command in a simple batch file. While I am not familiar with it, if this is a serious and long-term problem (virus = I really hope not), you might want to look into Powershell, as it may provide the functionality (and better programmability) that you need.
精彩评论