【问题标题】:Batch script to check if directories exist检查目录是否存在的批处理脚本
【发布时间】:2015-08-21 01:39:31
【问题描述】:

我有一个执行 Windows 系统分析的批处理脚本。但是为了收集浏览器信息,我必须检查是否安装了浏览器。所有浏览器的路径取决于系统架构(Program Files 或 Program Files (x86))。所以我还要检查系统是 32 位还是 64 位,并将路径存储在变量 %program_files% 中。但是,虽然我的电脑中有谷歌浏览器,但程序会打印一个错误:“Chrome 似乎没有安装”。 下面是描述的代码:

Setlocal EnabledDelayedExpansion
Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
REG.exe Query %RegQry% > %usbpath%CheckOS.txt
Find /i "x86" < %usbpath%CheckOS.txt >   %usbpath%StringCheck.txt

IF %ERRORLEVEL% == 0 (
     SET PROGRAM_FILES=%PROGRAMFILES%
) ELSE (
      SET "PROGRAM_FILES= %PROGRAMFILES(x86)%"
)

ECHO Check Google Chrome installation
IF NOT EXIST "!PROGRAM_FILES!\Google\Chrome\" GOTO SKIPG_CHROME

ECHO Running Google ChromeCacheView

:SKIPG_CHROME
ECHO Google Chrome doesn't seem to be installed. ChromeCacheView skipped.

【问题讨论】:

  • 不应该IF NOT EXIST "!PROGRAM_FILES!\... 行实际上是% 而不是!?所以IF NOT EXIST "%PROGRAM_FILES%\....
  • 我试过了,但由于路径 Program Files (x86) 的括号,它不起作用
  • 你少了一个“%”,SET "PROGRAM_FILES= %PROGRAMFILES(x86)%"
  • 我的错...对不起!我忘了把它写下来。我的代码中有它。不是这样的!
  • 无效的Setlocal参数,Enabled应该是Enable

标签: batch-file


【解决方案1】:

从App Paths 注册表项获取应用程序路径对于确定 Chrome 可能位置的任务要好得多。

但这里有一个简单的批处理解决方案,可以避免您犯的所有打字错误:

@echo off
goto GetProgFilesDir

rem If the environment variable "ProgramFiles(x86)" exists with a non
rem empty value, this batch file is running on Windows x64 with a
rem separate standard program files directory for x86 applications.
rem Otherwise the batch file is running on Windows x86 with just
rem one standard program files directory for all applications.

:GetProgFilesDir
if "%ProgramFiles(x86)%" == "" (
    set "PROGRAM_FILES=%ProgramFiles%"
) else (
    set "PROGRAM_FILES=%ProgramFiles(x86)%"
)

echo Check Google Chrome installation
if not exist "%PROGRAM_FILES%\Google\Chrome" goto SKIPG_CHROME

echo Google Chrome is installed.
goto :EOF

:SKIPG_CHROME
echo Google Chrome doesn't seem to be installed.

我不建议通过评估处理器架构来确定 Windows 的版本。我有一台带有 x64 处理器的机器,但安装和运行的是 Windows x86,因此虽然处理器是 64 位,但没有目录 Program Files (x86)。

【讨论】:

    【解决方案2】:

    %ERRORLEVEL% 如果语句为真(即您的操作系统基于 x86),则返回 0。所以现在我知道为什么批处理一直显示 Google Chrome 似乎没有......

    通过浏览您的脚本,您似乎正在尝试检测操作系统。 systeminfo 和 find /i 的组合会更容易。

    替换整个

    Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
    REG.exe Query %RegQry% > %usbpath%CheckOS.txt
    Find /i "x86" < %usbpath%CheckOS.txt >   %usbpath%StringCheck.txt
    

    有

    systeminfo | find /i "x64-based" > %usbpath%StringCheck.txt
    

    【讨论】:

    • 我认为,对于这项任务,使用 systeminfo 不是一个好主意。在我当前正在写此评论的计算机上,此命令当前退出,loading network card information ... error provider load failure 输出作为最后一条消息。因此,根本无法确定安装的 Windows 是 x86 还是 x64。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2015-07-09
    • 2011-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多