I'm trying to pass the working directory to vbscript as a named argument. The system normally expands "." to the current path, but when I check the named argument I just get the string "."
Here's the command line:
cscript myscript.vbs /a:"first arg" /b:second /c:.
Here's the script:
dim args : set args = wscript.arguments.named
wscript.echo args.item("a")
wscript.echo args.item("b")
wscript.echo args.item("c")
Here's the output:
first arg
second
.
Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.GetAbsolutePathName(args("c"))
Or you could use /c:"%CD%" instead of /c:..
If you always want to know the current directory, you don't need to pass it as an argument, though. Simply use
cwd = CreateObject("WScript.Shell").CurrentDirectory
Related
I need to initialize multiple env vars only on certain conditions, I cannot use anything but command line.
For the example of my problem I am going to take a symple case of initializing an alphabet.
First cmd script is :
#echo OFF
set PATH=%PATH%;"%~dp0"
IF "%_ALPHABET%"=="" (
echo "DEFINE"
call setEnvA
call set "_ALPHABET=%_ALPHABET%;b"
) ELSE (
echo "ALREADY DEFINE"
set _ALPHABET=
set A_ADDED=
)
setEnvA cmd is :
IF "%A_ADDED%"=="" (
set A_ADDED=OK
set _ALPHABET=%_ALPHABET%;a
)
I was expecing
;a;b
as a result, but I get only
;b
I tried to throw random delayedexpansion but without any result. I am starting to think that this is not possible and I should do some dirty goto.
The contents of environment variable _ALPHABET is being substituted within the parenthesis before the call to setEnvA is performed. You need to do something like:
#echo OFF
set PATH=%PATH%;"%~dp0"
IF "%_ALPHABET%"=="" (
echo "DEFINE"
call setEnvA
call :set_ALPHABET
) ELSE (
echo "ALREADY DEFINE"
set _ALPHABET=
set A_ADDED=
)
exit /b
:set_ALPHABET
set "_ALPHABET=%_ALPHABET%;b"
So I'm trying to run a CMD prompt as a VBScript because I can't just drop this CMD Line.
Set cmdl = WScript.CreateObject("WScript.Shell")
cmdl.Run "cmd.exe ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe""--app="https://app.powerbi.com/"""--kiosk""--fullscreen""--user-data-dir=c:/monitor1""
My dilemma is that every time I try and run this I get the following error:
Script: Script.vbs
Line: 2
Char: 90
Error: Expected end of statement
Code: 800A0401
Source: Microsoft VBScript compilation error
I've tried putting in quotes, taking out quotes, moving spaces, etc. and this dang thing is driving me crazy. Does anyone see where my mistake may lie?
Your command string is broken. In VBScript a string is defined by putting a character sequence between double quotes:
s = "some string"
If you want to use double quotes within a string you need to escape them by doubling them:
s = "some ""quoted"" string"
Also, you don't need cmd.exe for starting an executable via Run, but you do need whitespace between the parameters to the executable.
Change this:
cmdl.Run "cmd.exe ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe""--app="https://app.powerbi.com/"""--kiosk""--fullscreen""--user-data-dir=c:/monitor1""
into this:
cmdl.Run """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" --app=https://app.powerbi.com/ --kiosk --fullscreen --user-data-dir=c:/monitor1"
The rule is: Use "" to insert " in VBScript literals.
The first violation in your
"cmd.exe ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe""--app="https://app.powerbi.com/"""--kiosk""--fullscreen""--user-data-dir=c:/monitor1""
is
--app="https
This
"cmd.exe ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" --app=""https://app.powerbi.com/"" --kiosk --fullscreen --user-data-dir=""c:/monitor1"""
may be what you want.
A better (scaling) approach is to use a quote function and an array for the parts/arguments:
Function qq(s)
qq = """" & s & """"
End Function
s = Join(Array( _
"cmd.exe" _
, qq("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe") _
, "--app=" & qq("https://app.powerbi.com/") _
, "--kiosk" _
, "--fullscreen" _
, "--user-data-dir=" & qq("c:/monitor1") _
), " ")
WScript.Echo s
output:
cscript a.vbs
cmd.exe "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --app="https://app.powerbi.com/" --kiosk
--fullscreen --user-data-dir="c:/monitor1"
This solution is based from #Ekkehard.Horner
So, i liked his method when using the join function with the array.
And, i tried it on my windows 7 (32 bits) and it works like a charme ;)
Function qq(s)
qq = chr(34) & s & chr(34)
End Function
s = Join(Array( _
"cmd /c start chrome" _
, "--app=" & qq("https://app.powerbi.com/") _
, "--kiosk" _
, "--fullscreen" _
, "--user-data-dir=" & qq("c:/monitor1") _
), " ")
WScript.Echo s
set ws = CreateObject("wscript.shell")
ws.run s,0,false
I want to call matlab & at the same time run .m file from visual basic 6. but I'm getting this run-time error '91', variable not set bla bla. I've searched the internet to find any solution but I couldn't. There is something wrong with my code, I don't know what it is. can anyone please check & see what's wrong?
Private Sub Form_Load()
Dim MatLab As Object
Dim Result As String
Dim MReal(1, 3) As Double
Dim MImag(1, 3) As Double
Dim mat_exe As String
Dim mat_io_folder As String
Dim mat_m As String
mat_exe = "G:\matlab\bin\matlab.exe"
mat_io_folder = "G:\Farin\New folder"
mat_m = "Untitled.m"
FileName = mat_exe & " " & "addpath('mat_io_folder') & mat_m" & " -s1"
runmatlab = Shell(FileName, 1)
Result = MatLab.Execute("cd G:\Farin\New folder")
Result = MatLab.Execute("Untitled")
'Calling m-file from VB
'Assuming solve_bvp exists at specified location
'Result = MatLab.Execute("cd G:\Farin\New folder\Untitled")
End Sub
Error 91 in VB6 means object variable not set, which, at a guess, would be the statement
result = MatLab.Execute("...")
Matlab is declared as an object but it has not been assigned a value. List of VB6 runtime errors can be found in https://msdn.microsoft.com/en-us/library/aa264975(v=VS.60).aspx
Another problem is the Filename assignment. It should read
FileName = mat_exe & " " & "addpath('" & mat_io_folder & "') " & mat_m & " -s1"
Might be an idea to MsgBox Filename before running the shell command.
I am trying to autofilter in Excel using the below VBScript code. This script called multiple times from a Perl program.
Dim objExcel : Set objExcel = GetObject(,"Excel.Application")
objExcel.Visible = True
objExcel.Selection.AutoFilter
objExcel.ActiveSheet.Range("G1").AutoFilter WScript.Arguments.Item(0), _
WScript.Arguments.Item (1)
Now I would like to know: is there a way by which I can pass an array for WScript.Arguments.Item (1) so that all the conditions are selected in one go? The task is to delete the filtered value. I call this script through Perl multiple times and the above script filter one value at a time and delete. The program works fine, but is slow.
Following is the part of Perl which calls the VBScript.
while(<FILE>){
chomp;
system("CSCRIPT "."\"$currentWorkingDirectory\"".'aboveVBS.vbs 9 '."\"$_\"");
sleep(2);
}
If you put quotes around the values, VBScript will treat it as a single argument.
> cscript script.vbs arg1 "multiple values for arg 2"
In the script:
WScript.Echo WScript.Arguments.Count ' ==> 2
a = Split(WScript.Arguments(1))
WScript.Echo a(0) ' ==> multiple
WScript.Echo a(1) ' ==> values
WScript.Echo a(2) ' ==> for
WScript.Echo a(3) ' ==> arg
WScript.Echo a(4) ' ==> 2
Excel expects:
Range.AutoFilter <Field>, <Criteria>, <Operator>
If you want a list of criteria to filter on, you'll use xlFilterValues for the <Operator> argument. <Criteria> will be an array of string values, which we created above.
Const xlFilterVaues = 7
objExcel.ActiveSheet.Range("G1").AutoFilter WScript.Arguments.Item(0), a, xlFilterValues
So, just try adding Split() around WScript.Arguments(1) in your existing code, and pass xlFilterValues for the third param.
If only your second argument changes, you could pass the entire content of your data file to the VBScript:
local $/;
my $args = <FILE>;
$args =~ s/^\s+|\s+$//g;
$args =~ s/\r?\n/" "/g;
system("cscript \"$currentWorkingDirectory\\your.vbs\" 9 \"$args\"");
and change the processing in your VBScript to this:
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbook.Open("C:\path\to\your.xlsx")
Set ws = wb.Sheets(1)
...
xl.Selection.AutoFilter
For i = 1 To WScript.Arguments.Count - 1
ws.Range("G1").AutoFilter WScript.Arguments(0), WScript.Arguments(i)
...
Next
Or you could simply call the VBScript with the field and the path to the data file:
system("cscript \"$currentWorkingDirectory\\your.vbs\" 9 \"$filepath\"");
and do all the processing in VBScript:
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbook.Open("C:\path\to\your.xlsx")
Set ws = wb.Sheets(1)
...
xl.Selection.AutoFilter
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(WScript.Arguments(1))
Do Until f.AtEndOfStream
ws.Range("G1").AutoFilter WScript.Arguments(0), f.ReadLine
...
Next
f.Close
Applying more than 2 AutoFilter conditions to a column at the same time is not possible. Check the signature of the AutoFilter method in the documentation:
expression .AutoFilter(Field, Criteria1, Operator, Criteria2, VisibleDropDown)
expression An expression that returns a Range object.
You have Critera1 and Criteria2 and an Operator for combining the two. Calling the AutoFilter method with another set of criteria replaces the existing criteria.
I'm attempting to pass a property to MSBuild. The property is a semicolon-delimited list of values. Unlike this question, I'm running MSBuild from PowerShell.
I get:
PS> msbuild .\Foo.sln /p:PackageSources="\\server\NuGet;E:\NuGet"
MSBUILD : error MSB1006: Property is not valid.
Switch: E:\NuGet
If I run the same command from Command Prompt, it works fine. How do I get it to work in PowerShell?
Wrap the parameter in single quotes:
... '/p:PackageSources="\\Server\NuGet;E:\NuGet"'
On PowerShell v3 try this:
msbuild .\Foo.sln --% /p:PackageSources="\\Server\NuGet;E:\NuGet"
Also using ASCIII value helps:
msbuild .\Foo.sln /p:PackageSources="\\Server\NuGet%3BE:\NuGet"
VBScript function below can be used to escape property values passed to MSBuild.exe inside double quotes:
Function Escape(s)
Escape = s
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "(\\+)?"""
Escape = objRegEx.Replace(Escape,"$1$1\""")
objRegEx.Pattern = "(\\+)$"
Escape = objRegEx.Replace(Escape,"$1$1")
End Function
The following example demonstrates usage of Escape() function
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "msbuild.exe echo.targets /p:Param1=""" & Escape("ParamValue1") & """,Param2=""" & Escape("ParamValue1") & """", 1, True