Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Findstr problem within batch

23 views
Skip to first unread message

MordacNM

unread,
Jul 29, 2003, 10:01:38 AM7/29/03
to
Have the following in a batch file and it doesn't work as I'd like (doesn't
seem to pay attention to the exclude.txt file).

FOR /F "tokens=1 eol=(" %%I in (
'findstr /l /i /v /g:exclude.txt c:\killita\machines.txt') DO (
SET Machine=%%I) & (CALL :PINGIT)
GOTO :EOF

The machines.txt file contains UPPER case characters in the machine names
as well as an end of file line with ( as the starting character on the last
line. All machine names are on separate lines.

If I run the same findstr command line at a cmd prompt, it works fine
(excludes the contents of the exclude.txt file).

If I run the command inside the batch and just echo the results to a file
instead of doing the SET Machine=%%%I, it also works.

Is there something I'm missing here?

Dean Wells [MVP]

unread,
Jul 29, 2003, 10:43:16 AM7/29/03
to
The following script appears to work per your request -

[SCRIPT]
@echo off
for /f "tokens=1 eol=(" %%I in (
'findstr /b /e /i /v /g:c:\exclude.txt c:\machines.txt'
) do (
call :PINGIT "%%I"
)

GOTO :EOF

:PINGIT
>nul ping "%1" -n 1 && echo %1 alive || echo %1 dead
[/SCRIPT]

[MACHINES.TXT]
falcon
odyssey
cube
lasso
paddy
cubelet
(
[/MACHINES.TXT]

[EXCLUDE.TXT]
cube
[/EXCLUDE.TXT]

[RESULTS]
"falcon" alive
"odyssey" alive
"lasso" dead
"paddy" dead
"cubelet" alive
[/RESULTS]

The results are as they should be.

HTH

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"MordacNM" <Mor...@NoSpam.Hotmail.com> wrote in message
news:%23NdGvnd...@TK2MSFTNGP11.phx.gbl...

Ritchie

unread,
Jul 29, 2003, 10:45:39 AM7/29/03
to
"MordacNM" <Mor...@NoSpam.Hotmail.com> wrote in message news:#NdGvndV...@TK2MSFTNGP11.phx.gbl...

> Have the following in a batch file and it doesn't work as I'd like (doesn't
> seem to pay attention to the exclude.txt file).
>
> FOR /F "tokens=1 eol=(" %%I in (
> 'findstr /l /i /v /g:exclude.txt c:\killita\machines.txt') DO (
> SET Machine=%%I) & (CALL :PINGIT)
> GOTO :EOF

I think you mean to do something like:-

1. FOR /F "tokens=1 eol=(" %%I in (
2. 'findstr /l /i /v /g:exclude.txt c:\killita\machines.txt'
3. DO (SET Machine=%%I & CALL :PINGIT)
4. GOTO :EOF

In your example, :pingit is only called once.

--
Ritchie


MordacNM

unread,
Jul 29, 2003, 12:04:22 PM7/29/03
to
"Dean Wells [MVP]" <dwe...@mask.msetechnology.com> wrote in news:OZ2pB
$dVDHA...@tk2msftngp13.phx.gbl:

> The following script appears to work per your request -
>
>

> The results are as they should be.
>
> HTH
>
> Dean
>

Still no joy on this - I tried both alternatives posted (by Dean and
Ritchie)...

Here's the actual batch file I'm working on. Maybe more details will help.
The batch is running a sql query into a file called masterlist.txt. Then
use another findstr command to filter out some machine prefixes (line 20).
and redirect the results of this into machines.txt. Set variable
date1/time1 and use these to record activity (machine count) into
ActivityLog.txt. I want to remove any machines (exclude.txt) from
machines.txt and use these names and run another batch (go.cmd) that uses
PSService restart to restart the service on the machines. I run into
several machines that don't respond to the PSService restart function (they
hang) and thus the PSKill at the end gets rid of these extraneous cmd
windows.

Still cannot get the machine names contained in exclude.txt to NOT be
listed in the machines.txt file that the rest of the batch works off of.

Still puzzled...


1. @ECHO OFF
2. @COLOR 1F
3. @TITLE ITA Re-Start
4.
5. :TOP
6.
7. SETLOCAL
8. CALL :CREATE
9. CALL :SERVTEST
10. CALL :LOOP
11. ENDLOCAL
12.
13. GOTO :EOF

14. :CREATE
15. del masterlist.txt>nul
16. del machines.txt>nul
17. cls
18.
19. isql -S sqlservername -d sqltablename -E -h-1 -Q "SELECT MachineName
FROM vwSQLServerView WHERE LastHelloOn > DATEADD(day, -1,
CURRENT_TIMESTAMP) " -o masterlist.txt
20. findstr /l /i /v "( cre- fitl- ncsc- upds- b- icsc- itl- aserver
dccs50" masterlist.txt > machines.txt
21. FOR /f "tokens=2,3,4 delims=/ " %%I in ('DATE /T') DO (SET date1=%%I-%%
J-%%K)
22. FOR /f "tokens=1,2 delims=:" %%T in ('TIME /T') DO (SET time1=%%T:%%U)
23. FOR /f %%a in ('find /v /c "" ^< machines.txt') DO (SET linecount=%%a)
24. echo %date1% %time1% COUNT = %linecount%>>c:\killita\ActivityLog.txt
25. GOTO :EOF
26.
27.:SERVTEST
28. FOR /F "tokens=1 eol=(" %%I in (
'findstr /b /l /i /v /g:exclude.txt c:\killita\machines.txt') DO (


SET Machine=%%I) & (CALL :PINGIT)

29. GOTO :EOF

30.:PINGIT
31. ping -w 2000 -n 1 %MACHINE% | find " TTL=">nul
32. IF NOT ERRORLEVEL 1 (GOTO :RESTARTSERVICE)
33.
34. :BADPING
35. echo %time1% %machine% Unavailable
36. GOTO :ENDSTAT
37.
38. :RESTARTSERVICE
39. FOR /f "tokens=1,2 delims=:" %%I in ('TIME /T') DO (SET time1=%%I:%%J)
ECHO %time1% %MACHINE%
40. start /min go.cmd
41. GOTO :ENDSTAT
42.
43. :LOOP
44. sleep 30
45. pskill psservice.exe>nul
46. GOTO :TOP
47.
48. :ENDSTAT
49.
50. GOTO :EOF

Ritchie

unread,
Jul 29, 2003, 12:35:47 PM7/29/03
to
"MordacNM" <Mor...@NoSpam.Hotmail.com> wrote in message news:eog$TseVDH...@TK2MSFTNGP12.phx.gbl...

> Still no joy on this - I tried both alternatives posted (by Dean and
> Ritchie)...

My earlier guess made an incorrect statement anyway.

> Still cannot get the machine names contained in exclude.txt to NOT be
> listed in the machines.txt file that the rest of the batch works off of.

Reading the above paragraph literally, then thats because you have no code
to remove the machines listed in "exclude.txt" from "machines.txt". But I
doubt thats what you meant.

Please post the "exclude.txt" and "machines.txt" files.

--
Ritchie


MordacNM

unread,
Jul 29, 2003, 1:06:50 PM7/29/03
to
"Ritchie" <qiournvdlirh...@hotmail.com> wrote in
news:bg67pa$l74ng$1...@ID-156657.news.uni-berlin.de:

Exclude.txt contains a list of "don't touch" machines.

Machines.txt contains the machine listing after filtering out the
unwanted machine prefixes from masterlist.txt. I then wish to process
only the results.

Machines.txt minus Exclude.txt equals final list to work off of.

Now that I look at the machines.txt more closely, I notice there is a
space before each line and four spaces after each machine name -
apparently from SQL query output. Maybe a factor???

Here's a sampling...

machines.txt

ENGR-78PRNFZ
ENGR-78PRNGG
ENGR-78PRNGY
ENGR-78PRNKY

exclude.txt

ENGR-78PRNGG

End result I'm after is:

ENGR-78PRNFZ
ENGR-78PRNGY
ENGR-78PRNKY

Ritchie

unread,
Jul 29, 2003, 1:32:03 PM7/29/03
to
"MordacNM" <Mor...@NoSpam.Hotmail.com> wrote in message news:uNaZOPfV...@tk2msftngp13.phx.gbl...

> Machines.txt minus Exclude.txt equals final list to work off of.
>
> Now that I look at the machines.txt more closely, I notice there is a
> space before each line and four spaces after each machine name -
> apparently from SQL query output. Maybe a factor???

If I run the script below with your posted data, only the machines not
excluded are echoed (and surrounded by square brackets). What happens
if you run this script with the live data?

--
Ritchie

:::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal ENABLEEXTENSIONS
for /f %%a in (
'findstr /l /i /v /g:exclude.txt machines.txt'
) do (set "machine=%%a" & call :pingit)

goto :EOF

:pingit
echo/[%machine%]
:::::::::::::::::::::::::::::::::::::::::::::::::


MordacNM

unread,
Jul 29, 2003, 2:54:02 PM7/29/03
to
"Ritchie" <qiournvdlirh...@hotmail.com> wrote in
news:bg6b2o$gn19q$1...@ID-156657.news.uni-berlin.de:

> If I run the script below with your posted data, only the machines not
> excluded are echoed (and surrounded by square brackets). What happens
> if you run this script with the live data?
>
> --
> Ritchie
>
>:::::::::::::::::::::::::::::::::::::::::::::::::
> @echo off & setlocal ENABLEEXTENSIONS
> for /f %%a in (
> 'findstr /l /i /v /g:exclude.txt machines.txt'
> ) do (set "machine=%%a" & call :pingit)
>
> goto :EOF
>
>:pingit
> echo/[%machine%]
>:::::::::::::::::::::::::::::::::::::::::::::::::
>
>
>

It runs just fine if I have only the code shown above. If I use this
code within the original batch file it still doesn't exclude the machine
names that are contained in the exclude.txt file.

Here's what's in the batch file now...

@echo off & setlocal ENABLEEXTENSIONS

.
.
:SERVTEST


for /f %%a in (
'findstr /l /i /v /g:exclude.txt machines.txt'

) do (set "MACHINE=%%a" & call :pingit)

goto :EOF

:PINGIT


ping -w 2000 -n 1 %MACHINE% | find " TTL=">nul

IF NOT ERRORLEVEL 1 (GOTO :RESTARTSERVICE)

:BADPING
echo %time1% %machine% Unavailable
GOTO :ENDSTAT

:RESTARTSERVICE


FOR /f "tokens=1,2 delims=:" %%I in ('TIME /T') DO (SET time1=%%I:%%J)
ECHO %time1% %MACHINE%

start /min go.cmd
GOTO :ENDSTAT

Ritchie

unread,
Jul 29, 2003, 4:08:39 PM7/29/03
to
"MordacNM" <Mor...@NoSpam.Hotmail.com> wrote in message news:eVMQILgV...@TK2MSFTNGP09.phx.gbl...

> It runs just fine if I have only the code shown above. If I use this
> code within the original batch file it still doesn't exclude the machine
> names that are contained in the exclude.txt file.

Do you have by any chance, more than one copy of exclude.txt? May be the
isql section or something else affects the current working directory. As
a test, specify the full path of exclude.txt, when using FINDSTR

--
Ritchie


MordacNM

unread,
Jul 29, 2003, 4:47:30 PM7/29/03
to
"Ritchie" <qiournvdlirh...@hotmail.com> wrote in
news:bg6k89$l1c0u$1...@ID-156657.news.uni-berlin.de:
>
> Do you have by any chance, more than one copy of exclude.txt? May be
> the isql section or something else affects the current working
> directory. As a test, specify the full path of exclude.txt, when using
> FINDSTR
>
> --
> Ritchie
>
>

I do indeed but they aren't in the path and are the exact same file (I use
this machine exclusion in several other batch files).

Tested and got the same results with the complete path\filename...

At this point, I'm going to modify my SQL view to do the exclusion rather
than trying to do this thru the batch file. Just seem strange that it
won't work within the batch but will outside it.

Thanks a lot for all your suggestions! They're much appreciated.

M Ward

unread,
Jul 29, 2003, 5:14:56 PM7/29/03
to
In article <eVMQILgV...@TK2MSFTNGP09.phx.gbl>,
Mor...@NoSpam.Hotmail.com says...

> It runs just fine if I have only the code shown above. If I use this
> code within the original batch file it still doesn't exclude the machine
> names that are contained in the exclude.txt file.
>

It's a long shot, but you should be aware that the content of each line
in your exclude.txt file must be equal to or a *substring* of the lines
you want to match in the machines.txt. Spaces are important.

For example, if you have 6 spaces at the end of each line in
exclude.txt, but only 4 spaces at the end of each line in machines.txt,
then this does not count as a match.

Other than the missing :ENDSTAT label, I didn't notice any problems with
the code you posted.

--
Marcel

0 new messages