Jump to content

Storing Text Tab Delimited into variables


acarter
 Share

Go to solution Solved by acarter,

Recommended Posts

Hey Gang!

So I've ran into an issue where I don't know how to separate a text tab delimited file by tabs. 

Here is what I'm trying to do - I'm building a loader program that reads each "cell" that Excel outputted to this Text Tab-Delimited file and hold it until its used. There are 4 columns in the file and each "cell" will be used at a different time.

Here is how the file goes:

Column A - Contract #

Column B - Year

Column C - Contract Line Number

Column D - Code

The program will need to run though a process of Send commands and when needed, it will output the contents of each cell (at different times) and run to the next line in the text file and repeat the process.

My code thus far: (it might look amateurish - its a combination of a bunch of example scripts I've tested).

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>

#region ### START Koda GUI section ###
$Form1 = GUICreate("SP", 606, 220)
$Label1 = GUICtrlCreateLabel("DM Loader", 32, 24, 279, 39)
GUICtrlSetFont(-1, 20, 400, 0, "Elephant")
$Label2 = GUICtrlCreateLabel("Select File:", 32, 80, 56, 17)
$Input1 = GUICtrlCreateInput("", 88, 75, 281, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetFont(-1, 8, 800, 4, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Select", 376, 72, 75, 25)
$Button2 = GUICtrlCreateButton("Load", 32, 150, 512, 25)
Local $aInput
$message = "Select File"
GUISetState(@SW_SHOW)

#endregion ### END Koda GUI section ###


While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button1
            $var = FileOpenDialog($message, "C:\", "Text (*.txt)", 1)
            GUICtrlSetData($Input1, $var)
        Case $msg = $Button2
            FileOpen($var, 0)
            _FileReadToArray($var, $aInput)
            For $i = 1 To UBound($aInput) - 1
                MsgBox(0, '', $aInput[$i])
            Next

            For $i = 1 To _FileCountLines($var)
                $line = FileReadLine($var, $i)
                MsgBox(0, " ", "The Line " & $i & "is" & $line)
            Next
            FileClose($var)
    EndSelect
WEnd

Does anyone know a solution to my issue?

Thanks!

Link to comment
Share on other sites

i guess the 2 parts of $Button2 (separated by a blank line) are alternatives that you tried?

suggestion 1: google autoit file read to array 2d

there are many variations that will get your data in a table (2d array) instead of a list (1d array).

suggestion 2:

try using StringSplit() on each line before you use the data, use @TAB as delimiter.

if @TAB as delimiter gives you trouble, use space, or better yet - change your Excel output format (if you can) to CSV, separating by commas is easier.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

i guess the 2 parts of $Button2 (separated by a blank line) are alternatives that you tried?

suggestion 1: google autoit file read to array 2d

there are many variations that will get your data in a table (2d array) instead of a list (1d array).

suggestion 2:

try using StringSplit() on each line before you use the data, use @TAB as delimiter.

if @TAB as delimiter gives you trouble, use space, or better yet - change your Excel output format (if you can) to CSV, separating by commas is easier.

 

Thank you! I was originally going to use just an Excel file to read off of, but it looks more complicated. 

Which would be easier - Text (Tab Delimited), CSV, or an Excel file?

Link to comment
Share on other sites

EDIT: I got it to read and put it into a text file. That was easier than I thought - but my next (and last) question is, how do I get it to hold on to each "cell" and send it when needed? Right now, it sends the first "cell", presses enter", enters the second one & Enter, third one & Enter, and fourth one & Enter, and goes to the next line. I want to have it send other things between each "cell" a line.

 
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>




#region ### START Koda GUI section ###
$Form1 = GUICreate("SP", 606, 220)
$Label1 = GUICtrlCreateLabel("DM Loader", 32, 24, 279, 39)
GUICtrlSetFont(-1, 20, 400, 0, "Elephant")
$Label2 = GUICtrlCreateLabel("Select File:", 32, 80, 56, 17)
$Input1 = GUICtrlCreateInput("", 88, 75, 281, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetFont(-1, 8, 800, 4, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Select", 376, 72, 75, 25)
$Button2 = GUICtrlCreateButton("Load", 32, 150, 512, 25)


$message = "Select File"
GUISetState(@SW_SHOW)




Local $aInput


#endregion ### END Koda GUI section ###










While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $Button1
$var = FileOpenDialog($message, "C:\", "Text (*.txt)", 1)
GUICtrlSetData($Input1, $var)
Case $msg = $Button2
While 1
FileOpen($var, 0)
$lines = StringSplit(FileReadLine($var), @TAB, 1)
_FileReadToArray($var, $lines)


For $i = 1 To _FileCountLines($var)
ConsoleWrite('line:' & $i & ' ')
$columns = StringSplit($lines[$i], @TAB)


For $j = 1 To $columns[0]
ConsoleWrite('col' & $j & ':' & $columns[$j] & ' ')
WinWaitActive("Untitled - Notepad")
Send($columns[$j])
Send("{ENTER}")
Next
ConsoleWrite(@TAB)
FileClose($var)
Next
ExitLoop
WEnd






EndSelect
WEnd
Edited by acarter
Link to comment
Share on other sites

To better explain what I want the above code to do:

Text file:

12345     14    1    92837

23456    14    9    92837

23456    14    10    92837

I want it to take, for example, "12345" and send it to notepad, then send some stuff (a name, or phone number hard-coded), then send "14", and send some more hard-coded stuff, then send "1", and more hard-coded stuff, and send "92837, and finally more hard-coded stuff.....and move to the next line and repeat the above.

Link to comment
Share on other sites

It seems that you can't avoid doing like this

For $i = 1 To _FileCountLines($file)
   $line = FileReadLine($file, $i)
   $columns = StringSplit($line, @TAB)
   WinWaitActive("Untitled - Notepad")
   Send($columns[1])
      ; do some work
   Send($columns[2])
      ; do some other work
   Send($columns[3])
      ; do some different work
   Send($columns[4])
      ; do some final work
Next

But the 'send' series are not a very reliable way

Edited by mikell
Link to comment
Share on other sites

  • Solution

It seems that you can't avoid doing like this

For $i = 1 To _FileCountLines($file)
   $line = FileReadLine($file, $i)
   $columns = StringSplit($line, @TAB)
   WinWaitActive("Untitled - Notepad")
   Send($columns[1])
      ; do some work
   Send($columns[2])
      ; do some other work
   Send($columns[3])
      ; do some different work
   Send($columns[4])
      ; do some final work
Next

But the 'send' series are not a very reliable way

 

Thank you!!! That works. Unfortunately, the SEND series is my only option. But this works perfectly. Thank you very much!

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...