Why won't ArrayList.execute() use given arguments? - arraylist

I'm trying to run a batch script on windows using groovy. I started with this code:
StringBuilder output = new StringBuilder()
StringBuilder error = new StringBuilder()
dir = new File("$homedir")
Process proc = "pathToScript\script.bat --arg test".execute(null, dir)
proc.consumeProcessOutput(output, error)
proc.waitFor()
print proc.exitValue()
This code works just fine for my scripts, but some commands don't play nice, so I decided to start using an ArrayList to store the arguments. So now my code looks like this:
StringBuilder output = new StringBuilder()
StringBuilder error = new StringBuilder()
dir = new File("$homedir")
ArrayList command = ["pathToScript\script.bat", "--arg test"]
Process proc = command.execute(null, dir)
proc.consumeProcessOutput(output, error)
proc.waitFor()
print proc.exitValue()
Now for some inexplicable reason it runs script.bat without the arguments. I've printed out the list to be sure it contains the arguments properly, which it does, so I'm not sure why the execute method isn't using them.

Try:
ArrayList command = ["pathToScript\script.bat", "--arg", "test"]
When an instance of List is used as source for execute() all arguments should be passed separately.

Related

VB.net Unable to cast object of type System.Collections.Generic.list to type string

Please forgive me in advanced for my lack of coding knowledge.
I'm using a NuGet package called KuCoin.Net and have everything setup and connected. I can run the command and Place a Buy order so I know my Api settings are correct. The issue I'm having is when I run the following code:
Public Async Function GetBalancesAsync() As Task
Dim kucoinClient = New KucoinClient(New KucoinClientOptions() With {
.ApiCredentials = New KucoinApiCredentials("xxx", "xxx", "xxx"),
.LogLevel = LogLevel.Debug,
.RequestTimeout = TimeSpan.FromSeconds(60),
.FuturesApiOptions = New KucoinRestApiClientOptions With {
.ApiCredentials = New KucoinApiCredentials("xxx", "xxx", "xxx"),
.AutoTimestamp = False
}})
Dim accountData = Await kucoinClient.SpotApi.Account.GetAccountsAsync()
MessageBox.show(accountData.data)
End Function
I guess I'm needing to convert the list to a string so I can display it into a Messagebox.
The Error I recieve is as follows:
Unable to cast object of type 'System.Collections.Generic.List`1[Kucoin.Net.Objects.Models.Spot.KucoinAccount]' to type 'System.String'
Here is some additional info if this helps
Error
accountData
Any help is much appreciated
You can generate your String yourself using a StringBuilder while enumerating through accountData.Data:
Dim sb As New System.Text.StringBuilder
For Each account As Kucoin.Net.Objects.Models.Spot.KucoinAccount In accountData.data
sb.AppendLine(account.ToString)
Next
MessageBox(sb.ToString())
You can change account.ToString to something more appropriate like accout.Number perhaps (see the properties the KucoinAccount object has).

Clearing the existing collection and adding but still says - The SqlParameter is already contained by another SqlParameterCollection

I am not able to figure out whats wrong with this code. i am clearing all the parameters and then adding them but it still gives me the error saying "The SqlParameter is already contained by another SqlParameterCollection."
Please help
using (SqlConnection m_Connection = Class_SetApplicationEnviroment.Get_Sql_Connection())
{
m_Connection.Open();
SqlCommand oSqlCommand = new SqlCommand(m_spName, m_Connection);
oSqlCommand.CommandType = CommandType.StoredProcedure;
if (m_Parameters.Length > 0)
{
//SQLDataAdapter.SelectCommand.Parameters.Clear();
oSqlCommand.Parameters.Clear();
foreach (SqlParameter oParam in m_Parameters)
{
if (oParam != null)
{
// Check for derived output value with no value assigned
if ((oParam.Direction == ParameterDirection.InputOutput ||
oParam.Direction == ParameterDirection.Input) &&
(oParam.Value == null))
{
oParam.Value = null;
}
oSqlCommand.Parameters.Add(oParam);
//SQLDataAdapter.SelectCommand.Parameters.Add(oParam);
}
}
}
//Execute the Stored Procedure
//SQLDataAdapter.Fill(myTable);
strReturnValue = oSqlCommand.ExecuteNonQuery().ToString();
m_Connection.Close();
}
}
As I can see your m_Parameters is a private collection of parameters in your class.
When you instance this class, and run this code for the first time, you create a command, and add the parameters of this collection to a newly created command Parameters collection.
On the second run, you create a new different command, and try to add the parameters to the new, fresh command. And, as your parameters were already attached to the old command's collection, you get taht error.
You can create a single command as a member of your class, attach the parameters to its collection and reuse it as many times as you want. Or you can create a new collcetion of parameter for each command execution. Perhaps (I'm not sure) if you run this line oSqlCommand.Parameters.Clear(); just after closing the connection, they'll be "freed up" and can be used on the next execution. You can try this first

Dart file copy in Windows

The File class in the dart:io library doesn't yet include copy() and move() methods.
To tide me over until they arrive, I'm trying to roll my own copy function. I'm using the code below on Windows, but it just creates a 0kb file.
void copyFile(String input, String output) {
var inFile = new File(input), outFile = new File(output);
if (outFile.existsSync()) outFile.deleteSync(); // I realize this isn't required
var inStream = null, outStream = null;
try {
inStream = inFile.openInputStream();
outStream = outFile.openOutputStream(FileMode.WRITE);
inStream.pipe(outStream);
} finally {
if (outStream != null && !outStream.closed) outStream.close();
if (inStream != null && !inStream.closed) inStream.close();
}
}
I've also tried replacing the pipe line with print(inStream.read(100).toString()); and I get null. The input file does exist (otherwise I'd get a FileIOException). Am I doing something wrong, or are input streams broken under Windows?
I'm using:
Dart Editor version 0.3.1_r17463
Dart SDK version 0.3.1.2_r17463
Edit: The following works (although it doesn't "chunk"). Am I using the streams above incorrectly?
void copyFile(String input, String output) {
var inFile = new File(input), outFile = new File(output);
if (outFile.existsSync()) outFile.deleteSync(); // I realize this isn't required
outFile.writeAsBytesSync(inFile.readAsBytesSync(), FileMode.WRITE);
}
With your first code snippet, you get an empty file because pipe is not a synchronous method. Thus, the copy of inputStream to outputStream has not started when the finally block is execute. By closing the streams in this finally block, you stop the pipe before it even starts. Without that finally block the copy is done correctly.
void copyFile(String input, String output) {
final inStream = new File(input).openInputStream();
final outStream = new File(output).openOutputStream(FileMode.WRITE);
inStream.pipe(outStream);
}
Finally, you don't have to worry about closing streams because pipe close streams by default once achieved. See InputStream.pipe.
For synchronous copy, use:
File(sourceFile).copySync(destinationFile);
For asynchronous copy, use:
File(sourceFile).copy(destinationFile);

Getting No application is associated with the specified file for this operation while executing AutoIt scripts from vb.net

I have installed AutoIt on my machine. Its working ok on one machine but same configuration and code is not working on other. Any idea what I am missing?
Following error
Could not start process Z:\test\AutoItScripts\test.au3
No application is associated with the specified file for this operation
Also autoit script successfully executes from command line. Its just getting this error while using the following code to execute
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.Verb = "runas"
objProcess.StartInfo.Arguments = Argument
objProcess.StartInfo.FileName = ProcessPath
objProcess.Start()
'Wait until it's finished
objProcess.WaitForExit()
'Exitcode as String
Console.WriteLine(objProcess.ExitCode.ToString())
objProcess.Close()
Because AutoIt3 scripts are not themselves executable, you will need to be using shellexecute.
p.UseShellExecute = true;
Process compiler = new Process();
compiler.StartInfo.FileName = sExeName;
compiler.StartInfo.Arguments = sParameters;
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();
Console.WriteLine(compiler.StandardOutput.ReadToEnd());

Redirecting Standard Output from a Process (msxsl.exe) to a string in VB.NET

I am writing a command line application in VB.NET. This application is calling another one, msxsl.exe, to run an XSL transform. I am using the Process class to do this:
Dim process = New Process()
process.StartInfo.FileName = "msxsl.exe"
process.StartInfo.Arguments = "base.xml test.xsl -o styled.xml"
process.StartInfo.UseShellExecute = False
process.StartInfo.CreateNoWindow = True
process.StartInfo.RedirectStandardOutput = True
process.Start()
This part works great. What I want it to be able to display the output from this process to the console of my application. I have read several posts explaining this method, but it does not seem to work in this case. The output is an empty string.
Dim output As String = process.StandardOutput.ReadToEnd()
process.WaitForExit()
Console.WriteLine(output)
I have verified that if I run the msxsl executable on its own (i.e. running "msxsl.exe base.xml test.xsl -o styled.xml"), it displays output on the command line. What am I doing wrong?
EDIT: I should note that the msxsl process is currently failing due to a malformed XML file. It is displaying this error message:
Error occurred while executing stylesheet 'test.xsl'.
Code: 0x800c0006
The system cannot locate the object specified.
This is exactly the type of thing I want displayed in the console of my application (or, eventually, a log file.)
This is probably because this isn't standard output it is StandardError you will want to redirect StandardError like so Process.StartInfo.RedirectStandardError = True and then read that into a string.
Dim ErrorString As String = Process.StandardError.ReadToEnd()