Find Multiple Strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Find Multiple Strings

#1 Post by booga73 » 13 Feb 2013 12:27

hey, maybe I can get help on this .. .

I have a text file, named Piddb.txt which consists of this:

Image Name: System
PID: 4
Modules: N/A

Image Name: smss.exe
PID: 308
Modules: N/A

Image Name: csrss.exe
PID: 440
Modules: N/A

Image Name: wininit.exe
PID: 496
Modules: N/A

Image Name: csrss.exe
PID: 508
Modules: N/A

Image Name: services.exe
PID: 556
Modules: N/A

Image Name: lsass.exe
PID: 576
Modules: N/A

Image Name: lsm.exe
PID: 584
Modules: N/A

Image Name: winlogon.exe
PID: 708
Modules: N/A




I have code that will find 1 string value, Image Name: but I also want to include another string, PID: too.

for /F "tokens=2 delims=:" %%a in ('type "c:\temp1\PidDb.txt"^|find /i "Image Name:"') do (
echo Image Name:%%a
IF !linecount! EQU %maxlines% GOTO MyExit1

SET /A linecount+=1
)


best regards, Booga73

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: Find Multiple Strings

#2 Post by shirulkar » 13 Feb 2013 13:01

Following work for you

Code: Select all

@echo off
for /F "tokens=2 delims=:" %%a in ('type "c:\temp1\PidDb.txt"^|find /i "Image Name:"') do (
echo Image Name:%%a >>a.txt
IF !linecount! EQU %maxlines% GOTO MyExit1
SET /A linecount+=1
)

for /F "tokens=2 delims=:" %%b in ('type "c:\temp1\PidDb.txt"^|find /i "PID:"') do (
echo. >>b.txt
echo PID:%%b >>b.txt
IF !linecount! EQU %maxlines% GOTO MyExit1
SET /A linecount+=1
)

type b.txt >>d.txt

set dec=1
For /F "usebackq tokens=* delims=" %%c in ("a.txt") do call :File1 "%%c"
set dec1=0
del d.txt
del a.txt
del b.txt

exit /b

:File1
SET str1=%~1
SET Count=1
For /F "usebackq tokens=* skip=%dec% delims=" %%A in ("d.txt") do call :File2 "%%A"
set /a dec=%dec%+2

exit /b

:File2
SET str3=%~1
IF %Count% EQU 1 (
echo %str1%
echo %str3%
set /a Count=%Count%+1)

exit /b

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

Re: Find Multiple Strings

#3 Post by Squashman » 13 Feb 2013 13:16

Use FINDSTR

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Find Multiple Strings

#4 Post by mfm4aa » 13 Feb 2013 14:48

Other suggestion:

Code: Select all

@echo off &setlocal enabledelayedexpansion

set "fname=PidDb.txt"
set /a cnt=0

for /f "usebackqtokens=1-3" %%i in ("%fname%") do (
   if "%%i"=="Image" (
      set /a cnt+=1
      set "$image!cnt!=%%k"
   )
   if "%%i"=="PID:" set "$pid!cnt!=%%j"
)
for /l %%i in (1,1,!cnt!) do call echo %%$image%%i%% %%$pid%%i%%
endlocal


mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Find Multiple Strings

#5 Post by mfm4aa » 13 Feb 2013 15:56

& with a temp file:

Code: Select all

@echo off &setlocal

set "fname=PidDb.txt"
set "tname=%temp%\~"

for /f "usebackqtokens=1-3" %%i in ("%fname%") do (
   if "%%i"=="Image" <nul set /p=%%k:>>"%tname%"
   if "%%i"=="PID:" >>"%tname%" echo %%j
)
for /f "usebackqtokens=1,2delims=:" %%i in ("%tname%") do echo %%i %%j
del "%tname%"
endlocal


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

Re: Find Multiple Strings

#6 Post by foxidrive » 13 Feb 2013 21:35

I'm not clear on what you want to do but as Squashman says, to find the items with Image or PID: then try this:

Code: Select all

findstr "PID: Image" "c:\temp1\PidDb.txt"

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

Re: Find Multiple Strings

#7 Post by booga73 » 14 Feb 2013 16:10

Hi Folks,

okay, I'm laying it out. First, I had earlier was asking about how to complete a string search to find not just one but two strings in a text file. The first two sample scripts did work but couldn't seem to be able to add additional processing elements. Koo-dos goes to shirulkar & mfm4aa.

I would like to stay with my goal to tie in both the needed string values but further add additional processing elements to produce needed output.



Intended goal is to display final output:

Image Name: XXXXX PID: XXXX
==========================================
execution path of the image name
<future additional processed elements, data for later>
<future additional processed elements, data for later>
==========================================



I had quite the success in setting up a trial run of the following code:

for /F "tokens=2 delims=:" %%a in ('type "c:\temp1\PidDb.txt"^|find /i "Image Name:"') do (
echo Image Name:%%a
IF !linecount! EQU %maxlines% GOTO MyExit1
SET /A linecount+=1
)

This permitted me to find the Image Name in PidDb.txt file. I also was interested in needing to get the PID too.

I honestly didn't know how to get the Image & PID from even using FindStr, a command that I hadn't used and decided to continue with code that I've used and familiar with too. Squashman & foxidrive - koo-dos, contributed, thank you!

Second, I instead continued to use code more familiar with what I needed, a little longer process but was familiar with how the code worked.

:: code used to extract needed Image & PID information

for /F "tokens=2 delims=:" %%a in ('type "c:\temp1\PidDb.txt"^|find /i "Image Name:"') do ( blaw blaw )

for /F "tokens=2 delims=:" %%b in ('type "c:\temp1\PidDb.txt"^|find /i "PID:"') do ( blaw blaw )


I had good success because I was able to place Image & PID information into their own respective temporary .txt file

After successfully extracting what I needed, I used additional code to take it a step further to then combine Image & PID into one file as shown in the

following code:


setLocal enableDELAYedeXpansion
set N=
for /f "tokens=* delims= " %%a in (c:\temp1\PidImg.txt) do (
set /a N+=1
set M!N!=%%a
)

set N=
for /f "tokens=* delims= " %%a in (c:\temp1\ImgName.txt) do (
set /a N+=1
set I!N!=%%a
)

for /L %%i in (1 1 !N!) do (
>> c:\temp1\NewList2.txt echo.!I%%i!!M%%i!
)
ENDLOCAL


Now, I have NewList2.txt that had the following:

<Image Name> <PID>



Now, I need to get that information from NewList2.txt and process further data need.

Third, I am trying to retrieve the execution path of Image Name paired up with the PID number. I wrote my code as such:




SET /A maxlines=!LineNumb!
SET /A linecount=0

set num=
set num=%%c

for /F "tokens=2 delims=:" %%c in ('type "c:\temp1\PidDb.txt"^|find /i "Image Name:"') do (

echo ========================================== >> c:\temp1\ExeCute.txt
dir /b /s %num% >> c:\temp1\ExeCute.txt
echo ========================================== >> c:\temp1\ExeCute.txt
echo. >> c:\temp1\ExeCute.txt
echo. >> c:\temp1\ExeCute.txt

IF !linecount! EQU %maxlines% GOTO MyExit1
SET /A linecount+=1

)

:MyExit1

this code actually works and hadn't seen problems; this code takes the Image Name, the variable, that is identified and finds the executable path. (slow . . slow to do too) So, essentially the output should be like this:


==========================================
execution path of the image name
==========================================


The following code reads the two tokens per line from


for /f "tokens=1-2 delims= " %%a in (c:\temp1\NewList2.txt) do (

echo Process Name:%tok1% PID Name:%tok2% >> c:\temp1\ExeCute.txt

)

Image Name: XXXXX PID: XXXX



Now, the goal though is to take both outputs and place it into a single file as this:



Image Name: XXXXX PID: XXXX
==========================================
execution path of the image name
<future additional processed elements, data for later>
<future additional processed elements, data for later>
==========================================


Now, I'm stuck to the point where, I feel that I'm using too many .txt files to temporary store data and that I need to get both my For loops to cohesively work together as to produce the target output. I wrote the following code; it's not correct; I tried to merge two for loops into a compound loop. The code will run,
the final output after this code is run, the image name & pid are correctly piped to the final .txt file, but the execution of the path for the image name is not resolving correctly. The for loop that tries to identify the executable path is not clearing out the %%c variable and my sample output in the final .txt file shows the same executable path throughout the final .txt file.


SET /A maxlines=!LineNumb!
SET /A linecount=0

set num=
set num=%%c
set tok1=
set tok1=%%a
set tok2=
set tok2=%%b

for /F "tokens=2 delims=:" %%c in ('type "c:\temp1\PidDb.txt"^|find /i "Image Name:"') do (

for /f "tokens=1-2 delims= " %%a in (c:\temp1\NewList2.txt) do (

echo Process Name:%tok1% PID Name:%tok2% >> c:\temp1\ExeCute.txt

echo ========================================== >> c:\temp1\ExeCute.txt
dir /b /s %num% >> c:\temp1\ExeCute.txt
echo ========================================== >> c:\temp1\ExeCute.txt
echo. >> c:\temp1\ExeCute.txt
echo. >> c:\temp1\ExeCute.txt

IF !linecount! EQU %maxlines% GOTO MyExit1
SET /A linecount+=1

)
)

:MyExit1

Now, I've run both the for loops independantly of each other to verify they work, yes. I've tried to research (google) how to construct a compound For Loop, nothing that is in good detail so I pieced together what I have now of the 2 for loops as it is. My intensions is to, once the compound for loop works, I want to be able to add additional processing elements but the for loops are a pain.

Do you have insight as to this compound for loop?


:: beginning of my script
:: ----------------------

@ECHO OFF

cd\
c:
cd temp1
cls

FOR /F "TOKENS=2" %%A IN ('tasklist /NH /fi "PID GE 0"') DO (
ECHO %%A >> c:\temp1\PID0.txt)
more +1 c:\temp1\PID0.txt > c:\temp1\PID1.txt
erase /s /q c:\temp1\PID0.txt


:: code start to identify number of lines in a text
Set FileName=PID1.txt
Set /a LineNumb=0
for /f "tokens=*" %%a in ('find /c /v "" %FileName%') do set /a LineNumb=%%a
:: code end to identify number of lines in a text


SET /A maxlines=!LineNumb!
SET /A linecount=0

FOR /F "tokens=*" %%b IN (c:\temp1\PID1.txt) DO (

IF !linecount! EQU %maxlines% GOTO ExitLoop
tasklist /M /FO "LIST" /FI "PID EQ %%b" >> c:\temp1\PidDb.txt


SET /A linecount+=1
)

:ExitLoop

cd\
c:
cd\



SET /A maxlines=!LineNumb!
SET /A linecount=0

set num=
set num1=%%a

for /F "tokens=2 delims=:" %%a in ('type "c:\temp1\PidDb.txt"^|find /i "Image Name:"') do (
:: dir /b /s %num% >> c:\temp1\ExeCute.txt
IF !linecount! EQU %maxlines% GOTO MyExit1
@echo %%a >> c:\temp1\ImgName.txt
SET /A linecount+=1
)

:MyExit1

SET /A maxlines=!LineNumb!
SET /A linecount=0

set num=
set num2=%%b

for /F "tokens=2 delims=:" %%b in ('type "c:\temp1\PidDb.txt"^|find /i "PID:"') do (

IF !linecount! EQU %maxlines% GOTO MyExit2
@echo %%b >> c:\temp1\PidImg.txt
SET /A linecount+=1
)

:MyExit2


setLocal enableDELAYedeXpansion
set N=
for /f "tokens=* delims= " %%a in (c:\temp1\PidImg.txt) do (
set /a N+=1
set M!N!=%%a
)

set N=
for /f "tokens=* delims= " %%a in (c:\temp1\ImgName.txt) do (
set /a N+=1
set I!N!=%%a
)

for /L %%i in (1 1 !N!) do (
>> c:\temp1\NewList2.txt echo.!I%%i!!M%%i!
)
ENDLOCAL







SET /A maxlines=!LineNumb!
SET /A linecount=0

set num=
set num=%%c
set tok1=
set tok1=%%a
set tok2=
set tok2=%%b

for /F "tokens=2 delims=:" %%c in ('type "c:\temp1\PidDb.txt"^|find /i "Image Name:"') do (

for /f "tokens=1-2 delims= " %%a in (c:\temp1\NewList2.txt) do (

echo Process Name:%tok1% PID Name:%tok2% >> c:\temp1\ExeCute.txt

echo ========================================== >> c:\temp1\ExeCute.txt
dir /b /s %num% >> c:\temp1\ExeCute.txt
echo ========================================== >> c:\temp1\ExeCute.txt
echo. >> c:\temp1\ExeCute.txt
echo. >> c:\temp1\ExeCute.txt

IF !linecount! EQU %maxlines% GOTO MyExit1
SET /A linecount+=1

)
)

:MyExit1

Start c:\temp1\ExeCute.txt
exit


:: ----------------------
:: end of my script


thank you .. . . .

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

Re: Find Multiple Strings

#8 Post by foxidrive » 14 Feb 2013 18:14

booga73 wrote:Intended goal is to display final output:

Image Name: XXXXX PID: XXXX
==========================================
execution path of the image name
<future additional processed elements, data for later>
<future additional processed elements, data for later>
==========================================


Usually it's better to say what you want to do, and let the readers figure out how to do it.
Start with the command that produces the info, and give accurate examples of what you want in the end file.

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

Re: Find Multiple Strings

#9 Post by abc0502 » 14 Feb 2013 18:52

I think he want a report in the form he posted for each process, so this form will be repeated for each image name.

@booga73
I read your post but it's hard to keep track.
This is a description of a simple way might help you.

> collect data first, each data ( image name, PID, path, ..etc ) make it in a separate file.
> Create empty file that will hold the final data.
> Then combine each one of the data field files with that empty file with a semi-colon as a separator.
> Finally read that combined file with a for loop which will take each field and add it to a function
that create your report in the form you desire.

Combine file should be done by using this Function (was taken from a snippets code here ) :
Edited: Fixed a problem might happen if using a file path or name that contain a space in it by adding 'type "%F1%"' instead of %F1%

Code: Select all

:Combine <1st_file> <2nd_file> <out_file> <Optinional_Separator>
rem This will combine two files in the formate Line1_from_file1 %Seprator% Line1_from_file2
rem if you didn't set the separator it will be set to semi-colon ;
SET "F1=%~1"
SET "F2=%~2"
SET "Out=%~3"
SET "Separator=%~4"
IF NOT Defined Seprator SET "Separator="
(
   FOR /F "delims=" %%A IN ('TYPE "%F1%"') DO (
      SETLOCAL EnableDelayedExpansion
         SET /P line=
         Echo.%%A!Separator!!line!
      ENDLOCAL
   )
)<"%F2%">>"%Out%"
GOTO :EOF
it is based on a code in one of the posts here, i think foxidrive created
Last edited by abc0502 on 14 Feb 2013 19:39, edited 1 time in total.

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

Re: Find Multiple Strings

#10 Post by foxidrive » 14 Feb 2013 18:57

I got the impression that he wants to search for one image name and PID and display it.

The problem is that we don't know for sure - nor do we know what the extra details that are required as listed in the example.

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

Re: Find Multiple Strings

#11 Post by abc0502 » 14 Feb 2013 19:09

I missed that,
Then he must specify what other data will be needed as modifying the batch after creating it will be hard

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

Re: Find Multiple Strings

#12 Post by abc0502 » 14 Feb 2013 21:12

I guess this is an easier way to do what you need,
This batch should process your file and produce the data needed, but if you wanted to add extra data , the easiest way will be to provide these data with your PIDdb.txt file, and then you will have to make small changes in these sections :

> Collect Data from DBfile : Add extra IF statements.
> Combine Files to RFile : Combine the files from the previous change.
> Read The RFile and Produce the Final Report : Modify the tokens and add the new inputs to GenReport Function.
> :GenReport Function : Added extra %~X to take the new inputs from the previous change.


Or create another batch file that will create extra file with the data needed then process it by adding new section to this batch.

It depends on the WMIC to get the process executable path, if it's not working on your machine you will not get the process location.

You can test it on the PIDdb.txt you provided on your first post.
Change the first two variables to match your settings.

Code: Select all

@Echo OFF

REM =====[ Settings ]=========================================
SET "Main=%userprofile%\Desktop"
SET "DBfile=%Main%\PIDdb.txt"
SET "TMPfolder=%Main%\TMPfolder"
SET "RFile=%TMPFolder%\Report.csv"
SET "Final=%TMPFolder%\Final_Report.txt"

REM =====[ Set Environment ]==================================
IF EXIST "%TMPfolder%" RMDIR /S /Q "%TMPfolder%" 2>NUL
MD "%TMPfolder%" 2>NUL

REM =====[ Collect Data from DBfile ]=========================
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=1* delims=:" %%A IN ('TYPE "%DBfile%"') DO (
   SET "Line=%%A:%%B"
   REM Data has one leading space
   SET "Data=%%B"
   REM we use !Data:~1! to remove the leading space
   IF /I "!Line:~0,5!" == "Image" (
      Echo !Data:~1!>>"%TMPfolder%\ImageName.txt"
      REM Get Process Location Too using the image name
      REM The search in PrecessLocation Function is NOt case sensitive, remove the /I switch to make it case sensitive
      CALL :ProcessLocation "!Data:~1!" "%TMPfolder%\ImageLocation.txt"
      )
   IF /I "!Line:~0,3!" == "PID" Echo !Data:~1!>>"%TMPfolder%\ImagePID.txt"
   IF /I "!Line:~0,7!" == "Modules" Echo !Data:~1!>>"%TMPfolder%\Modules.txt"
   )


REM =====[ Combine Files to RFile ]===========================
rem %RFile% is used as the final file, any other combine files will be TMP1, TMP2, .. etc
rem use this order to get a correct order of data fields:
rem In_file1 + In_file2 = out1
rem out1 + In_file3 = out2
rem out2 + In_file4 = out3
rem Conclusion: out file always first file

CALL :Combine "%TMPfolder%\ImageName.txt" "%TMPfolder%\ImagePID.txt" "%TMPfolder%\TMP1"
CALL :Combine "%TMPfolder%\TMP1" "%TMPfolder%\Modules.txt" "%TMPfolder%\TMP2"
CALL :Combine "%TMPfolder%\TMP2" "%TMPfolder%\ImageLocation.txt" "%RFile%"

REM =====[ Read The RFile and Produce the Final Report ]=========
rem set the tokens number based on the data fields in the csv file
rem this for command require Delayed Extension enabled [we did up in line 15]
FOR /F "tokens=1-4 delims=;" %%A IN ('TYPE "%RFile%"') Do (
   REM Generate Report
   CALL :GenReport "%%A" "%%B" "%%C" "%%D"
   )

REM =====[ Open Report ]=========================================
START "Report" "%Final%"
EXIT /B

REM =====[ Functions Start from here ]===========================
:Combine <1st_file> <2nd_file> <out_file> <Optinional_Separator>
rem This will combine two files in the formate Line1_file1 %Seprator% Line1_file2
rem if you didn't set the separator it will be set to semi-colon ;
SET "F1=%~1"
SET "F2=%~2"
SET "Out=%~3"
SET "Separator=%~4"
IF NOT Defined Seprator SET "Separator=;"
(
   FOR /F "delims=" %%A IN ('Type "%F1%"') DO (
      SETLOCAL EnableDelayedExpansion
         SET /P line=
         Echo.%%A!Separator!!line!
      ENDLOCAL
   )
)<"%F2%">>"%Out%"
GOTO :EOF

:ProcessLocation <Process_Name> <Out_file>
rem <process_name> : The process that you want it's location
rem <Variable> : a variable name to set the location to.
SETLOCAL EnableDelayedExpansion
WMIC Process list brief | FIND /I "%~1" >NUL
SET "WMICresult=%ERRORLEVEL%"
IF "%WMICresult%"=="1" Echo NOT Running>>"%~2"
IF "%WMICresult%"=="0" (
   FOR /F "skip=1 tokens=*" %%A IN ('WMIC PROCESS where name^="%~1" GET ExecutablePath') DO Echo %%~dpA>>"%~2"
   )
GOTO :EOF

:GenReport <Process_name> <Process_PID> <Modules> <Process_Location>
(
Echo Image Name: %~1 PID: %~2
Echo ==========================================
Echo Modules : %~3
Echo Location: %~4
Echo future additional processed elements, data for later
Echo future additional processed elements, data for later
Echo ==========================================
Echo.
Echo.
Echo.
)>>"%Final%"
GOTO :EOF


Credit goes to : Link
The ProcessLocation Function wasn't working until i found this piece of code. :D

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

Re: Find Multiple Strings

#13 Post by booga73 » 15 Feb 2013 14:19

after attempting to run the code abc0502 posted,

I did get error:

C:\Temp1>PIDTest2b.bat
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file C:\Users\brian.hazuga\Desktop\TMPfolder\Final_Report.txt.

I am shortly going to post a modification to my script; which has changed but same concept.

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

Re: Find Multiple Strings

#14 Post by booga73 » 15 Feb 2013 14:40

1. Create List of All Running Processes
2. Identify number of lines in final output list of step 1. Need to because this helps how many times my for loops later in script runs.
3. Create a separate list of processes & PIDs only.
4. Combine what is found in step 3, a list of both Processes and PIDs only.
5. Find executable path of Running Process, where the processes were identified in step 3
6. Now, need to combine what was generated in Step4 and Step5
for example in step 6, need to display the following:

Process Name PID #
==========================================
executable path information which step number 5 produced.
==========================================

I had examined my code and didn't like how it was written.

I still get this error occuring when I run the script:

Missing operator.
Missing operator.

but, after correcting and changing the script, I had documented each section as to what occurs and purpose, hopefully it will be clearer.
I would be a bonus if I could figure out how to extract and and keep path information.

For example, if I have a path with a file name, C:\Windows\System32\smss.exe
I would like to get smss.exe stripped from the path and take the path " C:\Windows\System32\ " and simply place it into a variable for later processing.

such as . . . <a href="%%c">Process Name</a>

Process Name PID #
==========================================
executable path information which step number 5 produced.
==========================================

will, by the time the code zips along to all the routines, when it comes to identifying the process executable path, it's rather slow. When I run my script on my immediate workstation that has a raid 0 solid state drives, it's abit faster, but slow.

Just looking to get help to complete Step #6 to get the data to a good text file, that I can then continue to work on the html coding myself. Just need to figure out how to get the the process name, pid num and executable path. My initial script was to create a compound for loop ( 2 for loops) to combine and place the needed data into text file for later processing to html. I ask for no fancy but basic understanding on how to do that; just simply stuck.

I appreciate all the help. I've posted my code below here, just need to copy/paste the code into a batch file w/in the temp1 subfolder on c: drive. Best Regards, booga73

Code: Select all

@ECHO OFF

cd\
c:
cd temp1
cls

:: 1.
:: Beginning of Task 1 Create List of All Running Processes & PIDS.
:: The Processes & PIDS reside in column 1 (processes) & Column 2 (PIDS) in tsklst2.txt
:: the tsklst2.txt will show all the running processes and pid number associated to each process
:: *************************************


tasklist > c:\temp1\tsklst1.txt
more +4 c:\temp1\tsklst1.txt > c:\temp1\tsklst2.txt
erase /s /q c:\temp1\tsklst1.txt
cls

:: *************************************
:: End of Task 1 Create List of All Running Processes & PIDs



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


:: 3
:: beginning of code to read all tokens in column 1 & 2 of tsklst2.txt
:: ProNme1.txt shows the unique processes on the computer
:: PID#.txt shows the unique processor identification of each process
:: *************************************
SETLOCAL
for /F "tokens=1" %%a in (c:\temp1\tsklst2.txt) do (
echo %%a >> c:\temp1\ProNme1.txt
)
ENDLOCAL

SETLOCAL
for /F "tokens=2" %%a in (c:\temp1\tsklst2.txt) do (
echo %%a >> c:\temp1\PID#.txt
)
ENDLOCAL
:: *************************************
:: end of code to read all tokens in column 1 & 2 of tsklst2.txt


:: 4
:: Beginning, this code here takes all the tokens from both ProNme1.txt and PID#.txt and places them into ProAnPid.txt
:: *************************************

setLocal enableDELAYedeXpansion
set N=
for /f "tokens=* delims= " %%a in (c:\temp1\PID#.txt) do (
set /a N+=1
set M!N!=%%a
)

set N=
for /f "tokens=* delims= " %%a in (c:\temp1\ProNme1.txt) do (
set /a N+=1
set I!N!=%%a
)

for /L %%i in (1 1 !N!) do (
>> c:\temp1\ProAnPid.txt echo.!I%%i!!M%%i!
)
ENDLOCAL

:: erase /s /q c:\temp1\ProNme1.txt
:: erase /s /q c:\temp1\PID#.txt

:: *************************************
:: End, this code here takes all the tokens from both ProNme1.txt & PID#.txt and places them into Pro&Pid.txt




:: 5
:: Find executable path of Running Process
:: Beginning code to identify path of running process
:: dir  /b /s is the command that processes and finds the path of the running process
:: *************************************


:: this code verifies that when processing this ExeCute code, dir /b /s needs to be run from the root of drive c:
:: ----------------------------
c:     
cd\
:: ----------------------------


set num=
set num=%%b


SETLOCAL
SET /A maxlines=!LineNumb!
SET /A linecount=0

FOR /F "tokens=*" %%b IN (c:\temp1\ProNme1.txt) DO (

echo ========================================== >> c:\temp1\ExeCute.txt
dir  /b /s  %num%  >> c:\temp1\ExeCute.txt
echo ========================================== >> c:\temp1\ExeCute.txt
echo. >> c:\temp1\ExeCute.txt
echo. >> c:\temp1\ExeCute.txt

  IF !linecount! EQU %maxlines% GOTO ExitLoop
 
  SET /A linecount+=1
)

:ExitLoop
ENDLOCAL

:: *************************************
:: End code to identify path of running process




pause
exit

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Find Multiple Strings

#15 Post by mfm4aa » 15 Feb 2013 15:15

Strip file path & file name from full path, see also the code by abc0502
e.g.

Code: Select all

set "commandline=C:\Windows\System32\smss.exe"
for %%i in ("%commandline%") do set "fpath=%%~dpi" &set "fname=%%~nxi"
echo %fpath% # %fname%


Output:

Code: Select all

C:\WINDOWS\system32\ # smss.exe

Post Reply