Page 1 of 1

File reading and extracting parts of strings

Posted: 01 Jan 2010 14:02
by smokeys_13
Hi, my current code stands as this:

Code: Select all

setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (running_tasks.txt) do (
set /a N+=1
set v!N!=%%a
)


Which works fine, but its putting the whole line into each variable. Is there a way to only set the variables to the beginning of the line ending after ".exe"?

Any help would be greatly appreciated :)

Sorted it!

Posted: 01 Jan 2010 14:59
by smokeys_13
Never mind, I've sorted it now the new code is below for anyone that had a similar question.

Code: Select all


setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (running_tasks.txt) do (
  set /a N+=1
  set v!N!=%%a

 for /f "tokens=1 delims= " %%a in ("%%a") do (
  set /a O+=1
  set w!O!=%%a
  )

)



The "name.exe" was the first part of the line and will never have spaces so the nested for loop just takes that - if only I'd thought of that to begin with!

Posted: 01 Jan 2010 19:57
by !k
running_tasks.txt is —

Code: Select all

smss.exe                     624                         0       384 КБ
csrss.exe                    724                         0     9 036 КБ
winlogon.exe                 780                         0     3 980 КБ
services.exe                 840                         0     2 920 КБ
lsass.exe                    852                         0     1 404 КБ
svchost.exe                 1164                         0     3 536 КБ
svchost.exe                 1200                         0     4 296 КБ
svchost.exe                 1332                         0    16 896 КБ
?

then

Code: Select all

@echo off
for /f "tokens=1" %%a in (running_tasks.txt) do (
set /a N+=1
set "v!N!=%%a"
)
set v

Thanks

Posted: 02 Jan 2010 08:09
by smokeys_13
Oh yeah, of course.... silly me. I've got pointless lines of code in there haven't I -- thanks for pointing that out!