B
B
Balkonsky2016-10-08 15:07:38
cmd/bat
Balkonsky, 2016-10-08 15:07:38

How to pass the value of the find command in a .bat file to a variable?

Hello!
The essence of the program:
1. We ping to a remote host and write it to a file
2. We look for 3 words "unreachable" in this file
3. If we find 3 words, then the following commands are executed:

tasklist/v >"C:\%date%_%USERNAME%_CPU_info.txt"
systeminfo >"C:\%date%_%USERNAME%_systeminfo.txt"
netstat -abno >"C:\%date%_%USERNAME%_netstat.txt"
pathping -n 8.8.8.8 >"C:\%date%_%USERNAME%_pathping.txt"

Tell me how to perform a search by passing the search value to a variable for its further comparison with the if statement.
So far, this is implemented like this:
@echo off 
setlocal enabledelayedexpansion
chcp 1251 >nul
:LOOP
echo [%DATE% %TIME%] >> "C:\%date:~-10%_%USERNAME%_logfile"  & ping -n 1 8.8.8..8 | find /i "TTL=">nul >> "C:\%date:~-10%_%USERNAME%_logfile.log"
FIND /C "unreachable" <"C:\%date:~-10%_%USERNAME%_logfile"
if NOT %Здесь ПЕРЕМЕННАЯ из поиска% 3 ( 
echo PING - ok Packet Lost = %errorlevel% 
)else ( 
echo [%DATE% %TIME%] Destination host unreachable >> "C:\%date:~-10%_%USERNAME%_logfile_"
echo NOT OK
tasklist/v >"C:\%date%_%USERNAME%_CPU_info.txt"
systeminfo >"C:\%date%_%USERNAME%_systeminfo.txt"
netstat -abno >"C:\%date%_%USERNAME%_netstat.txt"
pathping -n 8.8.8.8>"C:\%date%_%USERNAME%_pathping.txt"
rem ping -n %pause% 127.0.0.1>nul 2>&1
)
goto LOOP

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Olgeir, 2016-10-10
@Olgeir

ping 1.1.1.1 && echo OK || echo NO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question