Find Multiple Strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find Multiple Strings

#16 Post by foxidrive » 15 Feb 2013 22:22

What OS is this running under? Has it got WMIC ?

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find Multiple Strings

#17 Post by booga73 » 15 Feb 2013 22:34

I am running windows 7 & windows 8.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find Multiple Strings

#18 Post by foxidrive » 15 Feb 2013 22:49

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
=================================



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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find Multiple Strings

#19 Post by abc0502 » 15 Feb 2013 23:22

@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.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find Multiple Strings

#20 Post by booga73 » 16 Feb 2013 07:55

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?

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find Multiple Strings

#21 Post by booga73 » 16 Feb 2013 07:56

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

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find Multiple Strings

#22 Post by booga73 » 16 Feb 2013 08:11

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

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find Multiple Strings

#23 Post by booga73 » 16 Feb 2013 08:29

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

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find Multiple Strings

#24 Post by booga73 » 16 Feb 2013 09:15

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Find Multiple Strings

#25 Post by Squashman » 16 Feb 2013 12:29

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find Multiple Strings

#26 Post by abc0502 » 16 Feb 2013 23:57

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,

Code: Select all

for /f "tokens=2 delims=:" %%a in ('find /c /v "" "%fileName%"') do set /a "LineNumb=%%a"
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:

Code: Select all

For /F "tokens=1 delims=:" %%a in ('findstr /N "$" "list.txt"') Do echo %%a
pause
The "$" should be any character that you know it won't exist in your file.

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Find Multiple Strings

#27 Post by dbenham » 17 Feb 2013 10:22

Ouch, this thread is painful :lol:

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 :D

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 :wink:

Dave Benham

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find Multiple Strings

#28 Post by booga73 » 27 Feb 2013 22:01

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:

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.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find Multiple Strings

#29 Post by booga73 » 28 Feb 2013 10:15

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:

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find Multiple Strings

#30 Post by foxidrive » 28 Feb 2013 16:57

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.

Post Reply