- 1 year ago
Cant get tasklist batch file to output substring
Comment
1 / 1
If you're having trouble extracting a substring from the output of the tasklist
command in a batch file, here's an example that demonstrates how to do it:
batch@echo off
for /F "tokens=2 delims=," %%A in ('tasklist /nh /fi "imagename eq your_process_name.exe"') do (
set "process_id=%%~A"
echo Process ID: %process_id%
rem Do whatever you need with the process ID here
)
Replace "your_process_name.exe"
with the name of the process you want to extract the ID from.
Explanation:
- The
for /F
loop is used to iterate through the output of thetasklist
command. - The
"tokens=2 delims=,"
part tells the loop to consider the second token (process ID) and use comma as the delimiter. %%A
represents the variable that holds the process ID value.set "process_id=%%~A"
assigns the process ID value to theprocess_id
variable.echo Process ID: %process_id%
displays the extracted process ID (you can replace this line with your desired processing logic).
Save the batch file with a .bat
extension, run it, and it will output the process ID of the specified process.