Page 1 of 1

ExitCode in JScript != ErrorLevel in Batch

Posted: 05 Sep 2016 14:18
by Sponge Belly
Hello All! :)

I wanted to compare 2 files using JScript to see if they were the same or not. I thought the Exec method’s ExitCode property would return 0 for yes, and 1 for no. But it always returns 0: :(

Code: Select all

@if (@X==@Y) @then
@echo off & setlocal enableextensions disabledelayedexpansion

cscript //nologo //e:jscript "%~dpf0" "%~dpf1" "%~dpf2"

endlocal & exit /b 0

@end // JScript

var inFile1 = WSH.Arguments(0).replace(/\\/g, '\\\\'),
inFile2 = WSH.Arguments(1).replace(/\\/g, '\\\\'),
WshShell = WSH.CreateObject('WScript.Shell'),
fileCmp = WshShell.Exec('%ComSpec% /d /a /s /c "fc /b "'+
inFile1+'" "'+inFile2+'" 2>&1 >nul"');
WSH.Echo(fileCmp.ExitCode);
WSH.Quit(0);


A workaround is to change the last 3 lines as follows:

Code: Select all

fileCmp = WshShell.Exec('%ComSpec% /d /a /s /c "fc /b "'+
inFile1+'" "'+inFile2+'" 2>&1 >nul&&echo(0||echo(1"');
WSH.Echo(fileCmp.StdOut.ReadLine());
WSH.Quit(0);


Not very elegant, but it will have to do… unless a DosTips expert would care to enlighten me! ;)

- SB

Re: ExitCode in JScript != ErrorLevel in Batch

Posted: 05 Sep 2016 16:06
by aGerman
Normally I would have expected that you have to wait until the Status is 1. (see https://msdn.microsoft.com/en-us/library/443b45a5(v=vs.84).aspx)
But for whatever reason this ends up in an infinite loop.
To work around it you can read the stdout. If it is finished the right exitCode is available as well.

Code: Select all

:: ...

@end // JScript

var inFile1 = WSH.Arguments(0),
    inFile2 = WSH.Arguments(1),
    WshShell = WSH.CreateObject('WScript.Shell'),
    fileCmp = WshShell.Exec('fc /b "' + inFile1 + '" "' + inFile2 + '"');
fileCmp.stdOut.readAll();
WSH.Echo(fileCmp.exitCode);
WSH.Quit(0);

As you can see you don't have to double the backslashes (because that would be only required for string literals) and there is no need to call cmd.exe because the Exec method already runs a new instance of cmd.exe.

But why do you need JScript in that case? Why don't you just call FC and then return the errorlevel?

Steffen

Re: ExitCode in JScript != ErrorLevel in Batch

Posted: 06 Sep 2016 04:19
by Sponge Belly
Hi Steffen! :)

Thanks for your helpful answer… and not pointing out the glaring error in my definition of inFile2 (now corrected). :oops:

It didn’t occur to me that Exec runs commands asynchronously and that I had to wait for fc to finish. I did read the documentation, I just didn’t absorb it. Maybe it’s time I had my wits resharpened…

Anyways, I want to ensure that a file is 7-bit ASCII. So I read it in using ADODB.Stream, force it to US-ASCII, write it out to a temporary file using OpenTextFile, and run fc /b on the two files. If they're the same, input file is 7-bit ASCII. Otherwise, please try again. ;)

- SB

Re: ExitCode in JScript != ErrorLevel in Batch

Posted: 06 Sep 2016 09:17
by penpen
aGerman wrote:Normally I would have expected that you have to wait until the Status is 1. (see https://msdn.microsoft.com/en-us/library/443b45a5(v=vs.84).aspx)
But for whatever reason this ends up in an infinite loop.
If the stdout buffer is full the application (fc) is blocked, so the js loop is endless.
The same should happen if the stderr buffer is full.

penpen

Re: ExitCode in JScript != ErrorLevel in Batch

Posted: 06 Sep 2016 12:14
by aGerman
penpen wrote:If the stdout buffer is full the application (fc) is blocked, so the js loop is endless.
The same should happen if the stderr buffer is full.

Thanks penpen :)
I was tearing my hair because I didn't understand why I was able to read the stdout but unable to simply wait until FC returned. Now I understand it even if I wonder why there wasn't automatically reallocated more space. That's a weird design :?

Steffen