Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Error handling in MSDOS

kralskrals Member Posts: 57
Hi there,
i would like to ask this:
I have a batch file that connects to remote PC and copy some files.
Now,if the remote PC is closed then a Network error occured of course.
can anyone tell me HOW can i get this "Network error"? lets say something like error handling.
I have read some things about this:
IF ERRORLEVEL == 4 GOTO...........

but honestly i dont know how it works.

what i have done is this:
NET USE B: \IPADDRESSc password /USER:username
XCOPY C:TEST.TXT B: /Y
IF ERRORLEVEL == 4 GOTO error
:error
@echo failed
net use B: /d
CD..
type failed.txt>>test.txt
pause

can anyone help me?

Thanks-Jim

Comments

  • poetfreakpoetfreak Member Posts: 68
    : Hi there,
    : i would like to ask this:
    : I have a batch file that connects to remote PC and copy some files.
    : Now,if the remote PC is closed then a Network error occured of course.
    : can anyone tell me HOW can i get this "Network error"? lets say something like error handling.
    : I have read some things about this:
    : IF ERRORLEVEL == 4 GOTO...........
    :
    : but honestly i dont know how it works.
    :
    : what i have done is this:

    NET USE B: \IPADDRESSc password /USER:username
    XCOPY C:TEST.TXT B: /Y
    IF NOT ERRORLEVEL == 1 GOTO allok:
    @echo Your test failed.
    net use B: /d
    CD..
    type failed.txt>>test.txt
    pause
    :allok

    This only works if xcopy returns errorlevels, which it may not I'm not even sure. errorlevel evaluates to true on that or greater, so by asking about 1 you catch all errorlevels except 0 (no error)

    Hope I helped.
  • melissa_may1melissa_may1 Member Posts: 937
    [b][red]This message was edited by melissa_may1 at 2004-4-24 21:14:58[/red][/b][hr]

    Hi Jim:

    You're on the right track here, but a couple of things...

    You're not trapping the network error. The Net Use will fail and display an error if the computer is not available. Then the Xcopy will fail. Then finally, the Net Use B: /d will fail, because there is no B: in use.

    But, XCOPY does return ErrorLevel codes. Here they are:


    0 Files were copied without error.
    1 No files were found to copy.
    2 The user pressed CTRL+C to terminate XCOPY.
    4 Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
    5 Disk write error occurred.

    Here is a modified version of your example, with each possible error defined:

    [code]
    NET USE B: \IPADDRESSc password /USER:username

    XCOPY C:TEST est.bat B: /Y

    IF ERRORLEVEL = 5 GOTO WriteFailed:
    IF ERRORLEVEL = 4 GOTO InitFailed:
    IF ERRORLEVEL = 2 GOTO Terminated:
    IF ERRORLEVEL = 1 GOTO NoFiles:
    IF ERRORLEVEL = 0 GOTO allok:

    :WriteFailed
    @Echo Write Failed!
    GOTO allok:

    :InitFailed
    @Echo Init Failed!
    GOTO allok:

    :Terminated
    @Echo Terminated!
    GOTO allok:

    :NoFiles
    @Echo No Files To Copy!
    GOTO allok:

    :allok
    net use B: /d
    pause
    [/code]

    This might be better to use in testing, since you'll see the condition of every possible Xcopy return code.




    [purple]Melissa[/purple]





Sign In or Register to comment.