Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 FAIL2ECHO 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.

What would you suggest I do instead without calling other BATCH files, etc?
Are there other LOOPS available in DOS, rather than just for ?

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 EnableDelayedExpansionif %1'==' echo which IP? && goto :eof
set IP=%1for /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

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

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.

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

After the CALL it returns to the FOR loop.
=====================================
If at first you don't succeed, you're about average.M2

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 FAIL2ECHO 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 :EOFFAIL2:
ECHO %SERVER% OML file does not exist. Check Errlog >>Results.txt
GOTO :EOFFAIL3:
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

re-read #3
=====================================
If at first you don't succeed, you're about average.M2

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.

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

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.

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

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

Then I think you want to CALL the :sub instead of GOTO.
=====================================
If at first you don't succeed, you're about average.M2

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?

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.

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?

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

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 :LKFAILIF 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

![]() |
![]() |
![]() |

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