如何将tasklist输出的进程DLL信息拆分至多个TXT文件?
Got it, let's figure out how to split that single tasklist output file into individual TXTs for each process. Since we're working in CMD, a batch script is the most straightforward solution here. Here's what you can do:
Step 1: Use a Batch Script to Split the Output
You can create a reusable batch file that either works with an existing output.txt or generates the output and splits it in one go. Here are both options:
Option 1: Split an Existing output.txt
Save this as split_dlls.bat in the same folder as your output.txt:
@echo off setlocal enabledelayedexpansion set "input_file=output.txt" set "current_file=" :: Loop through each line of the input file for /f "delims=" %%a in ('type "%input_file%"') do ( set "line=%%a" :: Skip the header line (starts with "映像名称") if "!line:~0,4!"=="映像名称" ( continue ) :: Check if this line is a process entry (contains a numeric PID) echo !line! | findstr /r "[0-9][0-9]*" >nul if !errorlevel! equ 0 ( :: Extract process name and PID from the line for /f "tokens=1,2" %%b in ("!line!") do ( set "proc_name=%%b" set "proc_pid=%%c" ) :: Clean up the process name to avoid invalid filename characters set "proc_name=!proc_name:.exe=!" :: Remove .exe extension set "proc_name=!proc_name: =_!" :: Replace spaces with underscores :: Create a unique filename for the process set "current_file=!proc_name!_!proc_pid!.txt" :: Write the process header to the new file echo !line! > "!current_file!" ) else if not "!line!"=="" ( :: Write DLL lines to the current process file (if we have an active file) if not "!current_file!"=="" ( echo !line! >> "!current_file!" ) ) else ( :: Empty line means we're done with the current process - reset the file variable set "current_file=" ) ) echo Split complete! Each process's DLL list is saved in a separate TXT file. endlocal
Option 2: Generate and Split in One Step
If you want to skip manually creating output.txt, use this script to run tasklist and split the output immediately:
@echo off setlocal enabledelayedexpansion :: First generate the full tasklist output tasklist /m /fi "imagename eq *.exe" > output.txt :: Now split the output (same logic as Option 1) set "input_file=output.txt" set "current_file=" for /f "delims=" %%a in ('type "%input_file%"') do ( set "line=%%a" if "!line:~0,4!"=="映像名称" ( continue ) echo !line! | findstr /r "[0-9][0-9]*" >nul if !errorlevel! equ 0 ( for /f "tokens=1,2" %%b in ("!line!") do ( set "proc_name=%%b" set "proc_pid=%%c" ) set "proc_name=!proc_name:.exe=!" set "proc_name=!proc_name: =_!" set "current_file=!proc_name!_!proc_pid!.txt" echo !line! > "!current_file!" ) else if not "!line!"=="" ( if not "!current_file!"=="" ( echo !line! >> "!current_file!" ) ) else ( set "current_file=" ) ) echo Done! Check the folder for files like `notepad_1234.txt` or `chrome_5678.txt`. endlocal
How It Works
- Delayed Expansion:
setlocal enabledelayedexpansionlets us update and read variables inside the loop correctly. - Process Line Detection: We use
findstrto spot lines with numeric PIDs (since process entries include a PID, while DLL lines don't). - Filename Cleanup: We remove
.exeand replace spaces with underscores to avoid invalid filenames. - Per-Process Files: Each time we hit a new process entry, we create a new TXT file and write all subsequent DLL lines to it until we hit an empty line (the separator between processes).
Notes
- If a process name has special characters like
&or%, you might need to add extra cleanup logic to handle those. - Make sure you run the script with sufficient permissions—some system processes might not return DLL info if you don't have admin rights.
- All output files will be saved in the same folder as the batch script.
内容的提问来源于stack exchange,提问作者Nadav Kohen
相关产品推荐
相关产品推荐

