Find Multiple Strings
Moderator: DosItHelp
Re: Find Multiple Strings
What OS is this running under? Has it got WMIC ?
Re: Find Multiple Strings
I am running windows 7 & windows 8.
Re: Find Multiple Strings
This creates the entries in this format:
taskhostex.exe PID 4100
=================================
C:\WINDOWS\system32\taskhostex.exe
=================================
TeamViewer.exe PID 4244
=================================
C:\Program Files\TeamViewer\Version8\TeamViewer.exe
=================================
explorer.exe PID 4660
=================================
C:\WINDOWS\Explorer.EXE
=================================
taskhostex.exe PID 4100
=================================
C:\WINDOWS\system32\taskhostex.exe
=================================
TeamViewer.exe PID 4244
=================================
C:\Program Files\TeamViewer\Version8\TeamViewer.exe
=================================
explorer.exe PID 4660
=================================
C:\WINDOWS\Explorer.EXE
=================================
Code: Select all
@echo off
del filelist.log 2>nul
for /f "tokens=1,2 delims=," %%a in ('tasklist /FO CSV ^|find /i ".exe"') do (
for /f "delims=" %%c in ('WMIC PROCESS where name^="%%~a" GET ExecutablePath ^|find "\"') do (
echo %%~a PID %%~b
echo =================================
cmd /c echo %%c
echo =================================
)
)>>filelist.log
Re: Find Multiple Strings
@booga73 you said that you get an error message, did you changed the 1st two variables, i mean did you set them to match your files location ?
I tested many times and it work, i use windows XP. but the error you posted is not related to windows version.
I tested many times and it work, i use windows XP. but the error you posted is not related to windows version.
Re: Find Multiple Strings
foxidrive,
Good Morning, will check out that code. Question, is that what is needed to combine the needed info in the last step? or is it replacing my entire script?
Good Morning, will check out that code. Question, is that what is needed to combine the needed info in the last step? or is it replacing my entire script?
Re: Find Multiple Strings
abc0502,
Good Morning,
I'll check out that error that I mentioned; it could be from the workstation I was running. I got testing to do with my script :<)
Thank you all for your assistance, b/ booga73
Good Morning,
I'll check out that error that I mentioned; it could be from the workstation I was running. I got testing to do with my script :<)
Thank you all for your assistance, b/ booga73
Re: Find Multiple Strings
abc0502,
Task2
It is in my routine here:
it is because of this code how I get my results to identify number of lines in a text. Actually, I was informed of this earlier in another posting, but somehow, the value of the number of lines is still being passed through. LineNumb shows not just 68 but also . ...
C:\temp1>set /a LineNumb=---------- TSKLST2.TXT: 68
Missing operator.
:: 2
:: code start to identify number of lines in a text & hold value in LineNumb
:: *************************************
SETLOCAL
Set FileName=tsklst2.txt
Set /a LineNumb=0
for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
ENDLOCAL
:: *************************************
:: code end to identify number of lines in a text
Task2
It is in my routine here:
it is because of this code how I get my results to identify number of lines in a text. Actually, I was informed of this earlier in another posting, but somehow, the value of the number of lines is still being passed through. LineNumb shows not just 68 but also . ...
C:\temp1>set /a LineNumb=---------- TSKLST2.TXT: 68
Missing operator.
:: 2
:: code start to identify number of lines in a text & hold value in LineNumb
:: *************************************
SETLOCAL
Set FileName=tsklst2.txt
Set /a LineNumb=0
for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
ENDLOCAL
:: *************************************
:: code end to identify number of lines in a text
Re: Find Multiple Strings
Foxidrive,
Yes, I ran the code. Your code is much more efficient than what I was doing. That is way too radical, lol.
.. hmmm just now I'm kicking myself in the rear for the long way about completing my code .. .
Each time I read through here at dostips, I always get a humbling experience and always grateful for all the team work.
very respectfully, Booga73
Yes, I ran the code. Your code is much more efficient than what I was doing. That is way too radical, lol.
.. hmmm just now I'm kicking myself in the rear for the long way about completing my code .. .
Each time I read through here at dostips, I always get a humbling experience and always grateful for all the team work.
very respectfully, Booga73
Re: Find Multiple Strings
mfm4aa,
good morning, thank you for your assistance on this:
set "commandline=C:\Windows\System32\smss.exe"
for %%i in ("%commandline%") do set "fpath=%%~dpi" &set "fname=%%~nxi"
echo %fpath% # %fname%
v/r Booga73
good morning, thank you for your assistance on this:
set "commandline=C:\Windows\System32\smss.exe"
for %%i in ("%commandline%") do set "fpath=%%~dpi" &set "fname=%%~nxi"
echo %fpath% # %fname%
v/r Booga73
Re: Find Multiple Strings
booga73 wrote:abc0502,
Task2
It is in my routine here:
it is because of this code how I get my results to identify number of lines in a text. Actually, I was informed of this earlier in another posting, but somehow, the value of the number of lines is still being passed through. LineNumb shows not just 68 but also . ...
C:\temp1>set /a LineNumb=---------- TSKLST2.TXT: 68
Missing operator.
:: 2
:: code start to identify number of lines in a text & hold value in LineNumb
:: *************************************
SETLOCAL
Set FileName=tsklst2.txt
Set /a LineNumb=0
for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
ENDLOCAL
:: *************************************
:: code end to identify number of lines in a text
You posted in someone elses thread about counting lines and saying that was the code you always used to count lines. The weird thing was you posted your code after Foxidrive and I already posted two working solutions. If you go back to that thread and look at our solutions you will see why that your code doesn't work.
Re: Find Multiple Strings
It is as Squashman said in the previous thread, you are assigning the whole sentence "---------- TSKLST2.TXT: 68" to the variable. you will need to use tokens and delims to ignore the 1st part and only take what is after the ":"
So your code should have a tokens=2 and a delims=: and but the result will have on leading space, so to remove it use set /a to assign the number to the variable and it will remove that space,The error happen because your set /a used to do math operations and with your previous code you can do math operation on text letters.
I mainly use one of these to get total line numbers :
This one ignore empty lines and doesn't count them:The "$" should be any character that you know it won't exist in your file.
This count empty lines and I prefer sing it:
So your code should have a tokens=2 and a delims=: and but the result will have on leading space, so to remove it use set /a to assign the number to the variable and it will remove that space,
Code: Select all
for /f "tokens=2 delims=:" %%a in ('find /c /v "" "%fileName%"') do set /a "LineNumb=%%a"
I mainly use one of these to get total line numbers :
This one ignore empty lines and doesn't count them:
Code: Select all
For /F "tokens=1 delims=:" %%a in ('findstr /N "$" "list.txt"') Do echo %%a
pause
This count empty lines and I prefer sing it:
Code: Select all
for /f %%i in ('type "file.txt"^|find /v /c ""') do echo %%i
pause
Re: Find Multiple Strings
Ouch, this thread is painful
I believe booga73 simply wants to get a listing of active processes, including image name, processId, and execution path, formatted a particular way. A tremendous amount of inefficient code has been written without need - WMIC provides almost everything you want very quickly.
The following command will list all the info you want for most processes. No need to search the disk drive for the executable path
You can use FOR /F to easily parse and process the output of the command, assuming none of the paths contain a comma. I've never seen a comma, but it is possible. I add the extra threadCount column to avoid including the <CR> artifact that is appended to each line when the output is processed by FOR /F. I also verify the image name includes an extension so as to filter out the System and System Idle processes.
The only problem is that some processes hide their executable path. Those processes are easy to identify because FOR /F parses fewer columns. You could use DIR /B /S to list all locations, but I prefer FOR /R. Your existing code assumes the process was launched from the current drive, when actually it could be launched from any volume available to your system. (Actually, the executable may not still be accessible if it was deleted or a removable media was removed). You can use another WMIC command to loop through all available drives.
I need to call a subroutine to execute my recursive path search because the FOR /R option can not use a FOR variable or delayed expansion.
It can take a long time to recursively search all drives for the executable. The executable may appear in multiple locations. If you don't care about listing all paths, but can accept listing the first one found, then the performance can be improved tremendously. The %%~$pathA syntax will quickly search the PATH for the file. If not found, then you can recursively search the drives. It is not possible to break out of a FOR /R loop, but it can be executed in a new CMD session and the session can EXIT with an error once if finds the file. An added bonus is the parsing of the FOR /R command is delayed, so there is no longer a need to CALL a subroutine. The CALL; command is used to force the ERRORLEVEL to 0.
Hope it helps
Dave Benham
I believe booga73 simply wants to get a listing of active processes, including image name, processId, and execution path, formatted a particular way. A tremendous amount of inefficient code has been written without need - WMIC provides almost everything you want very quickly.
The following command will list all the info you want for most processes. No need to search the disk drive for the executable path
Code: Select all
wmic process get executablePath, name, processId, threadCount /format:csv
You can use FOR /F to easily parse and process the output of the command, assuming none of the paths contain a comma. I've never seen a comma, but it is possible. I add the extra threadCount column to avoid including the <CR> artifact that is appended to each line when the output is processed by FOR /F. I also verify the image name includes an extension so as to filter out the System and System Idle processes.
The only problem is that some processes hide their executable path. Those processes are easy to identify because FOR /F parses fewer columns. You could use DIR /B /S to list all locations, but I prefer FOR /R. Your existing code assumes the process was launched from the current drive, when actually it could be launched from any volume available to your system. (Actually, the executable may not still be accessible if it was deleted or a removable media was removed). You can use another WMIC command to loop through all available drives.
I need to call a subroutine to execute my recursive path search because the FOR /R option can not use a FOR variable or delayed expansion.
Code: Select all
@echo off
for /f "skip=2 tokens=2-5 delims=," %%A in (
'"wmic process get executablePath,name,processId,threadCount /format:csv"'
) do if "%%A" neq "" (
if "%%D" neq "" (
echo Image Name: %%B PID: %%C
echo ===============================================================
echo %%~dpA
) else if "%%~xA" neq "" (
echo Image Name: %%A PID: %%B
echo ===============================================================
for /f "skip=2 tokens=2 delims=," %%E in (
'"wmic logicaldisk get deviceId,status /format:csv"'
) do call :listPaths %%E "%%A"
)
echo ===============================================================
echo(
)
exit /b
:listPaths drive file
for /r %1\ %%F in ("%~2*") do if "%%~nxF" equ "%~2" echo %%~dpF
exit /b
It can take a long time to recursively search all drives for the executable. The executable may appear in multiple locations. If you don't care about listing all paths, but can accept listing the first one found, then the performance can be improved tremendously. The %%~$pathA syntax will quickly search the PATH for the file. If not found, then you can recursively search the drives. It is not possible to break out of a FOR /R loop, but it can be executed in a new CMD session and the session can EXIT with an error once if finds the file. An added bonus is the parsing of the FOR /R command is delayed, so there is no longer a need to CALL a subroutine. The CALL; command is used to force the ERRORLEVEL to 0.
Code: Select all
@echo off
for /f "skip=2 delims=, tokens=2-5" %%A in (
'"wmic process get executablePath,name,processId,threadCount /format:csv"'
) do if "%%A" neq "" (
if "%%D" neq "" (
echo Image Name: %%B PID: %%C
echo ===============================================================
echo %%~dpA
) else if "%%~xA" neq "" (
echo Image Name: %%A PID: %%B
echo ===============================================================
if "%%~$path:A" neq "" (echo %%~$path:A) else (
call;
for /f "skip=2 tokens=2 delims=," %%E in (
'"wmic logicaldisk get deviceId,status /format:csv"'
) do if not errorlevel 1 cmd /c for /r %%E\ %%F in ("%%A*"^) do @if "%%A" equ "%%~nxF" echo %%~dpF^&exit 1
)
)
echo ===============================================================
echo(
)
Hope it helps
Dave Benham
Re: Find Multiple Strings
Hi folks, I'm back, in a good way. I had studdied the code that was worked with everybody and studdied as much as I could. I am limited to what I can do, but believe in the power of keep it simple... err... KISS theory anyway. I really value the illustrations provided here on dosTips, awesome. I am a visual learner and not an advance type guy, but I can break out the code at some levels and understand. Like I've mentioned before, I really apprieciate the help provided and illustrations. Excuse my spelling
At this time, I have the necessary formatting the way I like it. I simply can't carry over my value from my for loop below into my subroutine and need a reason why that it's not working and how I can fix it. This is like my 3rd or 4th revision of this code and made modifications, but this is by far the best that I can come up with, understand it and like it. i've placed commented out explainations below in my code to help point the way.
best regards, Booga73
here is my code:
At this time, I have the necessary formatting the way I like it. I simply can't carry over my value from my for loop below into my subroutine and need a reason why that it's not working and how I can fix it. This is like my 3rd or 4th revision of this code and made modifications, but this is by far the best that I can come up with, understand it and like it. i've placed commented out explainations below in my code to help point the way.
best regards, Booga73
here is my code:
Code: Select all
@ECHO OFF
removed my code, and got it working . . .
pause
exit
Last edited by booga73 on 28 Feb 2013 10:27, edited 1 time in total.
Re: Find Multiple Strings
it has finally dawned on me to figure this out, Boom! haha!
oh. . .by the way, the movie, Here Comes the Boom, it's awesome.
okay, I figured out how to use the subroutine.. .. . .
an example:
in the initial code, when the subroutine is called, :s_Staff Smith 100, it's the label that is being called and the two values, Smith and 100 that is being passed to the subroutine :s_Staff; the subroutine echo's the name and rate for the two values that had been passed to it from the initial call of the subroutine.
Now, though, why use %1 and %2? I can't figure that out; but in my code from above, I had passed my variables %%a and %%b into my subroutine but my subroutine failed, but when in the subroutine when I replaced the %%a and %%b values with simply %1 & %2, I got my code to work and loop through. OMG, lol, this is sooo awesome.
oh. . .by the way, the movie, Here Comes the Boom, it's awesome.
okay, I figured out how to use the subroutine.. .. . .
an example:
Code: Select all
@ECHO OFF
SETLOCAL
CALL :s_staff SMITH 100
GOTO s_last_bit
:s_staff
ECHO Name is %1
ECHO Rate is %2
GOTO :eof
in the initial code, when the subroutine is called, :s_Staff Smith 100, it's the label that is being called and the two values, Smith and 100 that is being passed to the subroutine :s_Staff; the subroutine echo's the name and rate for the two values that had been passed to it from the initial call of the subroutine.
Now, though, why use %1 and %2? I can't figure that out; but in my code from above, I had passed my variables %%a and %%b into my subroutine but my subroutine failed, but when in the subroutine when I replaced the %%a and %%b values with simply %1 & %2, I got my code to work and loop through. OMG, lol, this is sooo awesome.
Re: Find Multiple Strings
booga73 wrote:it has finally dawned on me to figure this out, Boom! haha!
oh. . .by the way, the movie, Here Comes the Boom, it's awesome.
okay, I figured out how to use the subroutine.. .. . .
Now, though, why use %1 and %2? I can't figure that out;
Good on ya! That's a step for mankind.
See, back in the olden days we had to create a temporary batch file and call the batch file where using %1 %2 etc is obvious because it is a called batch file.
Nowadays you can call a subroutine/label inside the same batch file as though it were a separate batch file - hence it also uses %1 and %2 etc
I hope I made sense there - but you figured out how to use it and that counts.
One extra thing: where you have 'GOTO s_last_bit' you could use 'GOTO :EOF' and the batch file will end at that point.