在批处理脚本中使用破折号和开关

时间:2016-05-31 22:21:44

标签: batch-file

我刚创建了简单的批处理脚本。我想用" -q"等交换机运行uninstall.exe。 " -splash卸载"

这是代码;      @echo off echo This script will uninstall some features. pause SET path=C:\Program Files\Program\uninstall.exe -q -splash Uninstall START "" "%path%" pause

如果我运行此代码,则会出错:

Windows cannot find 'C:\Program Files\Program\uninstall.exe -q -splash Uninstall'  
Make sure you typed the name correctly, and then try again.

如果删除开关,则卸载过程正常启动。

那么如何在批处理文件中使用此swtiches呢?

2 个答案:

答案 0 :(得分:1)

顺便说一句,不要使用path作为变量名的任意选择。它在Windows(以及Unix派生系统)中具有特殊意义。

您的主要问题是您在引用的字符串中包含了开关,然后将其视为可执行文件名整体。仅在文件名周围加上引号,并将开关留在外面:

SET command="C:\Program Files\Program\uninstall.exe" -q -splash Uninstall
START "" %command% 

(引号的唯一原因是路径名包含空格。)

另外,你根本不需要使用变量,但是因为你使用了变量,我已经使用了变量。

答案 1 :(得分:0)

我不太确定您遇到的每个程序是否都会在C:\ Program Files(此处放置程序名称)\目录中有一个等待您的uninstall.exe文件。即使这样,您可能必须从GUI控制它。但是,看一下另一个堆栈溢出线程here,我想将其归功于用户Bali C.和PA。通过使用注册表项找到Windows程序的卸载文件,提出使用批处理文件卸载文件的可能解决方案。我将重新粘贴PA。的代码如下:

@echo off
for /f "tokens=*" %%a in ('reg query hklm\software\Microsoft\Windows\CurrentVersion\Uninstall\ ^| find /I "%*"') do (
  for /f "tokens=1,2,*" %%b in ('reg query "%%a" /v UninstallString ^| find /I "UninstallString"') do (
    if /i %%b==UninstallString (
      echo %%d
    )
  )
)

此代码将从注册表中找到特定程序的卸载文件,然后它将打印出运行卸载文件所需的命令。删除回声'在确定它们是正确的时候运行这些命令。但是,即使这样也可能需要使用程序的卸载GUI。我不认为这会非常低效。除了效率之外,还有其他特殊原因要使用批处理文件吗?

我希望这有帮助!

相关问题