Retrieving exit code of batch file in PeopleCode - winscp

I have the following Java code in PeopleCode to execute a batch file (which in turn executes WinSCP script file). How to get the return code?
Else if you guys have similar code in people code to transfer file. Please let me know.
Local JavaObject &runtime = GetJavaClass("java.lang.Runtime").getRuntime();
Local JavaObject &process = &runtime.exec("\\xyz\BATCH_FILE_NAME.bat");
Rest of the code.

Use the Process.exitValue() method:
&process.exitValue()
This will of course get you an exit code of the batch file. Whether that gets you exit code of WinSCP depends on how the batch file is implemented. You didn't show us.
In general you propagate exit code of WinSCP to exit code of batch file like:
winscp.com /ini=nul /command ...
exit /b %ERRORLEVEL%
Read also about Checking script results.
Though with such trivial batch file, it does not really make sense to use the batch file in the first place. You can execute the winscp.com (or the winscp.exe) directly from your PeopleCode.

Related

Getting error level of a bat file when running it from Python

I have a .bat file that calls different things and runs different scripts to compile and build a system developed by other programmers.
I am trying to call that .bat file from Python using Qprocess. The bat file runs smoothly. However, if the bat file by some reason fails, my python script needs to be terminated in some way.
I had a short discusion with some of the guys that wrote that huge .bat file and they told me that the .bat file has a standard structure and returns error levels if something is failed.
I don't really know how to catch those levels and I don't know so much about batch programming. Is Error level something universal? Can it be catched from Python?
I googled a bit and I found some very old articles that didn't teach me so much. Any one has a short example on how to catch those levels?
Error level in a batch file context means it will exit with a certain code. You can query this exit code as soon as the process finished.
def properties(self):
self.process=QProcess()
self.process.finished.connect(self.onFinished)
self.process.start('test.bat', ['arg'])
def onFinished(self, exitCode, exitStatus):
[check exit code here...]

Is it possible to call a batch file from watir?

I am trying to call a batch file from watir-webdriver. I am using ruby with watir. I tried to use system(filename.bat) but it doesn't work.
I have written a step definition for this and when I run it I got an error stack level too deep (SystemStackError).
And(/^I execute a batch file$/) do
system('filename.bat')
end

How to change the current working directory in Groovy

Now, I am writting a Groovy script to invoke other's interface. But I need change my current working path when running the script. I know it is not possible in Java. Is it possible in Groovy?
If you can run other script as separate process, you can give ProcessBuilder parameter working dir:
def processBuilder=new ProcessBuilder(command)
processBuilder.directory(new File("Working dir"))
def process = processBuilder.start()
or
command.execute(null, new File("Working dir"))
so that process will switch to your new folder and execute it there.
As Groovy runs on JVM, the same restrictions apply. Unfortunately it is not possible.
Changing the current working directory in Java?
JDK bug
Java/groovy doesn't really "Have" a working directory as far as I can tell. The shell that launched groovy has one and any child "commands" inherit from that shell diretly.
Java also seems to read the current directory of the shell and store it in "user.dir". This is used as a base for the "File" object so if you System.setProperty("user.dir", "c:/windows") it will change future invocations of new File(".") but will not change the parent shell directory (and therefore not the child directories).
Here are three "Work-Arounds" that may work for different scenarios:
1) I KIND OF overcame this for a very specific task... I wanted to implement "cd" as a groovy script. It was only possible because all my scripts were already being "wrapped" in a batch file. I made it so that my script could create a file called "afterburner.cmd" that, if it existed, would be executed when the script exits. There was some batch file trickery to make this work.
A startup cmd file could also "Set" the current directory before invoking your groovy script/app.
By the way, Having a startup cmd has been much more helpful than I'd thought it would be--It makes your environment constant and allows you to more easily deploy your "Scripts" to other machines. I even have mine compile my scripts to .classes because it turned out to be faster to compile a .groovy to a .class and start the .class with "Java" than it was to just run the script with "groovy"--and usually you can skip the compile step which makes it a LOT faster!
2) For a few small commands, you might write a method like this:
def currentDir = "C:\\"
def exec(command, dir = null) {
"cmd /c cd /d ${dir?:currentDir} && $command".execute().text
}
// Default dir is currentDir
assert exec("dir").endsWith("C:\\>")
// different dir for this command only
assert exec("dir", "c:\\users").endsWith("C:\\users")
// Change default dir
currentDir = "C:\\windows"
assert exec("dir").endsWith("C:\\windows")
it will be slower than "".execute() if "cmd" is not required.
3) Code a small class that maintains an "Open" command shell (I did this once, there is a bit of complexity), but the idea is:
def process="cmd".execute()
def in=process.in
def out=process.out
def err=process.err
Now "in" is an input stream that you could spin off/read from and "out" is an output stream that you can write commands to, keep an eye on "err" to detect errors.
The class should write a command to the output, read the input until the command has completed then return the output to the user.
The problem is detecting when the output of any given command is complete. In general you can detect a "C:..." prompt and assume that this means that the command has finished executing. You could also use a timeout. Both are pretty fallible. You can set that shell's prompt to something unique to make it much less fallible.
The advantage is that this shell can remain open for the entire life of your app and can significantly increase speed since you aren't repeatedly creating "cmd" shells. If you create a class (let's call it "CommandShell") that wraps your Process object then it should be really easy to use:
def cmd=new CommandShell()
println cmd.execute("cd /d c:\\")
println cmd.execute("dir") // Will be the dir of c:\
I wrote a groovy class like this once, it's a lot of experimenting and your instance can be trashed by commands like "exit" but it's possible.
you can wrap it up in a dir block.
eg :
dir('yourdirectory') {
codeblock
}

XulRunner exit code

I was wondering how someone can specify an exit code when shutting a XULRunner application.
I currently use nsIAppStartup.quit() described in MDC nsIAppStartup reference to shutdown the application, but I can't figure out how to specify a process exit code.
The application is launched from a shell script and this exit code is needed to decide if it should be restarted or not.
NOTE : Passing eRestart to the quit function is useless in my situation because restarting depends on factors external to the application (system limits etc.)
Thank you and any help would be appreciated.
A quick look at XRE_main function shows that it will only return a non-zero value in case of errors - and even then the exit code is fixed. If everything succeeds and the application shuts down normally the exit code will be 0, no way to change it. XULRunner isn't really meant to be used in shell scripts, you will have to indicate your result in some other way (e.g. by writing it to a file).

running a .py file, giving it arguments and waiting for its return values

Just like the title says...
Example: I have a file called copy.py. That file wants a path to a file/folder which it will move to another directory and will then return "done" if it successfully moved the file. For some reason I have to run my copy.py file from another python program (it's not given that both files are in the same directory) and wait for copy.py to finish its actions. When it is finished, it should tell me "done" or, let's say, "error", so I know if it actually was successful or not.
Please answer in a way a python beginner can understand...
Often, you can just import the module and call its functionality, but if it's a stand-alone program that expects command-line arguments, etc., then you may want to separate the command-line handling from the functional part of the code, so that you can import and call it as I suggested at the beginning.
Failing that, just treat it like another program:
with os.popen('python copy.py {0} {1}'.format(src, dst)) as copy:
output = copy.readlines()
if 'error' in output:
# Oops...

Resources