From a vbscript I'm trying to get the user profile directory to expand and also run an executable with command line arguments. It is the spaces in the command line args that seem to be causing an issue. I'm close but no matter what I try I can't seem to get it.
This works:
WshShell.Run """%UserProfile%\test.exe"""
What I'm trying to do that does not work:
WshShell.Run """%UserProfile%\test.exe 8.8.8.8 8989 -e cmd.exe"""
I get an error of "The system cannot find the file specified."
As already mentioned in the comments the reason for the error in the second example is because the literal quotes are only needed to wrap a file path containing spaces, so you line should be;
WshShell.Run """%UserProfile%\test.exe"" 8.8.8.8 8989 -e cmd.exe"
It's the equivalent of writing
"%UserProfile%\test.exe" 8.8.8.8 8989 -e cmd.exe
at the command prompt in a console window (note, the position of the literal quotes).
Useful Links
Answer to: About using Double quotes in Vbscript
You can debug your variable before any execution with Wscript.Echo or MsgBox
Wscript.Echo chr(34) & CreateObject("wscript.shell").ExpandEnvironmentStrings("%UserProfile%\test.exe") & chr(34) & " 8.8.8.8 8989 -e cmd.exe"
Related
This question already has answers here:
Combining variables in Bash to form a command sent to AppleScript using the osascript command
(2 answers)
Closed 5 years ago.
I'm in need of your assistance on how to deal with white space in a path.
In my example below, I'm setting some variables in a Terminal bash shell and executing, "osascript" from command line, utilizing these variables to run ['do shell script'] & ['display dialog']. When I run these from command line, I'm getting different results. My ultimate goal here is to be able to execute ['do shell script'] and it execute the bash shell script based on the path. In this case, the path has spaces in it.
NOTE: The INSTALLER_PATH variable defined below is set like this because the path is generated from an Apple Script I wrote that basically takes a path [ with white spaces in it ] and combines that path with another variable. Because this path has spaces in the name, I'm using the [ to quoted form of ] setting that puts the path in quotes.
APPLE SCRIPT EXAMPLE:
set pathToApp to POSIX path of ((path to me) as text)
set dragonFrame to ("_DRAGONFRAME/")
set INSTALLER_PATH to quoted form of pathToApp & dragonFrame
display dialog INSTALLER_PATH
GENERATES THIS PATH {notice the ticks}:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
Instead of testing this through the Apple Script to find the exact syntax I would need, I figured it would be easier to test this from command line. This is exactly what I'm setting and executing from my Terminal command line. In the examples below, executing [ 'display dialog' ] works and [ 'do shell script' ] fails. I think if there's a way to enclose the entire path in double quotation marks, my problem would be solved although all attempts at getting the path enclosed in double quotation marks has failed:
%> INSTALLER_PATH='/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/'_DRAGONFRAME/
%> ADMIN_USER_PROC="_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh"
%> osascript -e 'do shell script ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS ERROR:
0:217: execution error: sh: /Volumes/Free.Space/Shotgun: No such file or directory (127)
%> osascript -e 'display dialog ("'"${INSTALLER_PATH}"'" & "'"${ADMIN_USER_PROC}"'")'
GENERATES THIS PATH:
/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/_Python_PySide_QT_Installer/Scripts/AdminUserProcesses.sh
Thank you in advance for any help you can offer.
I think you're looking for something similar to this syntax:
set dragonFrame to ("/_DRAGONFRAME/")
set INSTALLER_PATH to path of (pathToApp & dragonFrame)
Result:
'/Volumes/Free.Space/Shotgun Python Dragon Project 2017/DRAGONFRAME_SCRIPTS_MASTER_V1.02/Dragonframe_Scripts_Installer.app/_DRAGONFRAME/'
If you need double-quotes around the path you can use:
("\"" & pathToApp & dragonFrame & "\"")
and get rid of the quoted form argument leaving you with:
set INSTALLER_PATH to ("\"" & pathToApp & dragonFrame & "\"")
I am trying to get a VBScript to launch another VBScript while passing an argument from the first one to the second one. I got the part of how to receive the argument on the second script, however I have no clue how to call it within the first VBScript. Here is what I currently have in the first one:
arg1 = "MyArgument"
objShell.Run "ArgumentTest2.vbs arg1"
When I run the this script, it gives me the error message:
Script: C:\Argument Test 1.vbs
Line: 2
Char: 1
Error: Object required 'objShell'
Code: 800A01A8
Source: Microsoft VBScript runtime error
Any help would be greatly appreciated! Thanks!
You need to initialize objShell with
Set objShell = CreateObject("WScript.Shell")
before you can use its .Run method.
Also, VBScript doesn't expand variables inside strings, so you'll need to concatenate your argument to the rest of the command string:
objShell.Run "ArgumentTest2.vbs " & arg1
Note that you'll need to put tokens in double quotes if they contain spaces:
arg1 = "My Argument"
objShell.Run "ArgumentTest2.vbs """ & arg1 & """"
I can use CreateObject("shell.application").ShellExecute but can I run a cscript as administrator? using that? I'm kind of a newbie vbscripter. Thanks!
Have you tried the RunAs command? I posted a solution recently that walks you through getting the proper syntax for executing the RunAs command from within VBScript.
Take a look and see if it helps.
The scriptPath is the path to your script you want to run as administrator
The Arguments is the commandline arguments you want to pass to the script. NOTE the Arguments are space separated, if you want to pass an argument that contains spaces you will need to enclose that arguments in quotes [Chr (34) is the quotes]. to do that you will need to write for example. Arguments= chr (34) & "Hello World" & chr(34)
ScriptPath="Path to your script"
Arguments="Any arguments to pass to the script"
CreateObject("Shell.Application").ShellExecute "cscript.exe",_
Chr(34) & ScriptPath & Chr(34) & _
" " & Arguments, _
"", "runas", 1
I'm trying to understand how the ^ Caret escape works in a Batch CMD. Lets say I type this line in a CMD Window, I would expect it to send the DIR listing to "c:\my text.txt"
CMD.EXE "/C > ^"c:\my text.txt^" DIR C:\*.*"
Instead I get the error:
'txt.txt" DIR *.*"' is not recognized as an internal or external command,
operable program or batch file.
If I do something as simple as this:
ECHO "^"^""
Expected: """"
Actual : "^"""
Even if I try double quoting like this:
ECHO " "" "" "
Expected: " " " "
Actual : " "" "" "
Can someone 'splain how this works and what is a reliable way is to escape double quotes in a command line?
Thanks.
Additional Example:
Why does this work:
cmd.exe "/C sqlcmd.exe -S.\SQLEXPRESS -E -Q"select suser_name()" > "c:\temp\test 1.txt""
but this gives error "The system cannot find the path specified." and does not create the txt file.
cmd.exe "/C "sqlcmd.exe" -S.\SQLEXPRESS -E -Q"select suser_name()" > "c:\temp\test 1.txt""
I know it is not needed in this case, but how would I enclose the command sqlcmd.exe in quotes?
I also tried removing the quotes from the whole line with the same results, i.e.,
cmd.exe /C "sqlcmd.exe" -S.\SQLEXPRESS -E -Q"select suser_name()" > "c:\temp\test 1.txt"
Thanks again.
Quotes are a state machine within cmd.exe. The first quote turns quoting semantics on, the next quote turns it off, the next back on, etc.
It is possible to escape a quote such that it does not turn quote semantics on.
For example, the following will send a quote character to test.txt
echo ^" >test.txt
Without the escape, the string " >test.txt is simply printed to the screen.
Unfortunately, it is not possible to escape a quote that turns off quote semantics.
For example, suppose you want to print the string "hello world" >"test.txt" to the screen. None of the techniques you tried will work because an internal quote cannot be escaped. The ^" form treats the ^ as a literal, and the output will still be redirected to a file. The "" for properly quotes the >, so there is no redirection, but now you have two quotes instead of one.
You have two options.
1) escape all "poison" characters that are not quoted
echo "hello world" ^>"test.txt"
2) hide the quote literal within a delayed expansion variable
I'm assuming this code is in a batch script.
setlocal enableDelayedExpansion
set "quote=""
echo "hello world!quote! >!quote!test.txt"
Note that using this technique can become tricky if dealing with FOR /F ... IN('command') or CMD /C because delayed expansion will probably be off within the new CMD.EXE context.
Here is a way to do your original command
cmd /c dir c:\*.* ^>"c:\my text.txt"
Note that the entire command string after /c does not have to be enclosed within quotes. In this case, it is easier if it is not.
I believe your new command at the end can be written as follows:
cmd.exe /C ""sqlcmd.exe" -S.\SQLEXPRESS -E -Q"select suser_name()" > "c:\temp\test 1.txt""
If used within a parenthesized block, then the right paren will need to be escaped.
(
cmd.exe /C ""sqlcmd.exe" -S.\SQLEXPRESS -E -Q"select suser_name(^)" > "c:\temp\test 1.txt""
)
Basically I would like to export my exact PATH variable to a file automatically. It contains things like %ANT_HOME%/bin and I would like to keep it that way. From what I could find, using both set and echo will execute that argument and give me the absolute path. Is there something I'm missing?
To get a copy of your PATH without expansion of environment variables you could save the following as "rawPath.vbs"...
Option Explicit
Dim wsh
Set wsh = CreateObject("Wscript.Shell")
Wscript.Echo wsh.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
...and then issue the following command to pipe the output to a file
cscript -nologo rawPath.vbs > myPath.txt
Do you see %ANT_HOME% when you execute SET from the prompt?
If so,
>filename echo %path%
should export the path as desired.
If the PATH variable does not actually contain the "%" characters, then it's already been resolved. And remember, "%" is actually a legitimate (but annoying) filename character...
You CAN set a "%" character into an environment variable
set var=%%something%%
will set var to %something%
You need to escape the % good sir, example
>echo %path%
C:\windows\system32;C:\windows\system32\wbem
>echo ^%path^%
%path%