Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by pkwan
at 2024-07-22 21:49:18
Point:500 Replies:12 POST_ID:828597USER_ID:11439
Topic:
MS DOS;;Windows Batch Scripting
A sample file is attached.
I would like to search for all files in a directory that contains both the following strings:
"win7c.gho" and "win7d.gho"
and then get the file name and the IP address for each file.
For example, the IP address is 172.17.129.106 for the attached sample file.
How can I achieve it using DOS command or batch programming? Please keep it as simple as possible since my colleague is not good at computer programming.
I would like to search for all files in a directory that contains both the following strings:
"win7c.gho" and "win7d.gho"
and then get the file name and the IP address for each file.
For example, the IP address is 172.17.129.106 for the attached sample file.
How can I achieve it using DOS command or batch programming? Please keep it as simple as possible since my colleague is not good at computer programming.
Attachment:CH1-ATM-AV-06.log
Expert: duncanb7 replied at 2024-07-25 00:47:00
If have such log file, that will more easier, yes you are right
Expert: duncanb7 replied at 2024-07-25 00:45:50
SOrry, I though you don't have such log file
Expert: dragon-it replied at 2024-07-25 00:25:53
No problem, glad it helped.
steve
steve
Author: pkwan replied at 2024-07-24 18:57:37
Thanks all for your effort.
oBdA, dragon-it: both works so well.
DaveBaldwin, I have also considered this software, but it is rejected by my colleague since he does not have the rights to install anything on his work computer.
duncanb7, your script is too complicated. Besides, the IP addresses are not actually in the network. It is referring to the content of the log file only.
oBdA, dragon-it: both works so well.
DaveBaldwin, I have also considered this software, but it is rejected by my colleague since he does not have the rights to install anything on his work computer.
duncanb7, your script is too complicated. Besides, the IP addresses are not actually in the network. It is referring to the content of the log file only.
Expert: duncanb7 replied at 2024-07-23 06:33:57
you can also use "nslookup host !a_:~0,%pos%!" to replace
"ping !a_:~0,%pos%!" in the script if the script speed is too slow
"ping !a_:~0,%pos%!" in the script if the script speed is too slow
Expert: duncanb7 replied at 2024-07-23 06:13:02
I just test it working on Window 7 only
Expert: duncanb7 replied at 2024-07-23 06:11:34
Dear pkwan,
The following batch script on window 7 might be work and your need.
When type net view /all, you will see what the computer with its name connecting
to your network
So I set str variable for network computer hostname in the following batch script
for dos command of net view /all
and Please fill your window network shared folder name as
the filedirectory variable for the following script, probably it is "SharedDosc"
Hope it is your final solution or script
Duncan
The following batch script on window 7 might be work and your need.
When type net view /all, you will see what the computer with its name connecting
to your network
So I set str variable for network computer hostname in the following batch script
for dos command of net view /all
and Please fill your window network shared folder name as
the filedirectory variable for the following script, probably it is "SharedDosc"
Hope it is your final solution or script
Duncan
@echo offsetlocal EnableDelayedExpansionREM find the both strings in every network host computersset "xComputer="for /f %%A in ('net view /all') do (set "xComputer=%%~A"if "!xComputer:~0,2!"=="\" (echo.!xComputer! | find /I "\">tmp.txtREM set str equal to host name of networkset /p str= < tmp.txtset str=!str!echo !str!>tmp.txtset /p str= < tmp.txtREM Trim space from str variableset str=!str!##set str=!str: ##=##!set str=!str: ##=##!set str=!str: ##=##!set str=!str: ##=##!set str=!str: ##=##!set str=!str:##=!REM str equal to host name of network and filedirectory equal to data directory pathREM for exmple, findstr /s "seach-string" \DELL-PCuserslocalREM please fill your exact file directory name to filedirectory variablefindstr /s/m "win7c.gho" "!str!filedirectory*.*" >>tmp2.txtfindstr /s/m f:/tmp2.txt "win7d.gho" >>tmp3.txt))REM find the seached result file's IP for every hostname by Pingset /a inc=1;for /f %%A in ('more tmp2.txt') do (set "xComputer=%%~A"echo.echo File !inc!echo =============================================================================echo Found File at !xComputer!call :Charpos !xComputer:~2! "xam" position_ set /a inc +=1)del tmp2.txtdel tmp3.txtendlocalpauseREM subroutine or call function areaREM find pos of "" to extract the hostname in seached file with containing both string:Charpos@echo off & setlocal enableextensions set a_=%1 set b_=%2call :InstrFN "%a_%" "%b_%" position_REM echo The last position of "%b_%" in "%a_%" is %position_% set /a pos=%position_%-1echo NETWORK Hostname : !a_:~0,%pos%!ping !a_:~0,%pos%! endlocal & goto :EOF:: =============================================================== :InstrFN setlocal enableextensions enabledelayedexpansion echo %1|findstr %2>nul if %errorlevel% EQU 1 (endlocal & set %3=0& goto :EOF) set rest_=%1 set /a instr_=0 :_loop set rest_=%rest_:~1% echo !rest_!|findstr %2>nul if %errorlevel% EQU 1 (endlocal & set %3=%instr_%& goto :EOF) set /a instr_ +=1 if "%rest_:~0,1%"=="" (endlocal & set %3=%instr_%& goto :EOF) goto _loop 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:
Expert: Dave Baldwin replied at 2024-07-23 01:25:18
You might take a look at WinGrep, a Windows GUI version of the Unix grep program. http://www.wingrep.com/
Windows Grep is a tool for searching files for text strings that you specify.
Assisted Solution
Expert: dragon-it replied at 2024-07-23 01:05:08
250 points EXCELLENT
I was thinking along the lines of using "FIND" on the file, it may well be faster if there are many:
@echo offsetlocal enabledelayedexpansioncd /d c:logdirfor %%f in (*.log) do ( find /i "win7c.gho" <"%%f">NUL && find /i "win7d.gho" <"%%f">NUL && ( for /f "tokens=4 delims= " %%I in ('FIND "Volume in drive" ^<"%%~f"') do echo Found both in %%f - %%I )) 1:2:3:4:5:6:7:8:9:10:
That looks for all .log files in c:logdir currently. For each it does a find in the file case-insensitively for the "win7c.gho" and if that is found (&&) then it checks for the "win7d.gho", and if that is found too then it runs the next bit in brackets. That uses FIND again to pull out only the line that says "Volume in drive" and splits the line up where the and spaces are to give you the IP.
Steve
Accepted Solution
Expert: oBdA replied at 2024-07-22 23:25:50
250 points EXCELLENT
Try this:
@echo offsetlocal enabledelayedexpansionREM *** The folder in which the log files are:set SourceFolder=D:TempREM *** The file mask to search forset FileMask=*.logREM *** The file in which results will be logged; %~dp0 will expand to the script's Drive and Path, including a trailing backslash:set LogFile=%~dp0Results.csvREM *** The field separator to use in the results file:set Delim=,>"%LogFile%" echo File%Delim%IPfor %%a in ("%SourceFolder%\%FileMask%") do ( echo Processing %%a ... set IP=UNEXPECTED FILE FORMAT set Win7c=FALSE set Win7d=FALSE for /f "usebackq tokens=1-5 delims= " %%d in ("%%a") do ( if "%%d %%e %%f"=="Volume in drive" set IP=%%g if /i "%%h"=="win7c.gho" set Win7c=TRUE if /i "%%h"=="win7d.gho" set Win7d=TRUE ) if "!Win7c!!Win7d!"=="TRUETRUE" ( echo ... Win7c.gho and Win7d.gho found; IP: !IP! >>"%LogFile%" echo %%a%Delim%!IP! ) else ( echo ... either Win7c.gho or Win7d.gho missing; IP: !IP! )) 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:
Author: pkwan replied at 2024-07-22 23:24:37
Sorry I may describe my question unclear. I should say that I have a number of log files (attached sample is one of the files) in such directory, and the content is similar to the one attached.
I would like to search the content of the log files for "win7c.gho" and "win7d.gho". If both exists in the content, I output the filename of the log file and the IP address in the content of the log file.
I would like to search the content of the log files for "win7c.gho" and "win7d.gho". If both exists in the content, I output the filename of the log file and the IP address in the content of the log file.
Expert: manumd replied at 2024-07-22 23:12:13
dir win7?.gho /s