TNSPING Multiple hosts

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pedrohgl18
Posts: 2
Joined: 29 Aug 2018 13:17

TNSPING Multiple hosts

#1 Post by pedrohgl18 » 29 Aug 2018 13:20

im trying to create a .bat to check multiple hosts, the code below doent work and idk why. please help me =)

@echo off
set fnm=c:\scripts\databases.txt
set lnm=c:\scripts\results.txt

if exist %fnm% goto Label1

echo.
echo Cannot find %fnm%
echo.
Pause
goto :eof

:Label1
echo Ping Test on %date% at %time% > %lnm%
echo ================================================= >> %lnm%
echo.
for /f %%i in (%fnm%) do call :Sub %%i
echo.
echo ================================================= >> %lnm%
echo Ping Test ENDED on %date% at %time% >> %lnm%
echo ... now exiting
goto :eof

:Sub
echo Testing %1
set state=OK
tnsping 1 %1
if errorlevel 1 set state=Down
echo %1 is %state% >> %lnm%

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: TNSPING Multiple hosts

#2 Post by penpen » 30 Aug 2018 06:21

It probably would help if you describe the error in more detail than just "the code below doent work".

penpen

pedrohgl18
Posts: 2
Joined: 29 Aug 2018 13:17

Re: TNSPING Multiple hosts

#3 Post by pedrohgl18 » 30 Aug 2018 06:27

Keeps in looping at Testing 1.
Attachments
code1.PNG
code1.PNG (53.69 KiB) Viewed 2801 times

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: TNSPING Multiple hosts

#4 Post by penpen » 30 Aug 2018 15:22

I guess the reason is that your "c:\scripts\databases.txt" contains:

Code: Select all

bilad and maybe some additional unknown text
1 some other text
1 text text
1 text
1 *
1
1 ...
Beside we don't know what the content of the databas file really is (if it matches the output without the "Testing " part, then the output is completely correct) the main issue might be unwanted breaking of words/lines by some delimiters (characters that breaks a command line in statement/program name and arguments).

You should notice that the default delimiters of a 'for/f' loop are space and tabulator.
You should also use usebackq, to enable delimiters in filenames (although in your case the filename doesn't contain any delimiter like space).
In addition you should also use doublequotes around a parameter (again to prevent against such poisonous characters == delimiters).
To avoid unwanted breaking of parameters (or filenames) you should use something like:

Code: Select all

for /f "usebackq tokens=* delims=" ("%fnm%") do  do call :Sub %%~i
(That is just an exemplary line; you should also update any other such line.)

Sidenote:
In the future i recommend you to give at least a complete example, including the contents of all related (sample) files (using dummy data), give the expected output and the actual output.
Above i actually guessed the most probable issues, so it hopefully should help you, but it might also be that it doesn't help you.


penpen

Post Reply