windows Bat-File:命令只更改文件夹属性而不是递归
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6412316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bat-File: Command to change only folders attributes not recursively
提问by Sergey Vasilenko
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:
我们的网络中有一种病毒,它在 USB 闪存驱动器上设置隐藏和系统的所有根目录属性,并创建 lnk 文件,运行 cmd.exe,病毒本身然后打开目录,因此为了治愈这些驱动器,我使用以下命令:
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).
但是有一个问题:命令“attrib -s -h -r /d /s”递归处理所有文件和目录,如果有很多文件和目录需要太长时间(看起来Windows首先创建完整的文件列表然后开始处理一切)。
Is there a possibility to process only directories NOT files and not recursively with a bat-file?
有没有可能只处理目录而不是文件而不是用 bat 文件递归处理?
Like in perl:
就像在 perl 中一样:
opendir D, '.';
while($_ = readdir D){
if(-d $_){
#do something
}
}
closedir D;
Thank you.
谢谢你。
-- UPD: 2012-01-31, the solution:
-- UPD:2012-01-31,解决方法:
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)
(用 %%i 替换 %i 以在批处理文件中使用)
回答by ixe013
Use the FOR command, with a DIR output as working list.
使用 FOR 命令,将 DIR 输出作为工作列表。
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) :
其中 a、b、c 和 d 是目录,a 有子目录。输入这个命令(如果你在批处理文件中输入,请记住使用 %%):
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.
从您的问题中,我了解到应该避免处理子目录。如果没有,请评论我的答案,我会修复它。
回答by micah
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:
所以有点晚了——但我们刚刚有一个相同病毒的新变种袭击了我们的系统——进入了一个 300 多个文件夹的共享驱动器。清除了所有内容,但为了恢复属性,我执行了以下操作:
open a cmd prompt in the folder and run: dir /A:DHS /B > filename.txt
在文件夹中打开一个 cmd 提示符并运行: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
"/A:DHS" 仅输出 * D* 目录的列表,即 * H*idden 和 * S*系统,然后将 * B*are 信息输出到 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
将该文件中的文件夹名称列表复制到 Excel 列 B. 在 A 列中键入“attrib -H -S”并将其向下拖动以匹配文件夹名称的数量
then in column C put in: =A1 & " " & CHAR(34) & B1 & CHAR(34)
然后在 C 列中输入: =A1 & " " & CHAR(34) & B1 & CHAR(34)
this outputs to: attrib -H -S "folder name"
这输出到: attrib -H -S "文件夹名称"
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.
然后只需向下拖动公式即可获取每个文件夹名称的命令。全选 - 复制 - 粘贴到记事本 - 将文件另存为 .cmd - 将文件放入出现问题的文件夹中并运行它。
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.
这是有效的,因为它没有尝试递归/等它只是运行命令,移动到下一行等等。花了大约 3 秒钟为我修复了 300 多个文件夹。
回答by Philip Kelley
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.
我相信你不能在一个简单的批处理文件中用 attrib 命令做你想做的事。虽然我不熟悉它,但如果这是一个严重的长期问题(病毒 = 我真的希望不是),您可能需要查看 Powershell,因为它可能提供您需要的功能(和更好的可编程性)。