1

Hey there scripting geniuses, please note I am still learning VBScript and I am fully ready for the lashings. So, I will research anything you ask me to as long as I have direction to go for.

I searched through the suggested questions and I found one that might be what I needed but it was "closed" with no link to go look for the other like it.

Here is what I am trying to accomplish. I need to take a text file and load the data into an array (if that is correct). This data contains Pc name, Pc location. It will carry about 2000 records (or lines) and I need to load each one into a variable. The PC name will have no spaces but the Pc Location may. Like example:

Laptop, my bathroom 
Desktop, kitchen 

I want to load this information into a variable I can use WshShell.SendKeys and pass key commands. I have been able to make it "kinda" work with two text files and send that data to an excel sheet (because it can take tab and enter). But rather than match one to one it matches one to 10. So If PC.txt had 10 pc names and Location.txt had 10 locations on it (which match each file in line order), my script would give me a 100 results, not 10. So, I know I am doing this all wrong and need some guidance. Here is my failed attempt and I am ready for the laughter hehhee..

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\pc.txt", ForReading1)
Set objFile = objFSO.OpenTextFile("C:\Location.txt", ForReading)

'test is my excel file title test, this will allow tabs and enters
WshShell.AppActivate "test"

Const ForReading1 = 1
Dim arrFileLines1()
i = 0
Do Until objFile1.AtEndOfStream
  Redim Preserve arrFileLines1(i)
  arrFileLines1(i) = objFile1.ReadLine
  i = i + 1
Loop
objFile1.Close

Const ForReading = 1
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
  Redim Preserve arrFileLines(i)
  arrFileLines(i) = objFile.ReadLine
  i = i + 1
Loop
objFile.Close

For Each strLine1 in arrFileLines1
  For Each strLine in arrFileLines
    WshShell.SendKeys strLine1
    WshShell.SendKeys "{TAB}"
    WshShell.SendKeys strLine
    WshShell.SendKeys "{ENTER}"
  Next
Next

I have been researching about splitting this data based off the "," but I need to get that into a Variable that I can use and repeat the key strokes, Like:

Laptop {TAB} My kitchen {ENTER} Desktop {TAB} Bathroom {ENTER} ...........ETC till the end.

3
  • If PC name is your unique key here to sort. You may first load pc names into a 1D array. You may ReDim means re-dimensioned it to a 2D array. Then use the pc name as a key to match and retrieve matching location data and store them in the array. Now in terms of splitting it by delimeter: If you have no idea of the delimeters file may contain or if the delimeters are random, then you may store all possible delimeters in another array. Take a look at this VBA post code for delimeters part.. Commented Dec 17, 2012 at 17:16
  • Thank you so much for your reply. Because this data is going to have to go to a program form I am getting suggestions to go to Autoit. Just one more language to try and learn. I will read up on you suggestion to learn about "delimeters" on that post. Thank you! Commented Dec 17, 2012 at 17:24
  • I thought about introducing you to dictionary object, but then I realized it might not be feasible in your case. But Ansgar seems to have a solution based on that. You may try your own and any other answers given here. Happy to help if you have further questions on the same. :) Commented Dec 18, 2012 at 8:13

1 Answer 1

3

I would use a single loop and a dictionary to load the two input files into a data structure (assuming that the PC names are unique).

Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")

Set names     = fso.OpenTextFile("C:\pc.txt", ForReading)
Set locations = fso.OpenTextFile("C:\Location.txt", ForReading)

Set computers = CreateObject("Scripting.Dictionary")

Do Until names.AtEndOfStream Or locations.AtEndOfStream
  computers.Add names.ReadLine, locations.ReadLine
Loop

names.Close
locations.Close

This will create a dictionary with name/location pairs that can be accessed like this:

For Each key In computers
  WScript.Echo key & " is in " & computers(key)
Next

I would recommend to steer clear of SendKeys when it comes to passing data to other applications, though, because usage of that function tends to be flaky, to say the least. Depending on the target application there may be better ways, for instance if the application exposes a COM object that can be controlled from VBScript.

If the target application can't be RC'd like that you probably are better off using AutoIt, as other people seem to have already suggested.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.