如何使用Windows批处理命令一次在c,d,e驱动器的所有目录中搜索文件

时间:2015-08-03 09:57:17

标签: windows batch-file cmd

手动,我正在尝试在PC中搜索病毒。作为其中的一部分,我编写了一个Windows批处理脚本,如下所示。

@echo off

.....

:start

IF EXIST pathoengs.exe goto infected

IF EXIST pathoengs.bat goto infected

IF NOT EXIST pathoengs.bat goto statistics

IF NOT EXIST pathoengs.exe goto statistics

set Pathname="C:\"

CD %Pathname%

:infected

echo A Virus has been detected!

set /p n=Delete?(y/n)

If %n% == y del pathoengs.bat del pathoengs.exe

If %n% == n goto exit

goto start

:statistics

echo No virus

:exit

exit

发出我面临的问题,我的脚本无法检测到C:\驱动器或C:\驱动器的子文件夹中存在的文件。

如果我在路径中搜索文件,如C:\ Users \ xxx \ Desktop而不是CD C:\并将其写入" CD C:\ Users \ xxx \ Desktop"然后在脚本中搜索&按预期工作正常。我不明白,如果我尝试在CD驱动器中搜索CD C:\,为什么会失败。我在这里做错了什么,或者搜索代码本身的脚本是错误的。可能是搜索所有驱动器的方式。它的子文件夹不同,请告诉我你的建议。

实际上,我想搜索所有驱动器中的文件(如c,d,e ..)&它的子文件夹。如果文件存在,那么病毒就在那里。因此,用户将去&删除文件,继续相应。否则,不存在病毒。

如果您有任何想法,请告诉我如何使用Windows批处理脚本进行此类搜索。请只做那些需要的。

如果您需要我的更多信息,请告诉我。

感谢。

2 个答案:

答案 0 :(得分:0)

set /p n=Delete?(y/n)

If %n% == y del pathoengs.bat del pathoengs.exe

If %n% == n goto exit

你没有在两个&命令之间加del,脚本应该是这样的:

choice /c:yn /m "Delete?(y/n)"

If %errorlevel% == 1 del pathoengs.bat & del pathoengs.exe

If %errorlevel% == 2 exit /b

使用choice /c更好,并使用exit /b退出。

答案 1 :(得分:0)

这应该会为您提供一个文件,其中包含许多驱动器上的所有文件名,然后您可以阅读这些文件以执行您需要执行的操作。

它不会避免检查DVD驱动器等,而且它很慢,因为它必须检查每个驱动器的每个部分。

@echo off
(
   for %%a in ( c d e f g h i j k l ) do (
      if exist "%%a:\" dir "%%a:\pathoengs.exe" "%%a:\pathoengs.bat" /b /s /a-d
   )
)>"%temp%\virus list.txt"