Jump to content

How to separate an input string


Recommended Posts

I am trying to read in a file and display/change in in a gui.

When I read in the file I'm reading it a line at a time.

I need to separate out the information on the line and put the correct information into the correct columns.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Global $hListView
Global $Input1 = GUICtrlCreateInput("", 72, 16, 209, 21)

_Main()
FileClose($file)

Func _Main()

Local $GUI, $hImage
$GUI = GUICreate("List of folders", 400, 300)

$hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))

DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", $hListView, "wstr", "Explorer", "ptr", 0) ; Set "Vista" Style

GUISetState()

; Add columns
_GUICtrlListView_InsertColumn($hListView, 0, "FileID", 100)
_GUICtrlListView_InsertColumn($hListView, 1, "FileName", 100)
_GUICtrlListView_InsertColumn($hListView, 2, "FolderName", 100)


; Loop until user exits
Do
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
;    Case $Button1
Local $file = FileOpen("C:\Scratch\Scratch11\test.txt")
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
$row = 0
While 1
GUICtrlSetData($Input1, FileReadLine($file)) ; reads only the first Line of the file
If @error = -1 Then ExitLoop

; Add lines
$column = 0
_GUICtrlListView_AddItem($hListView, $Input1, $column) ;Row 1: Col 1
$column = $column + 1
_GUICtrlListView_AddSubItem($hListView, $row, "Row 1: Col 2", $column) ;Row 1: Col 2
$column = $column + 1
_GUICtrlListView_AddSubItem($hListView, 0, "Row 1: Col 3", 2) ;Row 1: Col 3
$row = $row + 1

WEnd
EndSwitch
WEnd
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>_Main

The file I'm working with test.txt has this in it:

2,1,"72496884:My mailbox","","My mailbox"
0,0,"1BD73CF2:5FDA:AS400LST","72496884:My mailbox","AS/400 Info"
0,0,"0D503CFA:5FDB:BPCSINFO","72496884:My mailbox","zBPCS info"
0,0,"1F0B3D0E:5FDD:FILEMAIL","72496884:My mailbox","zFiles by Mail"
0,0,"1CF43D15:5FDE:IN-MAIL","72496884:My mailbox","In-comming Saved Mail "

I only want the lines with 0,0 at the begging of the line.

For column one I need the four characters after the first Colin. So the first example would be 5FDA

For column two I need all characters after the second Colin up until the second ". So the first example would be AS400LST

For column three I need all characters in between the last set of quotes. So the first example would be AS/400 Info

I will be putting that just before ; Add lines

I don't know how to manipulate strings to separate these out.

Thank you,

Docfxit

Edited by Docfxit
Link to comment
Share on other sites

  • Moderators

Docfxit,

Try this: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

_Main()

Func _Main()

    Local $aLines, $aSplit_1, $aSplit_2, $iIndex, $sString

    Local $GUI = GUICreate("List of folders", 400, 300)

    Local $hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 396, 250)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    ; Add columns
    _GUICtrlListView_InsertColumn($hListView, 0, "FileID", 100)
    _GUICtrlListView_InsertColumn($hListView, 1, "FileName", 100)
    _GUICtrlListView_InsertColumn($hListView, 2, "FolderName", 100)

    Local $Button1 = GUICtrlCreateButton("Read", 10, 260, 80, 30)

    GUISetState()

    ; Loop until user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                If Not FileExists("test.txt") Then
                    MsgBox(0, "Error", "File does not exist")
                    Exit
                EndIf
                ; Read the file into an array
                _FileReadToArray("test.txt", $aLines)
                ; Loop through the lines
                For $i = 1 To $aLines[0]
                    ; Check it begins with 0,0
                    If StringLeft($aLines[$i], 3) = "0,0" Then
                        ; Now split the line
                        $aSplit_1 = StringSplit($aLines[$i], ",")
                        ; For the first line - split on :
                        $aSplit_2 = StringSplit($aSplit_1[3], ":")
                        ; Add the central section to the ListView
                        $iIndex = _GUICtrlListView_AddItem($hListView, $aSplit_2[2])
                        ; Now the end section
                        $sString = StringReplace($aSplit_2[3], '"', '')
                        _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 1)
                        ; Finally the end string
                        $sString = StringReplace($aSplit_1[5], '"', '')
                        _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 2)
                    EndIf
                Next
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>_Main

If you post scripts for debugging, do at least make sure they can at least be run and have all the controls necessary. Expecting people to add code just to get the script to run is not really a good idea of you want help. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I'm trying to figure out why my MsgBox's aren't displaying the strings.

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

_Main()

Func _Main()

    Local $aLines, $aSplit_1, $aSplit_2, $iIndex, $sString

    Local $GUI = GUICreate("List of folders", 400, 300)

    Local $hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 396, 250)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    ; Add columns
    _GUICtrlListView_InsertColumn($hListView, 0, "FileID", 100)
    _GUICtrlListView_InsertColumn($hListView, 1, "FileName", 100)
    _GUICtrlListView_InsertColumn($hListView, 2, "FolderName", 100)

    Local $Button1 = GUICtrlCreateButton("Read", 10, 260, 80, 30)

    GUISetState()

    ; Loop until user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                If Not FileExists("C:\Scratch\Scratch11\HIERARCH.PM") Then
                    MsgBox(0, "Error", "File does not exist")
                    Exit
                EndIf
                ; Read the file into an array
                _FileReadToArray("C:\Scratch\Scratch11\HIERARCH.PM", $aLines)
                ;                _ArrayDisplay($aLines)
                ; Loop through the lines
                For $i = 1 To $aLines[0]
                    ; Check it begins with 0,0

                    If StringLeft($aLines[$i], 3) = "0,0" Then
                        ; Now split the line
                        $aSplit_1 = StringSplit($aLines[$i], ",")
                        MsgBox(4096, "", "aSplit_1 -" & $aSplit_1 & "- " & "$i -" & $i & "-") ; this MsgBox isn't displaying anything for $aSplit_1
                        ; For the first line - split on :
                        $aSplit_2 = StringSplit($aSplit_1[3], ":")
                        MsgBox(4096, "", "aSplit_2 -" & $aSplit_2 & "- " & "$i -" & $i & "-") ; this MsgBox isn't displaying anything for $aSplit_2
                        ; Add the central section to the ListView
                        $iIndex = _GUICtrlListView_AddItem($hListView, $aSplit_2[2])
                        ; Now the end section
                        $sString = StringReplace($aSplit_2[3], '"', '')
                        MsgBox(4096, "", "$sString -" & $sString & "- " & "$i -" & $i & "-")
                        _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 1)
                        ; Finally the end string
                        $sString = StringReplace($aSplit_1[5], '"', '')
                        MsgBox(4096, "", "$sString -" & $sString & "- " & "$i -" & $i & "-")
                        _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 2)
                    EndIf
                Next
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>_Main

I'm getting an error when it gets to the last line:

2,1,"72496884:My mailbox","","My mailbox"
0,0,"1BD73CF2:5FDA:AS400LST","72496884:My mailbox","AS/400 Info"
0,0,"0D503CFA:5FDB:BPCSINFO","72496884:My mailbox","zBPCS info"
0,0,"1F0B3D0E:5FDD:FILEMAIL","72496884:My mailbox","zFiles by Mail"
0,0,"1CF43D15:5FDE:IN-MAIL","72496884:My mailbox","In-comming Saved Mail "
0,0,"","72496884:My mailbox",Name_Unavailable

I actually have a lot more lines in the file. I wasn't thinking the last line would cause an error. That line can be skipped. How can I skip it?

Thank you very much,

Docfxit

Link to comment
Share on other sites

That's because StringSplit is giving an array as a result, and your var $aSplit_1 is an array. So in order to use this, you will have to type $aSplit_1[1], or $aSplit_1[2], $aSplit_1[3], etc... all the way up to $aSplit_1[$aSplit_1[0]]. In your case, for function: $aSplit_1 = StringSplit($aLines[$i], ",") you will get:

$aSplit_1[0] = 5

$aSplit_1[1] = 0

$aSplit_1[2] = 0

$aSplit_1[3] = "1BD73CF2:5FDA:AS400LST"

$aSplit_1[4] = "72496884:My mailbox"

$aSplit_1[5] = "AS/400 Info"

edit: typo error for the var $aSplit_1

Edited by dragan
Link to comment
Share on other sites

  • Moderators

Docfxit,

Did you solve the problem with the last line in that example? :huh:

If not, then you just need to add some errorchecking to the script to check that there is something to split when you ask AutoIt to separate the different parts of the line. Otherwise when you try to split the section to extract the various elements and place them into the ListView there is nothing there to work with! :o

Let me know if you need some more pointers and I will show you what I mean. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Docfxit,

The problem arises becase when you try to split the "" element of this line:

0,0,"","72496884:My mailbox",Name_Unavailable

there is nothing in it to split. So we need to check that we did actually split something and that the various elements exist before we try to use them. We do it by checking the resulting array as you can see in the <<<<<<<<< lines below:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

_Main()

Func _Main()

    Local $aLines, $aSplit_1, $aSplit_2, $iIndex, $sString

    Local $GUI = GUICreate("List of folders", 400, 300)

    Local $hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 396, 250)
    _GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    ; Add columns
    _GUICtrlListView_InsertColumn($hListView, 0, "FileID", 100)
    _GUICtrlListView_InsertColumn($hListView, 1, "FileName", 100)
    _GUICtrlListView_InsertColumn($hListView, 2, "FolderName", 100)

    Local $Button1 = GUICtrlCreateButton("Read", 10, 260, 80, 30)

    GUISetState()

    ; Loop until user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                If Not FileExists("test.txt") Then
                    MsgBox(0, "Error", "File does not exist")
                    Exit
                EndIf
                ; Read the file into an array
                _FileReadToArray("test.txt", $aLines)
                ; Loop through the lines
                For $i = 1 To $aLines[0]
                    ; Check it begins with 0,0
                    If StringLeft($aLines[$i], 3) = "0,0" Then
                        ; Now split the line
                        $aSplit_1 = StringSplit($aLines[$i], ",")
                        ; For the first line - split on :
                        $aSplit_2 = StringSplit($aSplit_1[3], ":")
                        ; Now check there was a split <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                        If $aSplit_2[0] >= 3 Then
                            ; Add the central section to the ListView
                            $iIndex = _GUICtrlListView_AddItem($hListView, $aSplit_2[2])
                            ; Now the end section
                            $sString = StringReplace($aSplit_2[3], '"', '')
                            _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 1)
                            ; Finally the end string - if there is something to add <<<<<<<<<<<<<<<<<<<<<<<<
                            If $aSplit_1[0] >= 5 Then
                                $sString = StringReplace($aSplit_1[5], '"', '')
                                _GUICtrlListView_AddSubItem($hListView, $iIndex, $sString, 2)
                            EndIf
                        EndIf
                    EndIf
                Next
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>_Main

Al clear now? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

You just have to make sure the syntax remains the same for all lines, in the last line if you have:

0,0,"::","72496884:My mailbox","Name_Unavailable"

It wont crash, problem is the script doesn't find the : :

EDIT: arrived late. lol

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...