none
data file with spaces, how to store each one in variable VBS RRS feed

  • Question

  • I have a data file such as a txt file and it is like this:

    123456

    789542

    ....

    these can be any numbers and as many lines as needed. I need to take each number line by line storing them in 6 different variables. Such as:

    name = 1

    email = 2

    these could be dynamic arrays by the columns also:

    name = 1,7, ...

    Right now my code is only displaying one line and I am not sure how to put each one in the variables like I said above. Can anyone help me with this... my code is below, thanks in advance! This is my first post so I tried.

    Sub ButtonTool1_EventClick()
    Dim This : Set This = ButtonTool1
    
    Dim arrFileLines()
    Dim strLine 
    Dim arrStrings
    Dim arrPulled
    i = 0 
    
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objFile = objFSO.OpenTextFile("C:\Users\Administrator\My Documents\Nominations.dat", 1) 
     
    Do Until i = 2 '2nd entry, could be 181 for all lines
    Redim Preserve arrFileLines(i) 
    arrFileLines(i) = objFile.ReadLine 
    i = i + 1 
    Loop 
    objFile.Close 
    
    
    
    For Each strLine In arrFileLines
    arrStrings = Split(strLine,"  ")
    EditBox1.Text = (arrStrings(0))
    Next
    
    
    End Sub 

    Right now this code works but only displays the first line of the file which is okay. I just need these to be stored in 6 separate variables. Hopefully I am making sense.




    • Edited by EvilEngine9 Wednesday, June 25, 2014 4:36 PM
    Wednesday, June 25, 2014 4:32 PM

Answers

All replies

  • Start with what you need to do, not how you think it needs to be done.

    For example: "I have a text file with numbers on each line. I need to read in this list and number them and output the new list to a separate file."

    Probably PowerShell will provide a much shorter answer to your question (whatever it is).


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 25, 2014 4:51 PM
    Moderator
  • I need each column of numbers (or strings) to be placed in separate arrays. I do not need an output file. 

    Wednesday, June 25, 2014 5:02 PM
  • My sample question was just that.

    Your follow-up post does not explain why you need to read the data file or why you think you need separate arrays.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 25, 2014 5:06 PM
    Moderator
  • I need each column of numbers (or strings) to be placed in separate arrays. I do not need an output file. 

    This is an Office VBA project.  VBScript does not have text boxes.

    Yor code will only ever place the last line into the text field.

    As Bill noted: what is it you are trying to do and not how?


    ¯\_(ツ)_/¯

    Wednesday, June 25, 2014 5:08 PM
  • Correct. We only know what is posted. We do not have access to your network, nor can we see your screen.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 25, 2014 5:13 PM
    Moderator
  • How can I take each of those numbers and store them in variables? Like k = 1, e = 2, ... 

    That is all I am asking. Sorry for the confusion.

    Wednesday, June 25, 2014 6:03 PM
  • VBScript/VBA uses the = operator to assign a value to a variable.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 25, 2014 6:13 PM
    Moderator
  • But how can I make it save each one separately separated by the white space.
    Wednesday, June 25, 2014 6:47 PM
  • Sorry, it is not possible for me to understand what you are asking based on only what you have posted so far.

    Save what where? What whitespace?

    Remember: we can't read your mind, and we can't see your screen.

    Poor/incomplete questions only end up wasting everyone's time.

    Here are some tips on how you should ask a question:


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 25, 2014 6:51 PM
    Moderator
  • ok, say I have 100 rows. Each row has 6 numbers. Each number exists within a column.

    1 2 3 4 5 6 then one that is 6 2 7 9 6 8

    so 1 and 6 is the first column which needs to be firstNum.

    2 and 2 is second column which needs to be secondNum

    3 and 7 is third column which needs to be thirdNum

    In the file that this matrix exists in, the numbers are separated by " ". So I need to store firstNum and so on... They could be arrays, but how do I just store the columns to those variables?

    Just ignore the editbox and everything. 
    • Edited by EvilEngine9 Wednesday, June 25, 2014 6:57 PM
    Wednesday, June 25, 2014 6:55 PM
  • In your first post you stated '123456', now you're saying '1 2 3 4 5 6'.

    Are there actually spaces between each number? If so, split on " ":

    http://ss64.com/vb/split.html


    EDIT: Now that I look at your code, you're already using split. If you actually want six named variables, just assign them directly.

    varOne = arrStrings(0)

    varTwo = arrStrings(1)

    ...


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, June 25, 2014 6:59 PM
  • If the user could explain the purpose for this exercise it might make sense.  As stated it makes no sense.


    ¯\_(ツ)_/¯

    Wednesday, June 25, 2014 9:59 PM
  • It's basically an unanswerable drive-by question due to lack of context.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 25, 2014 10:08 PM
    Moderator