Jump to content

Recommended Posts

Posted (edited)

I have a problem. I have wrote a script that gets an IP address from Nslookup and passes it to DelProf2.exe.

However, I obviously need some error handling in the script so I can influence the flow of the batch file as it is run (if something goes wrong GOTO function name).

I'd like to control the flow upon Nslookup succeeding or failing.

 

How do I access the exit status and assign it to a variable?

 

Thanks in advance. This is my first post. This has been doing my head in!

 

Note: I will post up finished bat file upon completion so you can all benefit if needed.

Edited by psutton00
Posted (edited)
How do I access the exit status and assign it to a variable?

Microsoft chose not to include something as useful as an exit code for NSLOOKUP. What a disgrace.

 

Try this. It's very nasty code that uses a temp file but this code should do the trick nonetheless.

 

@echo off

nslookup DOMAIN_TO_TEST 2> %temp%\temp.txt


for /f "tokens=3 delims= "  %%a in ('find /c /i "Non-existent domain" %temp%\temp.txt') do (
if %%a NEQ 0 (set Found=false) else (set Found=true)
)


if %found% == true (
echo The domain exists
) else (
echo The domain does not exist
)

Edited by jinnantonnixx
Posted

Typical Microshite.

 

Thanks for the reply. I have been waiting all day! :)

 

Is there any way to use the FIND command to search the output in CMD rather than use a temp file?

Posted (edited)

FIND isn't too clever, either, you'll be shocked to learn.

 

The trouble is that an error from NSLOOKUP is sent through a different stream which isn't handled correctly by piping to FIND. It just doesn't work, which is why I had to use a file. I chose to pipe the STDERR output (which is the "2>" bit) to a file then run FIND against that looking for a specific message.

 

Unless another 'geeker with a 3rd Dan in script-fu knows of a better way?

 

SS64 is an tremendous site for all your scripty requisites.

Command Redirection, Pipes | SS64.com

Edited by jinnantonnixx
Posted

Basically we need some legal legislation to be implemented to hold programmers responsible for there code. IE. if they don't put a god damn exit status in there code they are shot!

I am a student on placement and it was one the first I learnt at A-Level! Never write a program that doesn't tell other programs it has ran successfully or failed!

Posted (edited)
I will give it a bash, no pun intended, and let you know. It may take a while as I am having to change a fair amount of my code to adjust. Thanks for your support. Edited by psutton00
Grammatical
Posted (edited)
I will give it a bash, no pun intended, and let you know. It may take a while as I am having to change a fair amount of my code to adjust. Thanks for your support.

 

Just put the NSLOOKUP check routine it in another file and use a 'call' to treat it as a function. If you use a SET command to set the variables, then it's a way of getting values visible between script routines. Messy, but in this case DOS batch scripting is very primitive. I'd probably try it in Powershell. That said, I find Powershell frustrating as it's an object-oriented scripting langauge, but the syntax is verb-based, not object-based! Gah!

 

DOS Batch - Function Tutorial

Edited by jinnantonnixx

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...