reading file in windows batch

aliyesami

New member
Local time
12:23 AM
Messages
19
i have a file b.txt with entries like :

explorer.exe pid: 3136 type: File A24: D:\ftproot\logs\tagswap
notepad.exe pid: 7732 type: File 18: D:\ftproot\logs\tagswap


I want to read each record and extract pid and kill it.
The following is only giving me only one record .

for /f "tokens=4 delims= " %%G in (c:\temp\b.txt) do (
SET KILLPIDSTR=%%G
SET KILLPIDSUBSTR=%KILLPIDSTR:~24,5%
echo %KILLPIDSUBSTR%
)
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
custom build
OS
win7-64 bit
CPU
Intel core i5-750
Motherboard
ASUS P7H55-M PROD
Memory
16Gb
Graphics Card(s)
ATI RADEAON HD4670
Hard Drives
C:\ partition 1 : 100gb partition 2: 600 gb
Hi,

Does this help

Code:
SETLOCAL ENABLEDELAYEDEXPANSION

for /f %%G in (c:\temp\b.txt) do (
SET KILLPIDSTR=%%G
SET KILLPIDSUBSTR=%KILLPIDSTR:~24,5%
echo %KILLPIDSUBSTR%
)

Pause

This is what i get when running the above code, as you can see it returns the name of the programs i expect (i did it against calc.exe and notepad.exe):

2014-08-18 21_52_23-Task Manager.png

Part of the problem you have is you are setting a variable and then setting another variable based on the 1st one within the same command, when you do this all variables are set at the same time so technically when you try to do the 2nd SET command its blank as the first one isn't set if that makes sense?

SETLOCAL ENABLEDELAYEDEXPANSION fixes that issue

Regards,
Jamie
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
i put your command in but i still dont see the pids assigned to the variable:


C:\TEMP>for /F %G in (c:\temp\b.txt) do (
SET KILLPIDSTR=%G
SET KILLPIDSUBSTR=~24,5
echo
)
C:\TEMP>(
SET KILLPIDSTR=explorer.exe
SET KILLPIDSUBSTR=~24,5
echo
)
ECHO is on.
C:\TEMP>(
SET KILLPIDSTR=notepad.exe
SET KILLPIDSUBSTR=~24,5
echo
)
ECHO is on.
C:\TEMP>
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
custom build
OS
win7-64 bit
CPU
Intel core i5-750
Motherboard
ASUS P7H55-M PROD
Memory
16Gb
Graphics Card(s)
ATI RADEAON HD4670
Hard Drives
C:\ partition 1 : 100gb partition 2: 600 gb
Sorry, i see what you mean... i can see you are stripping the output in the 2nd SET command, what do you expect to be echo'ed there?

Regards,
Jamie
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
i want the pids to be assigned to variable so
KILLPIDSUBSTR=3136
KILLPIDSUBSTR=7732

thanks
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
custom build
OS
win7-64 bit
CPU
Intel core i5-750
Motherboard
ASUS P7H55-M PROD
Memory
16Gb
Graphics Card(s)
ATI RADEAON HD4670
Hard Drives
C:\ partition 1 : 100gb partition 2: 600 gb
Does this do the trick?

Code:
SETLOCAL ENABLEDELAYEDEXPANSION

for /f "tokens=3 delims= " %%G in (b.txt) do (
	SET KILLPIDSTR=%%G
	echo !KILLPIDSTR!
)

pause

2014-08-18 22_58_44-C__Windows_system32_cmd.exe.png

Regards,
Jamie
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
Back
Top