Help with StringSplit ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Help with StringSplit ?

16 Dec 2018, 05:30

im trying having only the "Image Name" from the outcome table,
(without "PID" , "Session Name" etc etc)

what im doing wrong ?
thanks in advance

Code: Select all

Run, %ComSpec% /c tasklist > c:\tasklist.txt

loop, read, c:\tasklist.txt
{
    StringSplit, Name, A_LoopReadLine, .exe
    MsgBox % Name
}

return
+++ if you have a better methood please share it
gregster
Posts: 9003
Joined: 30 Sep 2013, 06:48

Re: Help with StringSplit ?

16 Dec 2018, 07:10

On my system, I am not allowed to write into the root directory c: - so, this might be a problem. Change the path to some path that works for you...

Then, give the text file a little time to be created, before you parse it -> sleep (or, use FileExist() to check)

Finally, your understanding of Stringsplit (which is marked 'deprecated', but anyway) is not correct. It creates a pseudo-array (hence, Name1 holds what you are looking for); also note
Otherwise, Delimiters contains one or more characters (case sensitive), each of which is used to determine where the boundaries between substrings occur in InputVar.
(https://autohotkey.com/docs/commands/St ... tm#command)
That's why using .exe won't do what you seem to expect.

The StrSplit() function works better here, because you can also use strings longer than one character.

Try this

Code: Select all

Run, %ComSpec% /c tasklist > C:\Users\g\Desktop\tasklist.txt	; change path
sleep 1000		; If FileExist() would be a nifty alternative
loop, read, C:\Users\g\Desktop\tasklist.txt			; change path
{
    Arr := StrSplit(A_LoopReadLine, A_space A_space)		; two spaces in a row because some names contain single spaces
    If (Arr[1] != "")
		MsgBox % Arr[1]
}
return
(I leave it to you to eliminate the headings of the text file from the results)
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Help with StringSplit ?

16 Dec 2018, 07:23

if you have a better methood please share it
Try this other method also, I don't claim it to be better, just a different approach:

Code: Select all

#NoEnv
#SingleInstance Force

    OutputFile := "R:\tasklist.txt"
    RunWait, %ComSpec% /c tasklist > %OutputFile%
    FileRead, TaskList, %OutputFile%

    /*  💬
        TaskList appears to be somewhat tabular.
        Desired result is contained in first column.
        Length of the columns seems to be related to length of "======".
     */

    ; naive approach: Look at only the first column
    Length := 25                                                                ; hardcoded 🤢, sufficient for this demo, ToDo: improve for real script
    Result := ""
    Loop, Parse, TaskList, `n, `r
        Result .= SubStr(A_LoopField, 1, Length) "`n"

    MsgBox % Result

ExitApp
I use RunWait, and later a parsing Loop.
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with StringSplit ?

16 Dec 2018, 09:08

Thank you both gregster & wolf_II ! :)
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Help with StringSplit ?

16 Dec 2018, 09:50

Code: Select all

#NoEnv
#SingleInstance Force

    OutputFile := "R:\tasklist.txt"
    RunWait, %ComSpec% /c tasklist > %OutputFile%
    FileRead, TaskList, %OutputFile%

    ; first column
    MsgBox, % Column(TaskList, 1)

ExitApp



;-------------------------------------------------------------------------------
Column(Haystack, n) { ; return the n-th column from tasklist.exe output
;-------------------------------------------------------------------------------
    Sum := 1, Start := [1], Lengths := [], Result := ""
    Loop, Parse, Haystack, `n, `r
        IfLess, A_Index, 3, continue
        else if A_Index = 3
            for each, Column in StrSplit(A_LoopField, A_Space)
                Lengths.Push(L:=StrLen(Column)), Start.Push(Sum+=1+L)
        else Result .= SubStr(A_LoopField, Start[n], Lengths[n]) "`n"
    return Result
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jameswrightesq and 300 guests