ExitCode in JScript != ErrorLevel in Batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

ExitCode in JScript != ErrorLevel in Batch

#1 Post by Sponge Belly » 05 Sep 2016 14:18

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
Last edited by Sponge Belly on 06 Sep 2016 03:09, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: ExitCode in JScript != ErrorLevel in Batch

#2 Post by aGerman » 05 Sep 2016 16:06

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

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: ExitCode in JScript != ErrorLevel in Batch

#3 Post by Sponge Belly » 06 Sep 2016 04:19

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: ExitCode in JScript != ErrorLevel in Batch

#4 Post by penpen » 06 Sep 2016 09:17

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: ExitCode in JScript != ErrorLevel in Batch

#5 Post by aGerman » 06 Sep 2016 12:14

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

Post Reply