Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: reading file in windows batch

18 Aug 2014   #1
aliyesami

win7-64 bit
 
 
reading file in windows batch

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 System SpecsSystem Spec
.
18 Aug 2014   #2
JDobbsy1987

Windows 8.1 Pro x64
 
 

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

reading file in windows batch-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 System SpecsSystem Spec
18 Aug 2014   #3
aliyesami

win7-64 bit
 
 

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 System SpecsSystem Spec
.

18 Aug 2014   #4
JDobbsy1987

Windows 8.1 Pro x64
 
 

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 System SpecsSystem Spec
18 Aug 2014   #5
aliyesami

win7-64 bit
 
 

i want the pids to be assigned to variable so
KILLPIDSUBSTR=3136
KILLPIDSUBSTR=7732

thanks
My System SpecsSystem Spec
18 Aug 2014   #6
JDobbsy1987

Windows 8.1 Pro x64
 
 

Does this do the trick?

Code:
SETLOCAL ENABLEDELAYEDEXPANSION

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

pause
reading file in windows batch-2014-08-18-22_58_44-c__windows_system32_cmd.exe.png

Regards,
Jamie


My System SpecsSystem Spec
Reply

 reading file in windows batch




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Batch File: works under cmd-Windows , NOT under MS-DOS
I made these codes to backup files : set p=C:\Users\ for /f %%a in ('dir C:\Users\ /b') do ( if not "%%a"=="Public", ( set fileDest=C:\Backup-%%a_%date:~7,2%.%date:~4,2%.%date:~10,4%
Virtualization
How to run a batch file at logoff & some batch scripting help please
Hiya, I'd just like to run a simple batch file at logoff to backup some settings. I plan to use Robocopy, which is what i use to more thorough backups. Two questions: 1. How make it run by clicking Start > Shutdown? (i realise i could make a shutdown batch file and run it instead, but i'd...
Backup and Restore
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion
Problem file reading with Windows 7
Hi all, The problem I have, I have just down loaded Virtual Dj to my home computer (Running on Windows 7 home edition) which then I connect an external USB hard drive (Seagate) and every thing runs well, all files detected etc. I have just purchased a new lap top (Dell, which is also running on...
Drivers
Batch File Error Windows 7 x64
Hey Guys, Tried writing a batch file to open a few programs I use together regularly, but which I don't want all the time on startup. I've tried: start “Maxivista” /d “C:\Program Files\MaxiVista Pro Server\MaxiVistaAll.exe" start “Netbeans” /d "R:\Program Files\NetBeans...
General Discussion
windows 7 64 bit BSOD help reading dmp file :(
I just recently upgraded my pc to windows 7 64bit ultimate running on i5-750@2.67mhz 4gb ddr3 g.skill 1600-2000mhz @2000mhz asrock h55m motherboard i been getting a lot of blue screen could someone help me read my minidump file:( i really need help to find out the causes.
BSOD Help and Support


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 03:21.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App