I need Batch file for create like this example :
▬ Google.com ▬
▬ Yahoo.com ▬
▬ Bing.com ▬
Also I have more links for this url in link.txt
http://drive.google.com
http://gallery.bing.com
http://mail.yahoo.com
http://video.google.com
http://brb.yahoo.com
http:/map.bing.com
I want After Run Batch file show links in below title like this :
▬ Google.com ▬
http://drive.google.com
http://video.google.com
▬ Yahoo.com ▬
http://mail.yahoo.com
http://brb.yahoo.com
▬ Bing.com ▬
http://gallery.bing.com
http:/map.bing.com
How about a following sample script?
Flow :
Retrieve lines included google.com, yahoo.com and bing.com from "link.txt" and import them to array.
if not "!val:google.com=!" == "!val!" means when a line (val) includes google.com, set commands are run.
Display elements in each array.
Sample script :
#echo off
setlocal enabledelayedexpansion
set g=0
set y=0
set b=0
for /f "delims=" %%a in (link.txt) do (
set val=%%a
if not "!val:google.com=!" == "!val!" (
set google[!g!]=!val!
set /a g+=1
)
if not "!val:yahoo.com=!" == "!val!" (
set yahoo[!y!]=!val!
set /a y+=1
)
if not "!val:bing.com=!" == "!val!" (
set bing[!b!]=!val!
set /a b+=1
)
)
set /a g-=1
set /a y-=1
set /a b-=1
echo - Google.com -
for /L %%i in (0,1,!g!) do echo !google[%%i]!
echo.
echo - Yahoo.com -
for /L %%i in (0,1,!y!) do echo !yahoo[%%i]!
echo.
echo - Bing.com -
for /L %%i in (0,1,!b!) do echo !bing[%%i]!
Result :
- Google.com -
http://drive.google.com
http://video.google.com
- Yahoo.com -
http://mail.yahoo.com
http://brb.yahoo.com
- Bing.com -
http://gallery.bing.com
http:/map.bing.com
Note :
This sample script can be used for the case that the URLs include google.com, yahoo.com and bing.com. So if there is an URL of http://google.com.yahoo.com, the result is displayed at both google.com and yahoo.com.
The order of each result which is displayed is followed for the order of link.txt.
If I misunderstand your question, I'm sorry.
Edit :
If you want to save the result as a text data using the batch file, please modify as follows. When this modification is done, link.txt is overwritten by the result. So please be careful. If you want to add the result to link.txt, please change from echo - Google.com - > %outputfile% to echo - Google.com - >> %outputfile%.
From :
echo - Google.com -
for /L %%i in (0,1,!g!) do echo !google[%%i]!
echo.
echo - Yahoo.com -
for /L %%i in (0,1,!y!) do echo !yahoo[%%i]!
echo.
echo - Bing.com -
for /L %%i in (0,1,!b!) do echo !bing[%%i]!
To :
set outputfile=link.txt
echo - Google.com - > %outputfile%
for /L %%i in (0,1,!g!) do echo !google[%%i]! >> %outputfile%
echo. >> %outputfile%
echo - Yahoo.com - >> %outputfile%
for /L %%i in (0,1,!y!) do echo !yahoo[%%i]! >> %outputfile%
echo. >> %outputfile%
echo - Bing.com - >> %outputfile%
for /L %%i in (0,1,!b!) do echo !bing[%%i]! >> %outputfile%
This simpler Batch file don't requires to explicitly write the target sites; it works with any urls placed in the file with the same format of the example data.
#echo off
setlocal EnableDelayedExpansion
rem Sort and store urls
for /F "tokens=1* delims=." %%a in (link.txt) do (
set "name[%%b]=!name[%%b]! %%a.%%b"
)
rem Output sorted urls
(for /F "tokens=2* delims=[]=" %%a in ('set name[') do (
echo - %%a -
for %%c in (%%b) do echo %%c
echo/
)) > link.txt
Result:
- bing.com -
http://gallery.bing.com
http:/map.bing.com
- google.com -
http://drive.google.com
http://video.google.com
- yahoo.com -
http://mail.yahoo.com
http://brb.yahoo.com
Related
I am trying to create a batch script that will ping a machine by hostname, save ip and domain to a variable and display the state (ON or OFF).
How can i fix this code so it will display the status correctly?
My code always returns ON even if pc is actually OFF.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set hostname=non-existent-hostname
set domain=UNRESOLVED
set ip=UNRESOLVED
for /f "tokens=2,3 delims= " %%b in ('ping -a -4 -n 1 !hostname! ^| find "Pinging"') do (
set domain=%%b
set ip=%%c
)
if errorlevel 1 (
echo !hostname! !ip! [!domain!] is OFF
) else (
echo !hostname! !ip! [!domain!] is ON
)
pause
I have another approach using this kind of ping, because my machine is French.
For example, this batch script pings a list of URLs and displays their status (ON/OFF) with different colors.
The URLs are defined in the "URLS" variable.
The script loops through the URLs, formats them using a StringFormat function, gets the IP address of each URL using the "ping" command, and then displays the status of each URL (ON or OFF) with a color using a PSColor function.
#echo off
Title Multi-Ping hosts with colors
Set URLS="non-existent-hostname","https://www.google.com","Nothingtotest","https://www.yahoo.com","www.reddit.com",^
"http://www.wikipedia.com","www.stackoverflow.com","www.bing.com","NoBodyHere.com"
setlocal ENABLEDELAYEDEXPANSION
for %%a in (%URLS%) do (
Call :StringFormat "%%~a"
set "ip="
for /f "tokens=2 delims=[]" %%b in ('ping -n 1 !URL!') do set "ip=%%b"
ping -n 1 !URL!>nul && set "msg=!URL! - !ip! ON" && Call :PSColor "!msg!" Green \n || set "msg=!URL! - !ip! OFF" && Call :PSColor "!msg!" Red \n
)
pause & Exit
::---------------------------------------------------------------------------------
:StringFormat <URL>
(
echo Function StringReplace(Str^)
echo Str = Replace(Str,"http://",""^)
echo Str = Replace(Str,"https://",""^)
echo StringReplace = str
echo End Function
echo wscript.echo StringReplace("%~1"^)
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "URL=%%~a" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::---------------------------------------------------------------------------------
:PSColor <String> <Color> <NewLine>
If /I [%3] EQU [\n] (
Powershell Write-Host "`0%~1" -ForegroundColor %2
) Else (
Powershell Write-Host "`0%~1" -ForegroundColor %2 -NoNewLine
)
Exit /B
::---------------------------------------------------------------------------------
I figured it out, thanks everyone for hints.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set hostname=non-existent-hostname
set domain=UNRESOLVED
set ip=UNRESOLVED
for /f "tokens=2,3 delims= " %%b in ('ping -a -4 -n 1 !hostname! ^| find "Pinging"') do (
set domain=%%b
set ip=%%c
)
ping -n 1 %hostname% >nul
if %errorlevel% == 0 (
echo %hostname% %ip% [%domain%] is ON
) else (
echo %hostname% %ip% [%domain%] is OFF
)
pause
I'm trying to make a batch script which is supposed to ping a site, log the results, and start a program if the results were negative. This is a modification of original script (not mine), which can be found here. Values of domain, IP, and program variables are for illustrative purposes.
#echo off
cls
set domain=testsite.com
set IP=133.78.17.101
set program=c:\windows\notepad.exe
set output=c:\log.txt
set result=1
:Start
IF [%result%]==[] (
>>%output% echo -----------
start %program%
)
ECHO Pinging %domain%...
FOR /F "delims=" %%G in ('ping -n 1 %domain% ^| find "Reply"') DO SET result=%%G
IF NOT [%result%]==[] (
goto Success
) ELSE (
goto TryAgain
)
:TryAgain
ECHO %domain% unreachable. Trying again...
FOR /F "delims=" %%G in ('ping -n 1 %domain% ^| find "Reply"') DO SET result=%%G
IF NOT [%result%]==[] (
goto Success2
) ELSE (
goto TryIp
)
:TryIp
ECHO %domain% unreachable. Pinging %ip%...
FOR /F "delims=" %%G in ('ping -n 1 %IP% ^| find "Reply"') DO SET result=%%G
IF NOT [%result%]==[] (
goto SuccessDNS
) ELSE (
goto TestInternet
)
:TestInternet
ECHO %ip% unreachable. Testing internet connection.
FOR /F "delims=" %%G in ('ping -n 1 www.google.com ^| find "Reply"') DO SET result=%%G
IF NOT [%result%]==[] (
goto Success3
) ELSE (
goto NetDown
)
:Success
>>%output% ECHO Connected
>>%output% echo %date% %time% %result%
ping -n 3 127.0.0.1 > nul
goto Start
:Success2
>>%output% ECHO Connected with packet loss.
>>%output% echo %date% %time% %result%
set result=
ping -n 3 127.0.0.1 > nul
goto Start
:Success3
>>%output% ECHO Domain %domain% not reachable. Connected via IP.
>>%output% echo %date% %time% %result%
set result=
ping -n 3 127.0.0.1 > nul
goto Start
:SuccessDNS
>>%output% ECHO DNS problem.
>>%output% echo %date% %time% %result%
set result=
ping -n 3 127.0.0.1 > nul
goto Start
:NetDown
>>%output% ECHO No internet connection.
>>%output% echo %date% %time% %result%
set result=
ping -n 3 127.0.0.1 >nul
goto Start
What I'm trying to achieve is this - if anything other than a perfect reply to a ping request is received, the script should start a program. To secure that this happens only then, I've been clearing the result variable every time, other than on an expected ping response.
Echoing the value of result keeps returning 1, even after I've emptied it.
in your line
FOR /F "delims=" %%G in ('ping -4 -n 1 %domain% ^| find "Reply"') DO SET result=%%G
%%G is either not defined (when the word Reply doesn't occur), which doesn't touch your Result variable at all,
or a line like Reply from x.x.x.x : Bytes=32 Time<1ms TTL=128, which definitively isn't empty.
According to the rest of your code, you probably meant ... DO SET "result=" to unset the variable.
Note: searching for "Reply" is
a) language dependent ("Antwort" on German Windows) and
b) not reliable (think of Reply from localhost: destination address unreachable).
Better search for TTL= (even works without a for loop):
ping -n 1 %IP% | find "TTL=" >nul && set "reply=true" || set "reply=false"
echo %reply%
Just an addendum to Stephan's post.
In addition to reasons posted in Stephan's post, the code was not working as I had originally planned it to, because the comparisons were failing as well. If there was a successful ping, the result variable was being set to a string consisting of multiple words, which was breaking the comparison. To avoid this, I had to change every instance of
IF [%result%]==[]
to
IF ["%result%"]==[""]
Thomas Weller's (now deleted) comment was also on the right track - I did need to empty the result variable in :Start:
:Start
IF ["%result%"]==[""] (
>>%output% echo -----------
start %program%
)
SET result=
ECHO Pinging %domain%...
This emptying was needed to annul any previous successes and the original set result=1 (in case of consistent failures).
I am trying to make a tool that pings a list of IPs and outputs the three IPs with the lowest ms (time) to reach the servers.
Input: -list of IPs seperated by spaces- such as 1.1.1.1 1.1.1.2 1.1.1.3 etc
Output: -the three IPs with lowest ms- (in order of lowest ms on left to 3rd lowest ms on right) such as 1.1.2.2 1.2.3.3 1.4.4.4
I have this for the ping part so far:
#echo off
set ip=8.8.8.8
for /f "tokens=4 delims=(=" %%a IN ('ping %ip% -n 1 ^|find "Average = "') do echo Result is %%a
pause
This mini-code will output the ms ping from each server.
I need to find the ping of each server and compare them.
Can anyone help me with all this? I am sorry if I was not detailed enough and if I didn't show enough details or code.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
:: IPs from http://etherealmind.com/what-is-the-best-ip-address-to-ping-to-test-my-internet-connection/
Set "IPlist=8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 4.2.2.2 141.1.1.1"
For %%A in (%IPlist%) Do (
Set IP=%%A
for /f "tokens=6delims==, " %%B in (
'ping %%A -n 1^|findstr Average'
) Do (
Set PingRes=%%B
Set /A Avg=100000+!PingRes:ms=!
Set MS_!Avg!_!IP:.=!=!IP!
)
)
Echo Show intermediate results
Set MS_1
Set Cnt=0 & Set "NewIPlist="
For /f "tokens=2,4 delims=_=" %%A in ('Set MS_1') Do (
Set /A Cnt+=1
if !Cnt! gtr 3 Goto :End
Set NewIPlist=!NewIPlist! %%B
)
:End
Echo(
Set NewIPlist=%NewIPlist:~1%
Set NewIPlist
Try to understand what the batch does, ask if you have any questions.
Sample output
> Measure-IP.cmd
Show intermediate results
MS_100013_141111=141.1.1.1
MS_100013_20867220220=208.67.220.220
MS_100014_4222=4.2.2.2
MS_100027_20867222222=208.67.222.222
MS_100028_8888=8.8.8.8
MS_100032_8844=8.8.4.4
NewIPlist=141.1.1.1 208.67.220.220 4.2.2.2
#echo off
setlocal EnableDelayedExpansion
rem Set the list of IP's
set "IPs=8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 4.2.2.2 141.1.1.1"
rem Set the number of desired IP's in the output
set "num=3"
rem Initialize "MS" array with large numbers
for /L %%i in (1,1,%num%) do set "MS[%%i]=9999"
for %%a in (%IPs%) do (
set "ip=%%a"
for /F "tokens=6 delims== " %%b in ('ping %%a -n 1 ^| find "Average = "') do (
set "ms=%%b" & set "ms=!ms:ms=!"
REM ECHO %%a = !ms!
rem Insert this ms and ip values in their right position in MS and IP arrays
for /L %%i in (1,1,%num%) do if !ms! lss !MS[%%i]! (
set /A t=MS[%%i], MS[%%i]=ms, ms=t
set "t=!IP[%%i]!" & set "IP[%%i]=!ip!" & set "ip=!t!"
)
)
)
echo %IP[1]% (%MS[1]%), %IP[2]% (%MS[2]%), %IP[3]% (%MS[3]%)
Output example:
208.67.222.222 (42), 208.67.220.220 (44), 4.2.2.2 (47)
I wrote a very simple script to output the host machine's MAC addresses to a text file.
The script is exiting right after line 3 - 'IF DEFINED WRITEOK ('.
#echo off
cls
copy /Y NUL "%CD%\.writable" > NUL 2>&1 && set WRITEOK=1
IF DEFINED WRITEOK (
rem ---- we have write access ----
set DIR=%CD%\interfaces
set FILE=%DIR%\%USERNAME%.txt
IF NOT EXIST "%DIR%" (
MKDIR "%DIR%"
echo DIR '%DIR%' was created
) else (
echo DIR '%DIR%' already exists
) for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (
echo %%i >> "%FILE%"
echo OUTPUT written to '%FILE%'
)
) else (
rem ---- we don't ----
echo DIR '%DIR%' is not writable
)
echo.
echo DONE!
pause
Try to put the FOR one line after the closing parenthesis :
...)
for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (...
you can't start a FOR with a closing parenthesis in front :
This will not work :
(echo 1
) for /l %%a in (1,1,10) do echo %%a
and this will work :
(echo 1
)
for /l %%a in (1,1,10) do echo %%a
EDIT 1 :
For the path variables containing space use double quote :
"%cd%"
when using it.
I wrote a batch but it doesnt work at all:
set THEFILE=ip_list.txt
if exist result.txt del result.txt
::read %THEFILE% and loop through each line
for /F %%A in (%THEFILE%) do (
SETLOCAL ENABLEDELAYEDEXPANSION
SET counter=1
FOR /F "tokens=* USEBACKQ" %%F IN (`ping %%A`) DO (
SET result%counter%=%%F
SET counter=%counter%+1
)
ECHO %result1%
ECHO %result2%
ECHO %result3%
echo %result9% >> result.txt
ENDLOCAL
echo ******************************************* >> result.txt
)
inside of ip_list.txt :
8.8.8.8
4.4.4.4
I need to get ip adresses if not %100 loss occurs..
I need a quick help in my code..
Here is how I'd do it:
#echo off
setlocal
set THEFILE=ip_list.txt
if exist result.txt del result.txt
for /F %%A in (%THEFILE%) do (
call :IsPingable %%A && (
Echo %%A is pingable ) || (Echo %%A is not pingable)
)
exit /b
:IsPingable <comp>
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b