Computing.Net > Forums > Programming > Batch Script Weirdness

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch Script Weirdness

Reply to Message Icon

Name: Pagey
Date: July 17, 2008 at 03:34:40 Pacific
OS: Windows XP/2000
CPU/Ram: N/A
Product: N/A
Comment:

Hello,

I'm homping someone here can help me with a Batch file problem I am having. I have googled about a lot with no answer.

Here is the code

[CODE]
@echo on
SetLocal EnableDelayedExpansion
FOR /F %%i IN (Stores.ini) DO (
SET SERVER=%%i
CLS
ECHO Connecting to %%i ...
REM ########PING AND MAPPING########
PING -n 1 %%i >nul
IF !ERRORLEVEL! == 1 GOTO PINGFAIL
NET USE /D V: /Y >nul
NET USE * \\%%i\g$ /USER:DEBENHAMS\Administrator leaving
IF !ERRORLEVEL! == 2 GOTO LKFAIL
REM ################

REM DFS
REM ########DFS FILE########
ECHO Checking for DFS file...
IF NOT EXIST V:\USR\OSCAR\DFS\osc%date:~0,2%%date:~3,2%%date:~6,4%.txt GOTO FAIL1
REM ################

REM OML
REM ########OML FILE########
ECHO Checking for OML File...
IF NOT EXIST V:\USR\OSCAR\LOGS\%date:~6,4%%date:~3,2%%date:~0,2%.OML GOTO FAIL2

ECHO Checking for successfull SOD in %date:~6,4%%date:~3,2%%date:~0,2%.OML
FINDSTR /I "StartOfDaySuccess|YES" V:\USR\OSCAR\LOGS\%date:~6,4%%date:~3,2%%date:~0,2%.OML
IF NOT !ERRORLEVEL! == 0 GOTO SODFAIL
REM ################

REM EJ
REM ########EJ FILE########
ECHO Checking for EJRL File...
IF NOT EXIST V:\USR\OSCAR\WORK\%date:~0,2%%date:~3,2%%date:~6,4%.ej GOTO FAIL3
GOTO SUCCESSFINISH
REM ################

:PINGFAIL
ECHO %SERVER% Is off the network. >>Results.txt
GOTO FINISHFAIL

:LKFAIL
ECHO %SERVER% Unable to map drive. Check Lifekeeper drive permissions and Share access. >>Results.txt
GOTO EOF

:SODFAIL
ECHO %SERVER% OML File exists, but successfull SOD flag not present. Check Errlog. >>Results.txt
GOTO FINISHFAIL

:FAIL1
ECHO %SERVER% OSC File missing for todays date. Check Errlog and OML file >>Results.txt
GOTO FINISHFAIL

:FAIL2
ECHO %SERVER% OML file does not exist. Check Errlog >>Results.txt
GOTO FINISHFAIL

:FAIL3
ECHO %SERVER% EJ file does not exist. Check Errlog and OML file >>Results.txt
GOTO FINISHFAIL

:SUCCESSFINISH
ECHO %SERVER% Start Of Day SUCCESS >>Results.txt

:FINISHFAIL
ECHO Finished Checking %SERVER%
)
EndLocal
[/CODE]

Basically, the FOR loop fails. It will only run through once. If i remove the label :SUCCESSFINISH it runs all the way through, but obviously outputs all of the other errors if the checks complete sucessfully.

I'm stumped, unless you can't have a GOTO :LABEL at the end of a FOR loop?

Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: July 17, 2008 at 04:10:47 Pacific
Reply:

FORs and GOTOs don't get along. Usually it won't loop, sometimes it will. Don't mix them together.


0

Response Number 2
Name: Pagey
Date: July 17, 2008 at 04:36:12 Pacific
Reply:

What would you suggest I do instead without calling other BATCH files, etc?

Are there other LOOPS available in DOS, rather than just for ?


0

Response Number 3
Name: Mechanix2Go
Date: July 17, 2008 at 05:45:24 Pacific
Reply:

Razor2.3 is right about goto with for.

There is no DOS in NT. You could use COMMAND.COM which comes with NT. But that's even more restrictive.

Note that PING returns errorlevel 0 regardless. So try something like this:

::=======================
@echo off
setLocal EnableDelayedExpansion

if %1'==' echo which IP? && goto :eof
set IP=%1

for /f "tokens=* delims= " %%a in ("!IP!") do (
ping %%a | find "Reply" > nul
call :!errorlevel!
)
goto :eof

:0
echo !IP! is alive
goto :eof

:1
echo nobody home
goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: Razor2.3
Date: July 17, 2008 at 05:55:16 Pacific
Reply:

What would you suggest I do instead without calling other BATCH files, etc?
Have your batch CALL the rest of your script as a subfunction.

Are there other LOOPS available in DOS, rather than just for ?
There's the classic :loop . . . GOTO loop


0

Response Number 5
Name: Pagey
Date: July 18, 2008 at 05:08:00 Pacific
Reply:

Have your batch CALL the rest of your script as a subfunction.

- But if you call one of the subfunctions, the script will then continue through the script, reporting out all of the other :labels

Can you think of any other way of doing my script?

Thanks for the replies so far.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: July 19, 2008 at 03:57:16 Pacific
Reply:

reread #3

You need to end each :sub with goto :eof

Otherwise it just bumbles along.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: Pagey
Date: July 21, 2008 at 04:00:29 Pacific
Reply:

But if I use a goto :eof will it not go beyond the for loop and not loop around?


0

Response Number 8
Name: Mechanix2Go
Date: July 21, 2008 at 04:10:59 Pacific
Reply:

After the CALL it returns to the FOR loop.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 9
Name: Pagey
Date: July 21, 2008 at 04:22:06 Pacific
Reply:

It doesn't appear to be doing so. It works fine for the first entry in the Stores.ini (List of servers) but then just stops. Here is the code with the GOTO :EOF entries.

@echo on
SetLocal EnableDelayedExpansion
FOR /F %%i IN (Stores.ini) DO (
SET SERVER=%%i
CLS
ECHO Connecting to %%i ...
REM ##############PING AND MAPPING#######
PING -n 1 %%i >nul
IF !ERRORLEVEL! == 1 GOTO PINGFAIL
NET USE /D V: /Y >nul
NET USE V: \\%%i\g$ /USER:DEBENHAMS\Administrator leaving
IF !ERRORLEVEL! == 2 GOTO LKFAIL
REM ############################

REM DFS
REM #######DFS FILE#######
ECHO Checking for DFS file...
IF NOT EXIST V:\USR\OSCAR\DFS\osc%date:~0,2%%date:~3,2%%date:~6,4%.txt GOTO FAIL1
REM ############################

REM OML
REM #######OML FILE#######
ECHO Checking for OML File...
IF NOT EXIST V:\USR\OSCAR\LOGS\%date:~6,4%%date:~3,2%%date:~0,2%.OML GOTO FAIL2

ECHO Checking for successfull SOD in %date:~6,4%%date:~3,2%%date:~0,2%.OML
FINDSTR /I "StartOfDaySuccess|YES" V:\USR\OSCAR\LOGS\%date:~6,4%%date:~3,2%%date:~0,2%.OML
IF NOT !ERRORLEVEL! == 0 GOTO SODFAIL
REM ############################

REM EJ
REM #######EJ FILE#######
ECHO Checking for EJRL File...
IF NOT EXIST V:\USR\OSCAR\WORK\%date:~0,2%%date:~3,2%%date:~6,4%.ej GOTO FAIL3
GOTO SUCCESSFINISH
REM ############################

:PINGFAIL
ECHO %SERVER% Is off the network. >>Results.txt
GOTO :EOF

:LKFAIL
ECHO %SERVER% Unable to map drive. Check Lifekeeper drive permissions and Share access. >>Results.txt
GOTO :EOF

:SODFAIL
ECHO %SERVER% OML File exists, but successfull SOD flag not present. Check Errlog. >>Results.txt
GOTO :EOF

:FAIL1
ECHO %SERVER% OSC File missing for todays date. Check Errlog and OML file >>Results.txt
GOTO :EOF

FAIL2:
ECHO %SERVER% OML file does not exist. Check Errlog >>Results.txt
GOTO :EOF

FAIL3:
ECHO %SERVER% EJ file does not exist. Check Errlog and OML file >>Results.txt
GOTO :EOF

:SUCCESSFINISH
ECHO %SERVER% Start Of Day SUCCESS >>Results.txt
GOTO :EOF

:FINISHFAIL
ECHO Finished Checking %SERVER%
GOTO :EOF
)
EndLocal


0

Response Number 10
Name: Mechanix2Go
Date: July 21, 2008 at 05:43:45 Pacific
Reply:

re-read #3


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 11
Name: Pagey
Date: July 21, 2008 at 05:49:12 Pacific
Reply:

Post #3 is referring to my Ping function (which ERRORCHECKS fine, it outputs different !errorlevel!'s dependant on what happens.

Maybe I am not unerstanding this correctly, please could you explain it so me more, or perhaps adapt my script partly?

Thanks.


0

Response Number 12
Name: Mechanix2Go
Date: July 21, 2008 at 06:01:19 Pacific
Reply:

My PING returns 1 for a non-existent IP, but 0 for an unresponsive one.

I'd be surprised if the PING in XP is different from PING in W2K.

Here's the MD5 hash for W2K PING.EXE:

f837281900f3793f6d38d5994a3ed319


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 13
Name: Pagey
Date: July 21, 2008 at 06:19:24 Pacific
Reply:

All devices that will be in Stores.ini will all be ones taken from DNS.

The ping feature is not my issue, the fact that the GOTO's do not work within my FOR loop doesn work is. I need to know how to resolve this with another method.

Whoever can give me an alternate method, could you please adapt part of my script so I can see how it would work.


0

Response Number 14
Name: Mechanix2Go
Date: July 21, 2008 at 06:37:31 Pacific
Reply:

The ) ending your FOR is near the bottom, just before EndLocal.

I think you need it after this line:

IF !ERRORLEVEL! == 2 GOTO LKFAIL


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 15
Name: Pagey
Date: July 21, 2008 at 06:41:09 Pacific
Reply:

But then the rest of the script wouldn't work for the different devices in Stores.ini It would render everything after that pointless and not included within the loop. I want ALL of the script to work for EVERYTHING in Stores.ini


0

Response Number 16
Name: Mechanix2Go
Date: July 21, 2008 at 06:48:40 Pacific
Reply:

Then I think you want to CALL the :sub instead of GOTO.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 17
Name: Pagey
Date: July 21, 2008 at 07:01:34 Pacific
Reply:

CALL doesn't work, because after the CALL it carries on from the :sub instead of calling the :SUB and carrying on.

If i move the order of the :sUB to after each procedure it will run it, regardless.

Any other suggestions?


0

Response Number 18
Name: Razor2.3
Date: July 21, 2008 at 17:32:32 Pacific
Reply:

CALL doesn't work, because after the CALL it carries on from the :sub instead of calling the :SUB and carrying on.
That's where GOTO :EOF comes into play.

If i move the order of the :sUB to after each procedure it will run it, regardless.
I have no idea what you're trying to say.


0

Response Number 19
Name: Pagey
Date: July 22, 2008 at 01:49:38 Pacific
Reply:

GOTO :EOF doesn't work, you can see my code above and test it if you wish. It doesn't work because it goest to the end of the script, and doesn't loop again past the first entry

CALL call's the :SUB and then continues through the script, it doesn't return to where the CALL is and continue. EG. If you see where the ping feature is, it would call the :PINGFAIL SUB and then continue through the rest of the script.

Any other suggestions?


0

Response Number 20
Name: Razor2.3
Date: July 22, 2008 at 01:59:46 Pacific
Reply:

Yeah, use both CALL and GOTO :EOF, just like we've been telling you to for the past, what, 5 days now?


0

Response Number 21
Name: Mechanix2Go
Date: July 22, 2008 at 02:15:28 Pacific
Reply:

I chopped out much of the REM and the %date% stuff. I don't have a network so I can't get a 'success' but hopefully this will give you some ideas on how to proceed.


::=========================
@echo off
SetLocal EnableDelayedExpansion
FOR /F %%i IN (Stores.ini) DO (
SET SERVER=%%i
ECHO Connecting to %%i ...
PING -n 1 %%i >nul
IF !ERRORLEVEL! == 1 call :PINGFAIL
NET USE /D V: /Y >nul
NET USE V: \\%%i\g$ /USER:DEBENHAMS\Administrator leaving
IF !ERRORLEVEL! == 2 call :LKFAIL

IF NOT EXIST zzzz call :FAIL1

call :SUCCESSFINISH

)
goto :eof

:PINGFAIL
ECHO %SERVER% Is off the network. >>Results.txt
GOTO :EOF

:LKFAIL
ECHO %SERVER% Unable to map drive. Check Lifekeeper drive permissions and Share access. >>Results.txt
GOTO :EOF

:SODFAIL
ECHO %SERVER% OML File exists, but successfull SOD flag not present. Check Errlog. >>Results.txt
GOTO :EOF

:FAIL1
ECHO %SERVER% OSC File missing for todays date. Check Errlog and OML file >>Results.txt
GOTO :EOF

:FAIL2
ECHO %SERVER% OML file does not exist. Check Errlog >>Results.txt
GOTO :EOF

:FAIL3
ECHO %SERVER% EJ file does not exist. Check Errlog and OML file >>Results.txt
GOTO :EOF

:SUCCESSFINISH
ECHO %SERVER% Start Of Day SUCCESS >>Results.txt
GOTO :EOF

:FINISHFAIL
ECHO Finished Checking %SERVER%
GOTO :EOF


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch Script Weirdness

batch script to parse filenames www.computing.net/answers/programming/batch-script-to-parse-filenames/15286.html

Batch Script Help Needed www.computing.net/answers/programming/batch-script-help-needed/14475.html

batch script www.computing.net/answers/programming/batch-script/14086.html