For file loop in a for file loop batch file - batch-file

I have a program to ping computers, check there registry, and tell me the results.
I am now trying to script it so that I don't have to know the ip address just the host name of the computer.
I found a script to give me the ip off of a hostname
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
I have tried to simply insert this into a biger loop that uses a file to give it the host names.
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do (
set host=%%a
set ip=%%c
echo "."
echo %ip% %host%
pause
) >>%2
)
I have also tried it like this,
for /f %%a in (%1) do (
set /p hostname=%%a
for /f "tokens=1,2 delims=[]" %%b in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
echo %ipaddress% %hostname%
)
Please any help on this would be greatly appreciated.
Thank you.

AFTER MUTCH HEAD BANGING
#echo off
SETLOCAL EnableDelayedExpansion
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping /4 /n 1 %%A ^| findstr "Pinging"') do echo "%%A %%b"
)

Related

batch code to list ip addressess that do not ping

I am using the code below to list ip addresses from a list of computer names on list.txt, but they have to be pinging to list. How can I edit the code to show these IP addressess that do not ping also?
#echo off
Echo Pinging list...
set ComputerList=list.txt
Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0, -1!>>Results.csv
))
pause
list.txt contains the following:
and
Results.csv will populate
but it will not populate IP address for PCWINDATA103 because it is not pinging but I know an IP exists
Force a custom output in case findstr doesn't find Reply (take care of "tokens=3"):
...
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3 delims=: " %%B in ('ping -4 -n 1 -l 1 %%A ^|findstr "Reply" ^|^|echo x x offline') do (
echo %%A,%%B
)
)

Copy with Batch

Hey guys ok so I'm trying to merge some files and if possible keep the folders and sub folder
the file I used for testing is called (1) Austin.txt
#ECHO OFF
SETLOCAL
For %%G In ("%~dp0..\New Folder 1") Do Set "source=%%~fG"
For %%G In ("%~dp0..\New Folder 2") Do Set "target=%%~fG"
For %%G In ("%~dp0..\New Folder 3") Do Set "destdir=%%~fG"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%source%\*.txt"') DO call :label "%%q"
goto :eof
:Label
set "FILE=%~1"
for /f "tokens=1 delims=[]" %%a in ('find /n "appi"^<"%~1"') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "opcn"^<"%~1"') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%~1"') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\%~n1A.txt"
for /f "tokens=1 delims=[]" %%a in ('find /n "paid"^<"%~1" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "whbn"^<"%~1" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%~1" ') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\%~n1B.txt"
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(1) AustinA.txt" + "%target%\(1) AustinB.txt" "%destdir%\(1).txt"
)
OK so up to this point my results will look like this
Source New Folder 1
| City
| (1) Austin.txt
Target New Folder 2
| (1) AustinA.txt
| (1) AustinB.txt
Now on this section on my original script it would merge A.txt and B.txt to (1).txt
now that I have the original file name, I am not sure how to merge Target to (1) Austin.txt
This is no longer needed
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(1) AustinA.txt" + "%target%\(1) AustinB.txt" "%destdir%\?????????"
)
I need A and B to be merged with the original name of the file
Source New Folder 1
| City
| (1) Austin.txt
Target New Folder 2
| City
| (1) AustinA.txt
| (1) AustinB.txt
Destdir New Folder 3
| City
| (1) Austin.txt
Here is what changed Now my script 99% perfect adding the second > is all I needed to do, for weeks I have been looking for a way to merge my files
From this
)>"%target%\%~n1A.txt"
)>"%target%\%~n1B.txt"
To this
)>"%target%\%~n1.txt"
)>>"%target%\%~n1.txt"
and I removed this
For %%G In ("%~dp0..\New Folder 3") Do Set "destdir=%%~fG"
This extraction can read between codes in any readable files
This extraction can be cut in half, if you only need to extract between 2 codes
my setup reads for codes and put them in one .txt file
#ECHO OFF
SETLOCAL
For %%G In ("%~dp0..\New Folder 1") Do Set "source=%%~fG"
For %%G In ("%~dp0..\New Folder 2") Do Set "target=%%~fG"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%source%\*.txt"') DO call :label "%%q"
goto :eof
:Label
set "FILE=%~1"
for /f "tokens=1 delims=[]" %%a in ('find /n "CODE1"^<"%~1"') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "CODE2"^<"%~1"') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%~1"') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\%~n1.txt"
for /f "tokens=1 delims=[]" %%a in ('find /n "CODE3"^<"%~1" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "CODE4"^<"%~1" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%~1" ') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>>"%target%\%~n1.txt"
RESULTS
Source New Folder 1
| City Folder
| (1) Austin.txt
Target New Folder 2 < ---- missing City Folder
| (1) Austin.txt
Still working on folders
Thank You Stephan for the wonderful help
for %%a in ("%~1") do for %%b in ("%%~dpa.") do echo "%%~nxb\%%~nxa"
where %~1 is a fully qualified filename (X:\Some Folder\Folder 1\file.ext). Output: "Folder 1\file.ext" (last folder plus filename plus extension)
But why do you generate two files, just to join them? Just put both outputs into the same file:
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%~1"') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%destdir%\%~nx1"
...
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%~1" ') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>>"%destdir%\%~nx1"

Batch file to print URL and IP list for hosts file

I have a list of host/url in a txt file (let's say URLList.txt) and I need to get the IP list of all of them.
There are a lot of url, so it will be pretty long to do that manually.
Basically I was wondering if, based on the input file list, I could get a result like this:
host1 IP1
host2 IP2
.
.
hostn IPn
For example
s12web 120.234.567.12
s34web 12.444.32.22
etc
Then I would put this output and copy it to my hosts file
Thanks a lot for your great help,
You can use this to do that, credit to #Lizz for the way to find the ip:
#echo off
for /f "usebackq delims=" %%h in ("URLList.txt") do (
for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
echo.%%h %%f
)
)
pause
And to echo it to a file you could use this:
#echo off
type nul>IPList.txt
for /f "usebackq delims=" %%h in ("URLList.txt") do (
for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
echo.%%h %%f
)>>IPList.txt
)
pause
As #aschipfl pointed out, another solution that's more elegant:
#echo off
>IPList.txt (
for /f "usebackq delims=" %%h in ("URLList.txt") do (
for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 %%h ^|find /i "pinging"') do if not "%%f"=="" (
echo.%%h %%f
)
)
)

Speed up reading results in batch script

What I am trying to do, is speed up my reading results of batch file.
Am trying to get different values using netsh commands and then present them in my script console but it takes to long.
See below a small part of my script to get the idea. (this is just a small part, I'm actually getting around 50 different values and using more netsh commands)
Does anybody know a way to speed up the process?
.
.
.
netsh interface ipv4 show config %AdapterLAN% >temp
for /f "tokens=3" %%i in ('findstr "IP Address" temp') do set ip=%%i
echo. IP Address : %ip%
for /f "tokens=5 delims=) " %%i in ('findstr "Subnet Prefix" temp') do set mask=%%i
echo. Mask : %mask%
for /f "tokens=3" %%i in ('findstr "Gateway:" temp') do set gateway=%%i
echo. Gateway : %gateway%
for /f "tokens=1,5,6" %%a in ('findstr "DNS" temp') do set dns1=%%a&set dns5=%%b&set dns6=%%c
If "%dns1%"=="Statically" set dns=%dns5%
if "%dns1%"=="DNS" set dns=%dns6%
echo. DNS Server : %dns%
for /f "tokens=3" %%i in ('findstr "Gateway Metric" temp') do set GMetric=%%i
for /f "tokens=2" %%i in ('findstr "InterfaceMetric" temp') do set IMetric=%%i
set /a metricLAN=Gmetric + imetric
echo. Metric : %metricLAN%
for /f "tokens=3" %%i in ('find "DHCP enabled" temp') do set LANDHCP=%%i
If "%dns1%"=="Statically" set xx=Static
if "%dns1%"=="DNS" set xx=DHCP
If /i %LANDHCP%==No set LANDHCP=Static
if /i %LANDHCP%==YES set LANDHCP=DHCP
echo. Obtained IP : %LANDHCP%
echo. Obtained DNS : %xx%
for /f "tokens=3 delims=," %%a in ('getmac /v /fo csv ^| find """%AdapterLAN-without-Q%""" ') do set macLAN=%%a
echo. MAC-Addres : %macLAN%
del temp
.
.
.
netsh wlan show profile >temp
.
Do a similar process of getting values from another netsh command sent them
in the temp file …echo the one I want on the screen ..delete the file etc.
Next approach could be a bit faster (no temporary file(s), updated no multiple findstr):
#ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "AdapterLAN=wiredEthernet"
set "IMetric="
set "GMetric="
for /F "tokens=1,2* delims=:" %%G in ('
netsh interface ipv4 show config "%AdapterLAN%"^|findstr /N /R "^"
') do (
rem echo G="%%G" H="%%H" I="%%I"
if "%%I"=="" (
rem line 1 skip
rem line 2 = Configuration for interface
rem line 10 = DNS server #2 etc.
) else (
set "hh=%%H"
set "xx=!hh:IP Address=!"
if not "!hh!"=="!xx!" for /F "tokens=1*" %%i in ("%%I") do set "ip=%%i"
set "xx=!hh:Subnet Prefix=!"
if not "!hh!"=="!xx!" for /F "tokens=3 delims=) " %%i in ("%%I") do set "mask=%%i"
set "xx=!hh:Default Gateway=!"
if not "!hh!"=="!xx!" for /F "tokens=1*" %%i in ("%%I") do set "gateway=%%i"
set "xx=!hh:Gateway Metric=!"
if not "!hh!"=="!xx!" for /F "tokens=1*" %%i in ("%%I") do set "GMetric=%%i"
set "xx=!hh:InterfaceMetric=!"
if not "!hh!"=="!xx!" for /F "tokens=1*" %%i in ("%%I") do set "IMetric=%%i"
)
)
echo( IP Address : [%ip%]
echo( Mask : [%mask%]
echo( Gateway : [%gateway%]
set /a metricLAN=Gmetric + IMetric
echo( Metric : [%metricLAN%]
ENDLOCAL
goto :eof
Output:
==>D:\bat\SO\31356115.bat
IP Address : [192.168.1.100]
Mask : [255.255.255.0]
Gateway : [192.168.1.1]
Metric : [20]
==>
Edit
Here is another approach: unlike netsh, parsing the wmic command output seems to be a bit easier when used get verb together with /value switch as it's well defined and well structured. You could find here all the info as from netsh: next code snippet should read and make public a huge range of information about all enabled NIC adapter(s) in a defined local or remote computer:
#ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "NetCount=0"
set "compName=%computername%" :: local or remote computer name
set "compIDXs="
for /F "tokens=2 delims==" %%N in ('
wmic /node:"%compName%" NIC where "NetEnabled=TRUE" get InterfaceIndex /value 2^>NUL ^| find "="
') do for /F "tokens=*" %%n in ("%%N") do (
for /F "tokens=*" %%G in ('
wmic /node:"%compName%" NIC where "InterfaceIndex=%%n" get /value 2^>NUL ^| find "="
') do for /F "tokens=*" %%g in ("%%G") do set "_%%n%%g"
for /F "tokens=*" %%I in ('
wmic /node:"%compName%" NICCONFIG where "InterfaceIndex=%%n" get /value 2^>NUL ^| find "="
') do for /F "tokens=*" %%i in ("%%I") do set "_%%n_%%i"
set /A "NetCount+=1"
set "compIDXs=!compIDXs! "%%n""
)
set _
rem sample of it:
echo compName=%compName% NetCount=%NetCount% compIDXs=%compIDXs%
for %%x in (%compIDXs%) do (
echo enabled InterfaceIndex=%%~x NetConnectionID=!_%%~xNetConnectionID!
for /F "tokens=1,2 delims={}," %%i in ("!_%%~x_IPAddress!") do echo ipv4=%%~i ipv6=%%~j
)
Read Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem to see why any wmic command output is parsed via a couple of nested for loops.

Batch file help - adding output from command to csv file

I currently have a batch file that reads a list of computer names and pings each of these and outputs the ones that reply to a csv file with the computer name and ip address.
I now need to edit this to also find out the user of the machine. I need to contact users which are online to arrange some work done to their computer. Their can be over a hundred machines in the batch file so to manually find out each user takes time. Is there a way to do this?
`IF EXIST C:\test\new.csv (del C:\test\new.csv)
IF EXIST C:\test\final.csv (del C:\test\final.csv)
set ComputerList=C:\test\ClientList.txt
Echo Computer Name,IP Address>Final.csv
setlocal enabledelayedexpansion
echo please wait...
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0,-1!>>final.csv
)
)
findstr /V "IPAddress" final.csv >> C:\test\new.csv
echo identified machines for Install
start excel C:\test\new.csv
echo opened csv file`
The command I want to use to get the username is:
`wmic.exe /NODE: %%A COMPUTERSYSTEM GET USERNAME`
Thanks
Mark
Here is a function I wrote to do just what you are trying to do:
:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
) ELSE (for /f "skip=1" %%a in (
'wmic /NODE:"%1" Computersystem get username' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
))
Exit /b
Here is my function for pinging. It returns a 0 if the ping succeeded and a 1 otherwise.
:IsPingable comp
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
Usage example:
for /l %%a in (1,1,255) do (
call:IsPingable 10.6.1.%%a && (
echo ping 10.6.1.%%a used )||( echo ping 10.6.1.%%a unused )
)
And here is for if you're pinging IP's and want to return the hostname as well:
:IsPingable2 comp ret
setlocal
for /f "tokens=2" %%a in (
'"ping -a -n 1 -4 "%~1" | Find "Pinging" 2>nul"') do set name=%%a
endlocal & set %~2=%name%
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
Usage example:
#echo off
setlocal enabledelayedexpansion
for /l %%a in (1,1,255) do (
call:IsPingable2 10.6.1.%%a host && (
echo ping !host! - 10.6.1.%%a used )||( echo ping !host! - 10.6.1.%%a unused )
)
I just posted these because they just might come in handy for this type of thing in the future. You can use the :IsPingable now though.
You would use it like this in your code:
IF EXIST C:\test\final.csv (del C:\test\final.csv)
set ComputerList=C:\test\ClientList.txt
Echo Computer Name,IP Address,Logged In User>Final.csv
setlocal enabledelayedexpansion
echo please wait...
echo.
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|find "TTL="') do (
if not errorlevel 1 (
set IPadd=%%B
call :GetLoggedInUser %%B uname
echo %%A,!IPadd:~0,-1!,!uname!>>final.csv
)
)
)
echo identified machines for Install
start excel C:\test\final.csv
echo opened csv file
goto :eof
:GetLoggedInUser comp user
for /f %%u in (
'wmic /NODE:"%1" Computersystem get username^|find "\"') do (
if not errorlevel 1 ( for /f "tokens=2 delims=\" %%a in (
'wmic /NODE:"%1" Computersystem get username^|find "\"' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
) ELSE (for /f "skip=1" %%a in (
'wmic /NODE:"%1" Computersystem get username' ) do (
For /f %%b in ("%%a") do (set %2=%%b))
))
Exit /b
The below code will count the number of lines in two files sequentially and is set to the variables SalaryCount and TaxCount.
#ECHO OFF
echo Process started, please wait...
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set SalaryCount=%%C
echo Salary,%SalaryCount%
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set TaxCount=%%C
echo Tax,%TaxCount%
Now if you need to output these values to a csv file, you could use the below code.
#ECHO OFF
cd "D:\CSVOutputPath\"
echo Process started, please wait...
echo FILENAME,FILECOUNT> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set Count=%%C
echo Salary,%Count%>> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set Count=%%C
echo Tax,%Count%>> SUMMARY.csv
The > will overwrite the existing content of the file and the >> will append the new data to existing data. The CSV will be generated in D:\CSVOutputPath

Resources