File reading and extracting parts of strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
smokeys_13
Posts: 3
Joined: 01 Jan 2010 13:57

File reading and extracting parts of strings

#1 Post by smokeys_13 » 01 Jan 2010 14:02

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

smokeys_13
Posts: 3
Joined: 01 Jan 2010 13:57

Sorted it!

#2 Post by smokeys_13 » 01 Jan 2010 14:59

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!

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#3 Post by !k » 01 Jan 2010 19:57

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

smokeys_13
Posts: 3
Joined: 01 Jan 2010 13:57

Thanks

#4 Post by smokeys_13 » 02 Jan 2010 08:09

Oh yeah, of course.... silly me. I've got pointless lines of code in there haven't I -- thanks for pointing that out!

Post Reply