VBScript传递包含空格的参数

时间:2019-06-13 14:03:46

标签: vbscript

我绝对是VBScript的第3方脚本,它基本上将Portable Chrome浏览器设置为默认系统浏览器。

但是,如果要打开的文件的名称中包含空格(例如My HTML File.html),则会将其打开为几个单独的虚空文件,并显示有关“无法打开文件”的错误(当然,不存在-应该是一个文件)。

我认为“ Classes \ ChromeHTML2 \ shell \ open \ command \”行中的“%1”需要一些内容,可能还有一些额外的引号,但无论我如何尝试都没有成功:“”%1”也不是“”“”%1“”“”

'Registers Google Chrome Portable with Default Programs or Default Apps in Windows
'chromeportable.vbs - created on May 20, 2019 by Ramesh Srinivasan, Winhelponline.com

Option Explicit
Dim sAction, sAppPath, oFSO, sbaseKey, sbaseKey2
Dim WshShell : Set WshShell = CreateObject("WScript.Shell") 
Dim oFS0 : Set oFSO = CreateObject("Scripting.FileSystemObject")

sAppPath = WshShell.CurrentDirectory & "\GoogleChromePortable.exe"
If Not oFSO.FileExists (sAppPath) Then WScript.Quit
If InStr(" ", sAppPath) <> 0 Then sAppPath = """" & sAppPath & """"

If WScript.Arguments.Count <> 0 Then
   If UCase(Trim(WScript.Arguments(0))) = "-REG" Then Call RegisterChromePortable(sAppPath)
   If UCase(Trim(WScript.Arguments(0))) = "-UNREG" Then Call UnregisterChromePortable(sAppPath)
Else
   sAction = InputBox("Type REGISTER to add Chrome Portable to Default Apps. Type UNREGISTER to remove.", "Chrome Portable Registration", "REGISTER")
   If UCase(Trim(sAction)) = "REGISTER" Then Call RegisterChromePortable(sAppPath)
   If UCase(Trim(sAction)) = "UNREGISTER" Then Call UnregisterChromePortable(sAppPath)
End If

Sub RegisterChromePortable(sAppPath)
   sbaseKey = "HKCU\Software\"
   sbaseKey2 = sbaseKey & "Clients\StartmenuInternet\Google Chrome Portable\"

   WshShell.RegWrite sbaseKey & "RegisteredApplications\Google Chrome Portable", "Software\Clients\StartMenuInternet\Google Chrome Portable\Capabilities", "REG_SZ"
   WshShell.RegWrite sbaseKey & "Classes\ChromeHTML2\", "Chrome HTML Document", "REG_SZ"
   WshShell.RegWrite sbaseKey & "Classes\ChromeHTML2\AppUserModelId", "Chrome Portable", "REG_SZ"
   WshShell.RegWrite sbaseKey & "Classes\ChromeHTML2\ApplicationIcon", sAppPath & ",0", "REG_SZ"
   WshShell.RegWrite sbaseKey & "Classes\ChromeHTML2\ApplicationName", "Google Chrome Portable Edition", "REG_SZ"
   WshShell.RegWrite sbaseKey & "Classes\ChromeHTML2\ApplicationDescription", "Access the internet", "REG_SZ"
   WshShell.RegWrite sbaseKey & "Classes\ChromeHTML2\ApplicationCompany", "Google Inc.", "REG_SZ"
   WshShell.RegWrite sbaseKey & "Classes\ChromeHTML2\DefaultIcon\", sAppPath & ",0", "REG_SZ"
   WshShell.RegWrite sbaseKey & "Classes\ChromeHTML2\shell\open\command\", sAppPath & " -- " & "%1", "REG_SZ"

   WshShell.RegWrite sbaseKey2, "Google Chrome Portable Edition", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\ApplicationDescription", "Google Chrome Portable Edition", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\ApplicationIcon", sAppPath & ",0", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\ApplicationName", "Google Chrome Portable Edition", "REG_SZ"  

   WshShell.RegWrite sbaseKey2 & "Capabilities\FileAssociations\.htm", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\FileAssociations\.html", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\FileAssociations\.shtml", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\FileAssociations\.xht", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\FileAssociations\.xhtml", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\FileAssociations\.webp", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\URLAssociations\ftp", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\URLAssociations\http", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "Capabilities\URLAssociations\https", "ChromeHTML2", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "DefaultIcon\", sAppPath & ",0", "REG_SZ"
   WshShell.RegWrite sbaseKey2 & "shell\open\command\", sAppPath, "REG_SZ"

   'Launch Default Apps after registering Chrome Portable   
   WshShell.Run "control /name Microsoft.DefaultPrograms /page pageDefaultProgram"  
End Sub


Sub UnregisterChromePortable(sAppPath)
   sbaseKey = "HKCU\Software\"
   sbaseKey2 = "HKCU\Software\Clients\StartmenuInternet\Google Chrome Portable"   

   On Error Resume Next
   WshShell.RegDelete sbaseKey & "RegisteredApplications\Google Chrome Portable"
   On Error GoTo 0

   WshShell.Run "reg.exe delete " & sbaseKey & "Classes\ChromeHTML2" & " /f", 0
   WshShell.Run "reg.exe delete " & sbaseKey2 & " /f", 0

   'Launch Default Apps after unregistering Chrome Portable   
   WshShell.Run "control /name Microsoft.DefaultPrograms /page pageDefaultProgram"   
End Sub

0 个答案:

没有答案