Verify user-entered paths (unexpected ERRORLEVEL-behaviour)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Verify user-entered paths (unexpected ERRORLEVEL-behaviour)

#1 Post by ©opy[it]®ight » 19 Sep 2012 10:44

Hi all (how's everyone doing?) :-)

It's been a while since my last visit, but i've been busy (as always) and stumbeled upon a problem i can't tackle.

I need to verify a user-given path that leads to Javaw.exe.

The code i have right now looks like this:

Code: Select all

@ECHO OFF
MODE CON COLS=80 LINES=25

SETLOCAL EnableExtensions EnableDelayedExpansion

TITLE Path ^& Application ERRORLEVEL-tester v0.1 (20092012-0025)

IF NOT EXIST PathAppTest (
  CD.>PathAppTest 2>NUL
)

:START
CLS

echo.
SET /P TESTER=Te testen pad/programma:

IF DEFINED TESTER (
  ECHO !TESTER!>PathAppTest 2>NUL
  goto CHECK
)

IF "!TESTER!"=="" (
  CD.
  IF EXIST PathAppTest (SET /P TESTER=<PathAppTest) ELSE (goto START)
)

:CHECK
IF !TESTER:~-1!==\ (
  SET TESTER=!TESTER:~0,-1!
  goto CHECK
)

echo.
ECHO Pad/programma om te testen: "!TESTER!"

echo.
"!TESTER!"

echo.
ECHO ERRORLEVEL %ERRORLEVEL%

echo.
PAUSE>NUL|SET /P =

SET TESTER=

:: RESET ERRORLEVEL
CD.
goto START


It works most of the time, but whenever i enter a colon as path, it shows ERRORLEVEL 0 (which i find awkward).

Even when typing a colon in the DOS promt it shows nothing but a new prompt:

Code: Select all

C:\Users\admin>:\javaw.exe & ECHO ERRORLEVEL %ERRORLEVEL%
C:\Users\admin>


A semi-colon gives a whole different result (disappearing semi-colon):

Code: Select all

C:\Users\admin>;\javaw.exe & ECHO ERRORLEVEL %ERRORLEVEL%
\javaw.exe wordt niet herkend als een interne
of externe opdracht, programma of batchbestand.
ERRORLEVEL 3


Other examples:

Code: Select all

C:\Users\admin>javaw.exe & ECHO %ERRORLEVEL%
javaw.exe wordt niet herkend als een interne
of externe opdracht, programma of batchbestand.
ERRORLEVEL 9009

C:\Users\admin>\\javaw.exe & ECHO ERRORLEVEL %ERRORLEVEL%
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
ERRORLEVEL 123

C:\Users\admin>/\javaw.exe & ECHO ERRORLEVEL %ERRORLEVEL%
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
ERRORLEVEL 123

C:\Users\admin>'\javaw.exe & ECHO ERRORLEVEL %ERRORLEVEL%
De syntaxis van de bestandsnaam, mapnaam of volumenaam is onjuist.
ERRORLEVEL 3


(most characters i tested showed an ERRORLEVEL 3)

My question: Is there a way to somehow work around this problem, as the code is a pretty crucial part of my script (verify/test a user-given java-path and use the path on a successful test).

Thank you for reading/thinking along with me :-)
Last edited by ©opy[it]®ight on 19 Sep 2012 16:35, edited 4 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Verifying user-given paths (unexpected ERRORLEVEL-behavi

#2 Post by foxidrive » 19 Sep 2012 11:41

Is it a leading colon that you have problems with?
Why not test for it and remove it?

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#3 Post by ©opy[it]®ight » 19 Sep 2012 15:24

I initially checked for a trailing backslash, as more occurences of a trailing backslash would result in different ERRORLEVEL than i expected.

There are more characters but i'm not sure how to (correctly) error-handle/escape them all. This has always been a problem for me.

edit: There's more errorlevel-related stuff i encountered afterwards.

The batch-program i continuously work on consists of many PAUSE>NUL|SET /P = instances, to filter out (un)intensional special-key presses that cause a later SET /P to be skipped.

But since i also often check the ERRORLEVEL, i notice that some commands that are run, report an ERRORLEVEL 1 (even java.exe and javaw.exe return an ERRORLEVEL 1 by default)

But apparently, the errorlevel also gets raised whenever SET/P gets a blank/empty input.

Is there a(nother) way to reset the ERRORLEVEL, besides putting CD. after each PAUSE>NUL|SET/P or accepting ERRORLEVEL 1 as the result for blank/empty input?

Thanks.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#4 Post by ©opy[it]®ight » 20 Sep 2012 07:58

I found another unusual/unexpected behaviour i can't explain, but when i enter a non-existing filename, it doesn't give the (correct) errorlevel (straight away).

I'm testing this using the following script(code):

Code: Select all

@ECHO OFF
MODE CON COLS=80 LINES=25

SETLOCAL EnableExtensions EnableDelayedExpansion

SET VERSION=20092012-1556
SET TITLE=-=^> copyitright's Path ^& Application ERRORLEVEL-tester ^| Build !VERSION! ^<=-
TITLE !TITLE!

:START
CLS
CD.
echo.
ECHO !TITLE!
echo.
echo.
echo.

ECHO Pad en/of programmanaam om te testen,
SET /P TESTER=Enter om een vorige test te herhalen:

IF DEFINED TESTER (
  ECHO !TESTER!>PathAppTest 2>NUL
  goto CHECK
)

IF "!TESTER!"=="" (
  IF EXIST PathAppTest (SET /P TESTER=<PathAppTest) ELSE (goto START)
)

:CHECK
IF !TESTER:~-1!==\ (
  SET TESTER=!TESTER:~0,-1!
  goto CHECK
)

CLS
echo.
ECHO !TITLE!
echo.
echo.
echo.
ECHO Te testen pad en/of programma: "!TESTER!"

echo.
echo.
ECHO resultaat: "!TESTER!"

echo.
ECHO ERRORLEVEL %ERRORLEVEL%

echo.
echo.
echo.
echo.
echo.
echo.
PAUSE>NUL|SET /P =Druk op een toets om een ander pad/programma te testen...

SET TESTER=
goto START

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#5 Post by foxidrive » 20 Sep 2012 08:12

Perhaps you could search the system drive for javaw.exe and then use that file and location.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#6 Post by ©opy[it]®ight » 20 Sep 2012 08:26

foxidrive: The script i'm working on is used to test/run console java-applications and is used by others as well.

Because the script is used on different systems and Java isn't always installed (in the same/system32 folder), errors occur.

I find it a challenge to create a script that first looks if an existing user-given path is found. If one exists it tests (validates) the path by trying the
java.exe command. If it fails, it tries to run the java.exe command without the path (to see if Java is in the Windows PATH Environment Variable).

If this also fails, the program asks the user to enter a different path. If that fails as well, the user is advised to check their input or installation of Java ;)

i.e. User-entered entries are always used first, the Windows PATH is used as fall-back system.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#7 Post by foxidrive » 20 Sep 2012 08:46

You didn't respond to the question I posed.

You can search for java.exe or javaw.exe or any other file you choose, and then use that path.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#8 Post by ©opy[it]®ight » 20 Sep 2012 08:58

Sorry.. Searching drives can take long. I only want to validate user-entered paths or search/use entries in the Windows PATH ;)

It also doesn't make me understand why i get unexpected ERRORLEVEL results.
Last edited by ©opy[it]®ight on 20 Sep 2012 09:06, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#9 Post by foxidrive » 20 Sep 2012 09:06

©opy[it]®ight wrote:Sorry.. Searching drives can take long.


Try this. Add a second search if you want to search 64 bit machines.

It took less than 1 second on my SSD.

Code: Select all

@echo off
time /t
dir "c:\program files\java.exe" /b /s
time /t
pause

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#10 Post by ©opy[it]®ight » 20 Sep 2012 09:24

Of course that will only work if Java is installed to the default location. Not all of my users (including me) have Java installed there.
Some of them don't even know where Java is (pre)installed.

For that reason i've created a script, but because i'm getting unexpected/the wrong ERRORLEVEL results i'm trying to get some advise.

If i was the only user, there was no need for a script or topic =)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#11 Post by foxidrive » 20 Sep 2012 09:44

©opy[it]®ight wrote:Of course that will only work if Java is installed to the default location. Not all of my users (including me) have Java installed there.
Some of them don't even know where Java is (pre)installed.


No, it will work for any system that has java installed somewhere in program files. 64 bit or 32 bit can be handled.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#12 Post by ©opy[it]®ight » 20 Sep 2012 10:07

ok, back on-topic:

I've made some progress with my script in regards to getting the expected errorlevels.
The mistake was that i ECHOed the path, instead of testing it (facepalm).

The below code works, and it even checks/removes trailing spaces, since those would simply echo the current state of ECHO.
(i know i'm better off using a FOR LOOP, for the sake of code prettyness, but the LOOPS work for now)

Code: Select all

@ECHO OFF
MODE CON COLS=80 LINES=25

SETLOCAL EnableExtensions EnableDelayedExpansion

SET VERSION=20092012-1820
SET TITLE=-=^> copyitright's Path ^& Application ERRORLEVEL-tester ^| Build !VERSION! ^<=-
TITLE !TITLE!

:START
CLS

echo.
ECHO !TITLE!
echo.

IF EXIST PathAppTest (
  SET /P TESTER=<PathAppTest
  IF DEFINED TESTER (ECHO  Voorgaande test: "!TESTER!") ELSE (echo.)
)

echo.
echo.
ECHO Pad en/of programmanaam om te testen,

SET /P TESTER=Enter om een vorige test te herhalen:

:CHECK_SLASH
IF "!TESTER:~-1!"=="\" (
  SET TESTER=!TESTER:~0,-1!
  goto CHECK_SLASH
)

:CHECK_SPACE
IF "!TESTER:~-1!"==" " (
  SET TESTER=!TESTER:~0,-1!
  goto CHECK_SPACE
)

IF NOT "!TESTER!"=="" ECHO !TESTER!>PathAppTest 2>NUL

IF "!TESTER!"=="" (
  IF EXIST PathAppTest (SET /P TESTER=<PathAppTest) ELSE (goto START)
)

CLS
echo.
ECHO !TITLE!

echo.
echo.
echo.
ECHO -=^> Te testen pad en/of programma ^<=-
echo.
ECHO "!TESTER!"

echo.
echo.
REM CD. i'm not sure if i should use CD. here, or somewhere else for that matter
ECHO -=^> resultaat ^<=-
echo.
"!TESTER!"
echo.
echo.
ECHO ERRORLEVEL !ERRORLEVEL!
echo.
echo.
echo.
echo.
echo.
echo.

PAUSE>NUL|SET /P =Druk op een toets om een ander pad/programma te testen...

SET TESTER=
goto START


Back to do some more testing :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#13 Post by foxidrive » 20 Sep 2012 10:21

Maybe someone else will reply. Good luck.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Verify user-entered paths (unexpected ERRORLEVEL-behavio

#14 Post by ©opy[it]®ight » 20 Sep 2012 10:30

hmm, too bad you aren't in the mood to assist me with that ERRORLEVEL-madness :)

Post Reply