【发布时间】:2011-09-18 15:32:31
【问题描述】:
我使用此批处理文件的目标是快速找出用户安装了多少防病毒应用程序。我打算通过使用两个 for 循环来做到这一点:
- 外循环:遍历 Program Files 目录中的文件夹名称
- 内循环:遍历常用防病毒名称列表(AVList 变量),查找当前目录名称的匹配项并将每个匹配项附加到 AntiVirus 变量。
话虽如此,这是我失败的代码。我得到的错误是 "" 此时是意外的。。请注意,我目前在 AVList 变量中的 Program Files 中有文件夹名称(用于测试目的)。
::@echo off
::variables
set AntiVirus="Initial Value"
set AVList=(adobe ccleaner auslogics)
SETLOCAL EnableDelayedExpansion
echo Checking Program Files...
if "%Processor_Architecture%" == "AMD64" (
echo 64-bit OS
echo.
for /d %%f in ("%ProgramFiles(x86)%\*") do (
echo "%%f"
SET "folder=%%f"
REM Begin loop to search substrings with words in AVList
Call:SearchAV "%folder%"
)
)
else echo 32-bit OS
echo.
echo AntiVirus: %AntiVirus%
echo.
for /d %%g in ("%ProgramFiles%\*") do (
echo "%%g"
SET "folder=%%g"
REM Begin loop to search substrings with words in AVList
Call:SearchAV "%folder%"
)
)
:SearchAV
for %%v in ("%AVList%") do (
echo "%%v"
SET "av=%%v"
if /I NOT "!~1:av=!"=="!~1!" set AntiVirus="%AntiVirus%%av%"
)
GOTO:EOF
echo.
echo.
echo %AntiVirus% found
echo.
echo Script created by Matthew Ammann, revised by Andriy M from StackOverflow
@pause
这是重定向到日志文件的输出:
C:\AVFinder>set AntiVirus="Initial Value"
C:\AVFinder>set AVList=(adobe ccleaner auslogics)
C:\AVFinder>SETLOCAL EnableDelayedExpansion
C:\AVFinder>echo Checking Program Files...
Checking Program Files...
C:\AVFinder>if "AMD64" == "AMD64" (
echo 64-bit OS
echo.
for / %f in ("C:\Program Files (x86)\*") do (
echo "%f"
SET "folder=%f"
REM Begin loop to search substrings with words in AVList
Call:SearchAV "C:\Program Files (x86)\Adobe"
)
)
64-bit OS
C:\AVFinder>(
echo "C:\Program Files (x86)\Adobe"
SET "folder=C:\Program Files (x86)\Adobe"
REM Begin loop to search substrings with words in AVList
Call:SearchAV "C:\Program Files (x86)\Adobe"
)
"C:\Program Files (x86)\Adobe"
"" was unexpected at this time.
C:\AVFinder> if /I NOT "!~1:av=!"=="!~1!" set AntiVirus=""Initial Value""(adobe ccleaner auslogics)""
我哪里错了?
更新:我终于找到了一些时间来解决这个问题。这是更新的代码:
::This script is licensed under the Creative Commons Attribution license (CC BY 3.0)
::Simply mention the original author in the source code if you make a derivative work.
@echo off
::variables
set AntiVirus=
set AVList=(norton mcafee kaspersky symantec avg comodo avast avira webroot eTRUST)
SETLOCAL EnableDelayedExpansion
echo Checking Program Files...
if "%Processor_Architecture%" == "AMD64" (
echo 64-bit OS
echo.
for /d %%f in ("%ProgramFiles(x86)%\*") do (
echo "%%f"
SET "path=%%f"
Call:SearchAV "!path!"
)
) else echo 32-bit OS
for /d %%g in ("%ProgramFiles%\*") do (
echo "%%g"
SET "path=%%g"
Call:SearchAV "!path!"
)
)
goto :END
:SearchAV
FOR %%a in %AVLIST% do (
set res="%~n1"
set res=!res:%%a=!
if NOT "%~n1" ==!res! (
ECHO "%~n1" contains %%a
if [!AntiVirus!] == [] (
set AntiVirus="%~n1"
) else (
set AntiVirus=!AntiVirus!, "%~n1"
)
)
)
goto :eof
:END
echo.
echo.
echo !AntiVirus! found
echo.
echo Script created by Matthew Ammann, revised by members of Stack Overflow
echo
@pause
【问题讨论】:
标签: function loops batch-file dos