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