Re: Findstr problem within batch

  • Thread starter Thread starter MordacNM
  • Start date Start date
M

MordacNM

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
 
MordacNM said:
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%]
:::::::::::::::::::::::::::::::::::::::::::::::::
 
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
 
MordacNM said:
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 said:
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

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.
 
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.
 
Back
Top