0

Is there a way to read the line of code that raised the errorlevel from within a batch file after its execution? (I'm afraid there isn't).

For example the command net use H: \\MyComputer\sharename02 would result in System error 85 has occurred. The local device name is already in use. In this case drive H: is not available and the errorlevel is no longer 0. My aim is to fetch that command to modify it, somewhat like so:

@echo off
set _counter=0

net use F: \\MyComputer\sharename01 /persistent:No 
IF %ERRORLEVEL% NEQ 0 goto manip
_counter=_counter+1

:next2
net use H: \\MyComputer\sharename02 /persistent:No
IF %ERRORLEVEL% NEQ 0 goto manip
_counter=_counter+1

:next3
net use I: \\MyComputer\sharename03 /persistent:No
IF %ERRORLEVEL% NEQ 0 goto manip
_counter=_counter+1

IF %ERRORLEVEL% EQU 0 goto end

:manip
(do something with...
_variable="\\MyComputer\sharenameXY"
...)
_counter=_counter+1
if _counter==3 (goto end)
else (goto next%_counter%)

:end

The section :manip would work with the command net use H: \\MyComputer\sharename02 that got stored in _variable.

I am aware that this kind of problem isn't really suited for Batch processing and would probably be easier solved using VBscript instead of batch.

Hope there is a solution otherwise I would need to rewrite all that I already have into VBscript or Powershell.

1 Answer 1

1

No, there isn't. However, you can use various kinds of loops:

call :map_share Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
echo I got %letter%!
goto :eof

:map_share
if "%~1"=="" goto :eof
net use %1: \\MyComputer\sharename02 /persistent:No || (
    shift
    goto :map_share
)
set letter=%1
goto :eof
2
  • As assumed in my question, I was afraid there isn't. So your answer is correct and I accept it as such. The code provided works as well. Thanks.
    – snahl
    Commented Nov 4, 2015 at 1:53
  • Only now it becomes apparent to me how very compact and thus useful your code is.
    – snahl
    Commented Nov 9, 2015 at 22:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .