Running VB Script file with arguments - command-line

I have to run a VB script which has 2 arguments. So I am running a command below.
Delete_Dummy1.vb
s C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml.csv C:\Users\c6342\Deskto
p\XML_to_CSV\Temp_Files\xml1.txt
**VB Script Sample - not working:**
**sourceloc** = WScript.Arguments.Item(0)
**destloc** = WScript.Arguments.Item(1)
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTextStream = objFSO.OpenTextFile("**sourceloc**")
Set newFile = objFSO.CreateTextFile("**destloc**")
It throws the error as file not found. But if I hard code the sourceloc and destloc and remove the arguments it is working fine. it is throwing error only when i am using arguments.
**Working VB Script sample:**
Set oTextStream = objFSO.OpenTextFile("C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml.csv")
Set newFile = objFSO.CreateTextFile("C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt")
This works fine. But as per my project requirement, I cant hard code these file locations. I can pass as parameters from command.

After a length discussion in the comments think it will be best to just update my answer.
The reason for the Arguments not working is you never pass them to the OpenTextFile() and CreateTextFile() methods. Instead you are passing literal strings containing the variable name instead of the actual variables.
sourceloc = WScript.Arguments.Item(0)
destloc = WScript.Arguments.Item(1)
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Pass the variables not a string literal
Set oTextStream = objFSO.OpenTextFile(sourceloc)
Set newFile = objFSO.CreateTextFile(destloc)
As it stands VBScript keeps trying to locate a file called sourceloc and destloc instead of the actual file names from the passed Arguments collection. Which is what likely causes the
Microsoft VBScript Runtime Error: File Not Found
Note: Below is based on initial question which has since been revised.
This comes down to how you are passing the arguments to the script, any spaces in the values will be treated as new arguments. At the moment this is how the arguments are passed;
0. C:\Users\enter
1. code
2. herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv
3. C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt
I'm sure this isn't what you expect. To avoid this enclose each argument in double quotes ("...").
Delete_Dummy1.vbs "C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv" "C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt"
That way you get more what you expected
0. C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv
1. C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt

Related

How to include OS command with Substitute function in Openedge?

Im trying to create an PDF via OS-Command in OpenEdge but I hit an error when I run the script.
*Error : The command "C: \ Program" is either misspelled or could not be found
It works perfectly :
os-command (' "C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe"
"V:\V11\WEB\PDF\Name_01.03.2021_14.09.30_da.html"
"V:\V11\WEB\PDF\Name_01.03.2021_14.09.30_da.pdf" ').
However, when I Include the command in script and run it then I encounter an error.
This one doesnt work :
define variable cmdcommand as char no-undo. cmdcommand = SUBSTITUTE
(' "C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe"
"V:\V11\WEB\PDF\Name_&1_&2_&3.html"
"V:\V11\WEB\PDF\Name_&1_&2_&3.pdf" ', "01.03.2021", "14.09.30", "da").
os-command value(cmdcommand).
What did I miss here? Can anyone help?
After having fought with os-command for quite some time to get normal errors and output returned, if you are only targeting Windows then it you may find it easier to use the .Net System.Diagnostics.Process class.
To get you started:
define variable oProcess as System.Diagnostics.Process no-undo.
define variable oInfo as System.Diagnostics.ProcessStartInfo no-undo.
oProcess = new System.Diagnostics.Process().
assign
oInfo = oProcess:StartInfo
oInfo:FileName = "C:~\Program Files (x86)~\winmerge~\winmergeu.exe".
oInfo:WorkingDirectory = "session:temp-directory
oInfo:Arguments = substitute(
"&1 &2",
quoter( "file1.txt" ),
quoter( "file2.txt" )
)
.
oProcess:Start().
oProcess:WaitForExit().
Other useful properties of the ProcessStartInfo class include:
CreateNoWindow
UseShellExecute
RedirectStandardError
RedirectStandardOutput
In your second sample, there's no value in using SUBSTITUTE, as your're not using place holders (&1, &2, ...). What you're doing is basically a straight forward string assignment.
The resulting string looks like this:
"C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe""V:\V11\WEB\PDF\Name_01.03.2021_14.09.30_da.html""V:\V11\WEB\PDF\Name_01.03.2021_14.09.30_da.pdf"
there's an extra space at the beginning
there's no space between the closing quote of the path to your exe and the first argument.
This here works for me:
define variable cmdcommand as char no-undo.
cmdcommand = SUBSTITUTE ('"c:\Program Files (x86)\WinMerge\winmergeu.exe" &1 &2',
"c:\temp\1.txt",
"c:\temp\2.txt").
OS-COMMAND silent value(cmdcommand).
Due to the use of the SUBSTITUTE function with place holders, this gives me a clean command with a space between the exe path and the first argument.
It works with or without the SILENT option.

Batch URL parsing with powershell passed variables

I have a batch file that runs off a powershell command I created, and I want to make it so you can pass a variable from the PS command to a website url query..
My problem is that the variables I pass from PS are referenced in batch as %1, %2, %n
now I set these variables at the beginning to more meaningful named variables, but in my url eg: www.google.com/myQuery%20has%20spaces would print out my %2 variable from PS instead of a space.
Is there anyway to clear out the %1,%2 variables that are passed? or any work around?
edit: I have tried a simple set %1= to try and set it as a null variable but it didn't work.
You can escape the % in the string with a %% in batch, which solves the issue.

Xcode breakpoint shell command argument length

Trying to pass a large string to a shell script using a breakpoint in Xcode
let value = Array(repeating: "a", count: 1500).joined()
let string = "{\"key\": \"\(value)\"}"
Unfortunately, the string is being truncated. Is this limitation documented and can it be overcome?
It's been nearly a year since you asked this, and I'm not sure if it will solve your question, but I've recently had a similar problem so thought I'd share my solution.
I had two issues:
LLDB was truncating any arguments to my shell script (and string variables printed in the console using po foo) to 1023 characters. I believe this is the issue to which your question relates.
Xcode was incorrectly confusing a comma , in my string as a separator for multiple arguments (e.g. passing foo, bar, and baz as arguments to the script wouldn't work correctly if any of the variables contained a , as Xcode would try to create another argument).
So, firstly, the LLDB issue...
It seems that by default LLDB has a limit on the character length that it will print to the console (or pass to a shell script via a breakpoint argument) of around 1023 characters. You can easily change this to something larger by setting another breakpoint before the breakpoint that uses your variable and running (lldb) set set target.max-string-summary-length 10000 in the console. This can be a bit annoying so I created a ~/.lldbinit file and placed set set target.max-string-summary-length 10000 in there instead so I don't have to keep setting it in the console.
Secondly, the comma issue...
Inside the Edit breakpoint... menu that you provided a screenshot of above there is the option to not only provide a path to a script but to also provide arguments. I can see from your question that you provided the argument #string#. For my script, I was passing multiple arguments, which Xcode allows you to do using a comma separated list, e.g. #foo#, #bar#, #baz#. Each of these arguments was a string.
I noticed that sometimes one or more of these strings would truncate if they contained a comma: ,.
So the string:
{ "num_friends" : "7" }
would be passed to my script as expected. But the string:
{ "num_friends" : "7", "num_deleted_friends" : "1" }
would truncate and would be passed to my script as two separate arguments. It seems that Xcode would split any string with a , even when entered using #string#.
I validated this in my script by simply using something like:
for var in "$#"
do
echo "$var"
echo "===="
done
Where $# expands to contain each argument. From this I could see that #string# was being correctly passed to my script but separated as multiple arguments wherever there was a ,. So if #string# contained a comma my script would print:
#"{ \"num_friends\" : \"7\"
====
\"num_deleted_friends\" : \"1\" }"
instead of what I expected which was:
#"{ \"num_friends\" : \"7\", \"num_deleted_friends\" : \"1\" }"
So it seems like it might be a bug in how Xcode passes strings inside # expressions in the breakpoint editor window.
My crude solution has been to just replace any commas with another character and then replace them back again inside my script. There's probably a better way to do this but I don't require it for my needs.

using the path function to change the search path

So I am trying to save a new search path.
I am trying to follow this example from Mathworks Mathworks example
My code is below. mypath is a 120 x 1 cell
path('mypath')
However when I run the code a pop up window says "Error using eval Underfined function 'workspacefunc' for input arguments of type 'struct'
When I try to check the path using the line below I get told the 'path may be bad'
checkPath = path;
The version they provide works with an array of strings, not a cell of strings. Hence, this should work:
mypath = char(mypath);
path('mypath');

perl-global enviornment variables

I am using set command in cmd to assign global variables
set TEMPDATA = C:\temp_data
In the same cmd session I am calling a Perl script
my $temp_path = $ENV{'TEMPDATA'}."\\temp.c";
But it gives this error:
use of uninitialised value $ENV{'TEMPDATA'}.
when I use setx then it works.
But I need to have a temporary variable which should be deleted as soon as session is closed and for that I need to use set only
do not include blank spaces in variable declaration. Use like this:
set TEMPDATA=C:\temp_data