Steve
Steve

Reputation: 53

vbs/batch folder name with spacing issue

Im having an issue where i am trying to create shortcuts but the vbs script is cutting out when it reaches a space in the path.

i have had a look around but many of the ones i have seen deal with the string being in vbs not being passed from a batch file.

here is my code so you can get a better understanding

Batch File:

@echo off
set office7="C:\ProgramData\Microsoft\Windows\Start Menu\Strategix Programs\Office Programs"
mkdir %office7%
cscript "H:\Installation Batch Files\createLink.vbs" ""%office7%\Purchase Order Entry.lnk"" "\\192.168.0.7\Temp\stock\Porder10.exe" "T:\Stock"
pause

Vbs file:

   Set oShell = CreateObject("WScript.Shell") Set args = WScript.Arguments

sShortcut = oShell.ExpandEnvironmentStrings("" & args.Item(0) & "") sTarget = args.Item(1) sStartIn = args.Item(2)

WScript.Echo "Shortcut: " & sShortcut WScript.Echo "Target: " & sTarget WScript.Echo "StartIn: " & sStartIn

Output:

Shortcut: C:\ProgramData\Microsoft\Windows\Start Menu\Strategix Programs\Office Programs\Purchase
Target: Order
StartIn: Entry.lnk

Upvotes: 0

Views: 130

Answers (1)

MC ND
MC ND

Reputation: 70923

Batch part

@echo off
set "office7=C:\ProgramData\Microsoft\Windows\Start Menu\Strategix Programs\Office Programs"
mkdir "%office7%"
cscript "H:\Installation Batch Files\createLink.vbs" "%office7%\Purchase Order Entry.lnk" "\\192.168.0.7\Temp\stock\Porder10.exe" "T:\Stock"
pause

The "correct" way of dealing with quotes is not include them in the value. If later you need them, adding them is easy (look the mkdir command and the arguments), but removing them is not. Without a good reason, do not include them. So, the "correct" way is

set "var=value"

That will assign the value to the variable, take care of problematic characters (all the assignation is inside quotes) and keep possible spaces at the end of the line out of the variable value.

Now, to the vbs part

Dim oShell
    Set oShell = CreateObject("WScript.Shell") 

Dim args
    Set args = WScript.Arguments

Dim sShortcut, sTarget, sStartIn
    sShortcut = args.Item(0)
    sTarget = args.Item(1) 
    sStartIn = args.Item(2)

    WScript.Echo "Shortcut: " & sShortcut 
    WScript.Echo "Target: " & sTarget 
    WScript.Echo "StartIn: " & sStartIn

There is no need for ExpandEnvironmentStrings, this has been done when the batch line was parsed in cmd. %office7% is a reference to the value of the variable, not the name of the variable, and the parser replaces variable reads with variable values.

And for the shortcut creation

With oShell.CreateShortcut( sShortcut )
    .TargetPath = sTarget
    .WorkingDirectory = sStartIn
    .Save
End With

Upvotes: 2

Related Questions