Extract text from text file, produces error Batch file - batch-file

Recently I posted something that extracts text from a text file (Here), now what I am trying to do is extract everything from [ to ] in my script which pings any server but it just keeps returning ]= instead of the Ip address. I'm tearing my hair out trying to figure out why... anyone know?
echo Enter Minecraft Server Address:
set /p ad=">"
ping "%ad%" /n 1 >test.txt
Set/P var=<test.txt
Set var=%var:]=&:%
Set var=%var:*[=%
Echo=%var%
(Returns ]=)
thanks for your help!

You probably don't need to use the same method:
#Echo Off
Set/P "ad=Enter Minecraft Server Address > "
For /F "Tokens=2 Delims=[]" %%A In ('Ping -n 1 "%ad%"') Do Set "var=%%A"
Echo=%var%
Timeout -1

Related

How can I pass the output of a ping command to a variable within a Batch-file?

Specifically here, I am trying to pass the output of a ping command that is piped to a Find "Reply" to a variable. From there, I'd like to take that variable and check the IP against a list of IP's to determine which subnet that IP is on. I'm thinking I can use some iteration of FOR to then split off just the IP, which I can then use from there to check against my list.
I was hoping that I could just use the FOR with the ping -4 -n 1 %WHATTOPING% | Find "Reply" as my parameter, but I don't think that is going to work.
Please let me know if my question needs any clearing up! Thank you!
So far my code is:
#echo off
CLS
:start
SET /p WHATTOPING= What would you like to ping?
ping -4 -n 1 %WHATTOPING% | Find "Reply"
for /f "delims=" %%A in ('ping -4 -n 1 %WHATTOPING%') do set "var=%%A"
echo %var%
PAUSE
CLS
goto start
for /f "delims=" %%A in ('ping -4 -n 1 %WHATTOPING% ^| Find "Reply"') do set "var=%%A"
should work for you, but without a(n obfuscated) sample of the data you are generating with the ping and the value you wish to extract from that data, it can be but a hint.
The critical point is that you need to escape the | with a caret ^ in order to tell cmd that the pipe is part of the single-quoted command, not of the for that is interpreting the result of that command.
Reply is language dependent (in a German Windows, it would be Antwort). (Besides that, it's not reliable within the same network: you can get something like Reply from <localhost>: destination not reachable.)
For an "international" solution, get the address from the header line (it's enclosed in []):
for /f "tokens=2 delims=[]" %%a in ('ping -4 -n 1 www.google.de ^|find "["') do set "IP=%%a"
echo %IP%

Ping with timestamp and log

Guys im using this script in a batch file to perform continuous ping with timestamp and to be recorded in a log file, it is working fine on my computer ( windows 8 64 bit ) but when i try to use it on a windows 7 machine also 64 bit, whenever i run the batch i keep getting that:
Could not find C:\user\administrator\pinglog.txt
knowing that i have created the file pinglog.txt but i really cant figure out the problem.
#echo off
del pinglog.txt
for /f "tokens=*" %%A in ('ping localhost -n 1 ') do (echo %%A>>pinglog.txt && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping localhost -n 1 ') do (echo %date% %time% %%A>>pinglog.txt && GOTO Ping)
i appreciate any help.
Thank you in advance
Over PowerShell you can use this CLI.
ping.exe -t 10.227.23.241 |Foreach{"{0} - {1}" -f (Get-Date),$_} >> Ping_IP.txt
Could not find C:\user\administrator\pinglog.txt means that the file was not found. And the reason for this is really simple. The folder is called users and not user. So the correct path is C:\users\administrator\pinglog.txt.

Automatically open the browser after losing the connection to a specific IP (Batch File)

Actually I am very new to batch and this forum, so this is my first question here.
My goal is to create a batch-program which keeps checking if a specific IP is reachable.
As soon as it stops to be reachable / loses connection, it should open a browser and a specific website on it.
Could maybe someone help me with it? Here is, what i created so far, but it does not work very well:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=127.0.0.1
:loop
set state=down
for /f "tokens=8,10" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%a"=="xLost" if "x%%b"=="x1," set state=up
(cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe http://www.google.com
)
)
echo.Link is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
endlocal
unsure, what "does not work very well" means. But I suggest another way, that works independend of locale Settings and uses the default browser:
set ipaddr=www.stackoverflow.com
:Loop
ping -n 6 127.0.0.1 >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul && goto Loop
echo Connection lost
REM start the site in the Default browser:
start "" http://www.google.com

Batch to pattern matching in a loop

I am trying to loop through a file which contains few names and search the same with two patterns in another file and echo some statements.
Like I have two files :
1.Installers.txt
2.Progress.txt
Installers.txt
abc.jar
def.jar
tef.jar
....
....
Progress.txt
abc.jar deployment started
abc.jar deployed successfully
def.jar deployment started
So my requirement is to read the Installers.txt file one line at a time and search for the 2 patterns "abc.jar deployment started" and "abc.jar deployed successfully" and report successful or else if both patterns are yet to be found to show as still in progress.
I have tried writing below but its failing at many things while doing pattern and the logic also does not look good. can someone help here.
for /F "usebackq delims=" %%a in ("Installer.txt") do (
set /A i+=1
call echo installing %%i%% : %%a
:NOTVALID
findstr /I "%%k\ in\ progress" %1%\progress.txt
If errorlevel 1 (
echo "installation still in progress.."
PING 127.0.0.1 -n 1 >NUL 2>&1 || PING ::1 -n 1 >NUL 2>&1
goto NOTVALID
) else (
set /A i+=1
echo "installation completed.."
call set array[%%i%%]=%%a
call set n=%%i%%
)
Try the below code which suite your requirement :
cls
#echo off
for /f %%g in (Installers.txt) do type Progress.txt|findstr "%%g" || echo %%g is Yet to start , still in progress
Above code will read a single line from installer and then search for that string in file Progress.txt and then print the output if it is found or not.

Parsing lines of text in a txt file with a batch script?

so I'm currently working on a batch file which scans the network. what I have done is created a text file which has just the IP of every computer on the network that responded to a ping request, and the output of the ping request is saved to a file called IPAdresses.txt
::==========================================================================
setlocal enabledelayedexpansion
#echo off
::File reset and variables
echo. > %~dp0Logs\IPAddresses2.txt
SET fl=%~dp0Logs\IPAddresses.txt
::This command does the ping request and saves just the replies to %~dp0Logs\IPAddresses.txt.
::Every returned ping will make a line in the file that looks like this "Reply from 192.168.1.1: bytes=32 time=2ms TTL=64".
FOR /L %%i IN (1,1,254) DO ping -n 1 192.168.1.%%i | FIND /i “Reply” >> %fl%
::This command Parse the "Reply from 192.168.1.1: bytes=32 time=2ms TTL=64" line in the file and saves the output to %~dp0Logs\IPAddresses2.txt.
for /f "usebackq tokens=3" %%t in (`findstr /b /c:Reply from %fl% `) do (
echo %%t >> %~dp0Logs\IPAddresses2.txt
)
::-----------THE ISSUE-----------
::The issue is the parsing. The last command will only parse "192.168.1.1: " this is because it only takes the 3 Token (or the third set of characters in the string) in the line. I need to get rid of the ": " from "192.168.1.1: ". To be clear i want to remove the semicolon and the following space, at the end of the IP address.
::i have tried to put every line into a variable and cut the tailing characters but i cannot get it to work.
::Or is there a way to alter the FOR command to do this??
I have been searching for two days now and trying all sorts of work arounds, but no luck.
I need just the IP's in a text files for further functions, and network wide NET commands.
Any help would be appreciated
Thank You.
Never too late to answer, eh? This gets you most of the way there. There are probably cleaner ways of doing this, but I tried to follow your code as much as I could.
setlocal enabledelayedexpansion
#echo off
SET fl=%~dp0Logs\IPAddresses.txt
set net=10.2.10.
set start=71
set end=75
FOR /L %%i IN (%start%,1,%end%) DO ping -n 1 %net%%%i | findstr /i Reply >> %fl%
for /f "usebackq tokens=3" %%t in (`findstr /b /c:Reply from %fl% `) do (
set ip=%%t
set ip=!ip:~0,-1!
echo !ip!
)

Resources