【发布时间】:2020-07-09 07:07:50
【问题描述】:
我第一次使用 VBScript 来完成我的一项任务。正在尝试构建 Windows 安装程序。
在将所有内容导入应用程序文件夹之前,想在外部尝试一下,但没有成功。我想以提升的权限执行安装。请更正我的脚本。
问题:如果发送到批处理文件的参数包含空格,则 参数被截断。
我的 VBScript 代码:
' Get target folder path from "CustomActionData" property.
dim targetDirectory
targetDirectory = "D:\New folder\batch files\"
' Prepare file path of install batch file.
batchFilePath = targetDirectory & "install-v2.bat"
' Pass targetDirectory as argument to batch file.
' Run the install batch file with elevated permissions as administrator
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute batchFilePath, targetDirectory, , "runas", 0
我的批处理文件:
@echo off
set HEADER=[MY-APP-NAME] %DATE% %TIME%
set TARGET_DIRECTORY=%1
set LOG_LOCATION="C:\Users\PureAjax\Downloads\batch-experiments\log.txt"
echo %HEADER% -- Instalation process started -- >> %LOG_LOCATION%
echo %HEADER% Target Directory %TARGET_DIRECTORY% >> %LOG_LOCATION%
rem will be using TARGET_DIRECTORY to achieve my task
echo %HEADER% -- Instalation process finished -- >> %LOG_LOCATION%
@pause
日志文件
[MY-APP-NAME] 28-03-2020 23.07.15.78 -- Instalation process started --
[MY-APP-NAME] 28-03-2020 23.07.15.78 Target Directory D:\Newfolder\batchfiles\
[MY-APP-NAME] 28-03-2020 23.07.15.78 -- Instalation process finished --
[MY-APP-NAME] 28-03-2020 23.09.13.66 -- Instalation process started --
[MY-APP-NAME] 28-03-2020 23.09.13.66 Target Directory D:\New
[MY-APP-NAME] 28-03-2020 23.09.13.66 -- Instalation process finished --
我可以在日志文件中看到,如果路径不包含空格,那么批处理文件会收到完整路径,否则会被截断。 或者,有没有办法在构建 MSI 安装程序时直接将参数传递给批处理文件?
尝试了以下解决方案,但没有奏效
arguments = Chr(34) & targetDirectory & Chr(34)和传递参数到批处理文件ObjShell.ShellExecute "cmd", batchFilePath, arguments, "runas", 0
【问题讨论】:
-
在 stackoverflow 中发现了几个类似的问题,但我遗漏了一些东西。
-
也试过这个 ObjShell.ShellExecute "cscript", batchFilePath, arguments, "runas", 0
标签: batch-file vbscript windows-installer