1

I'd really appreciate some advice on how to make the following work.

I am using a CMD script to make a shortcut LNK file. It does this by echoing out various commands to a VBS file using >>.

One of the commands is called oLink.Arguments.

Currently my working example is set as follows:

oLink.Arguments = "/run /tn Custom_Scripts\TaskName" >>%VBSScript%

This only works if there are no spaces in Custom_Scripts\TaskName. I need, however, the script to work with task names which contain spaces.

Manually all I need to do is place quotes as follows:

"Custom_Scripts\TaskName"

However when set, the command line cannot output the line as there are quotes present already.

Example:

oLink.Arguments = "/run /tn "Custom_Scripts\TaskName"" >>%VBSScript%

Doesn't work. I've tried various things like triple quotes or escaping quotes but no luck.

I apologise if I'm not explaining this to well but hope someone out there may have a solution or idea which they're willing to share with me.

Edit:

From my comment underneath this answer.

SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "C:\TechTools\Scripts\Create_an_elevated_shortcut\myshortcut.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "C:\TechTools\O&O\ShutUp10\OOSU10.exe" >> %SCRIPT%
echo oLink.Arguments = "/run /tn Custom_Scripts\Task_Name" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%
cscript /nologo %SCRIPT%

This works as long as there are no spaces in the "Custom_Scripts\Task_Name".

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Can't replicate. `echo oLink.Arguments = "/run /tn "Custom Scripts\TaskName"" >>test.txt` writes `oLink.Arguments = "/run /tn "Custom Scripts\TaskName""` into the file. – Stephan Feb 08 '20 at 17:37

1 Answers1

1

Try to replace character " by chr(34):


echo=Chr(34)oLink.Arguments = Chr(34)/run /tn Chr(34)Custom_Scripts\TaskNameChr(34)>>"%VBSScript%"

Use escaping in ) == ^)


@echo off

set "_SCRIPT=%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
>"%_SCRIPT%" ( 
    echo= Set oWS = WScript.CreateObject("WScript.Shell"^)
    echo= sLinkFile = "C:\TechTools\Scripts\Create_an_elevated_shortcut\myshortcut.lnk"
    echo= Set oLink = oWS.CreateShortcut(sLinkFile^)
    echo= oLink.TargetPath = "C:\TechTools\O&O\ShutUp10\OOSU10.exe"
    echo= oLink.Arguments = "/run /tn ""Custom Scripts\Task Name""" 
    echo= oLink.Save
)
%__APPDIR__%cscript.exe /nologo "%_SCRIPT%" 

Obs.: Is this the output echoeded file


 Set oWS = WScript.CreateObject("WScript.Shell")
 sLinkFile = "C:\TechTools\Scripts\Create_an_elevated_shortcut\myshortcut.lnk"
 Set oLink = oWS.CreateShortcut(sLinkFile)
 oLink.TargetPath = "C:\TechTools\O&O\ShutUp10\OOSU10.exe"
 oLink.Arguments = "/run /tn ""Custom Scripts\Task Name"""
 oLink.Save

Obs.: Is this Custom Scripts\Task Name a sub-folder?


@echo off

set "_SCRIPT=%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
>"%_SCRIPT%" ( 
    echo= Set oWS = WScript.CreateObject("WScript.Shell"^)
    echo= sLinkFile = "C:\TechTools\Scripts\Create_an_elevated_shortcut\myshortcut.lnk"
    echo= Set oLink = oWS.CreateShortcut(sLinkFile^)
    echo= oLink.TargetPath = "C:\TechTools\O&O\ShutUp10\OOSU10.exe"
    echo= script_folder = replace(WScript.ScriptFullName,WScript.ScriptName,""^)
    echo= script_folder = chr(34^)^&script_folder^&"Custom Scripts\Task Name"^&chr(34^)
    echo= WScript.Echo script_folder
    echo= WScript.Echo "/run /tn "^&script_folder
    echo= oLink.Arguments = "/run /tn "^+script_folder
    echo= WScript.Echo  oLink.Arguments
    echo= oLink.Save
)
%__APPDIR__%cscript.exe /nologo "%_SCRIPT%" 


  • Outputs test with: WScript.Echo:
"C:\Users\ecker\AppData\Local\Temp\Custom Scripts\Task Name"
/run /tn "C:\Users\ecker\AppData\Local\Temp\Custom Scripts\Task Name"
/run /tn "C:\Users\ecker\AppData\Local\Temp\Custom Scripts\Task Name"


Adding quotes to a string in vbscript

Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • Doesn't seem to help. echo off set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs" echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT% echo sLinkFile = "C:\TechTools\Scripts\Create_an_elevated_shortcut\myshortcut.lnk" >> %SCRIPT% echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT% echo oLink.TargetPath = "C:\TechTools\O&O\ShutUp10\OOSU10.exe" >> %SCRIPT% echo oLink.Arguments = "/run /tn Custom_Scripts\Task_Name" >> %SCRIPT% echo oLink.Save >> %SCRIPT% cscript /nologo %SCRIPT% This works as long as there are no spaces in the "Custom_Scripts\Task_Name". – Boyd Fields Feb 08 '20 at 19:04