Question : VBS script in Macro to pull multiple lines of data needed from file

I've got an .ini file that is an inventory of a PC.  A Macro has been created to pull info out of it and  populate an XLS file

It works fine when there is only one line in the file for certain info.  However, we also want to see all the printers a person has.

In the file, it looks like
----------------------
MappedPrinter.......: PMT : = Tesia PMT
MappedPrinter.......: TesiaPort: = Tesia
MappedPrinter.......: NVM5: = Send to Dentrix Document Center
MappedPrinter.......: XPSPort: = Microsoft XPS Document Writer
MappedPrinter.......: Microsoft Document Imaging Writer Port: = Microsoft Office Document Image Writer
MappedPrinter.......: HPFAX: = HP LaserJet 3050_3055_3390_3392 Fax
MappedPrinter.......: DOT4_001 = HP LaserJet 3050 Series PCL 6 (Copy 1)
MappedPrinter.......: IP_170.11.115.21 = Canon iR5000-6000-L1 PCL5e CP Barn
--------------

Depending on there person, they may have more or less printers.


When I set up code similar to the other fields
------------------
                If Left(xR, 13) = "MappedPrinter" Then

                    Cells(zZ, 23) = Right(xR, Len(xR) - 22)

                End If
--------------------
This only displays the last of the Mapped Printer info of "IP_170.11.115.21 = Canon iR5000-6000-L1 PCL5e CP Barn"

So it needs to loop on itself each time it see's MappedPrinter, creating a string that will grow (maybe have some sort of separator text added in between the info.

Up at the top of the VB script, I've created a variable of "printerInfo as String"

Below is the VBS code of the Macro
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
Option Explicit

 

Public xR, zZ As Long, daRows, memoryTotal As Integer, memSubString As String, printerInfo As String, startPos As Integer, i As Integer, driveSize As String, freeSpace As String, percentFree As Integer

 

Sub getFields()


    daRows = Application.CountA(ActiveSheet.Range("A:A"))   'Determine # of rows

    For zZ = 2 To daRows        'Set up loop

        Open Cells(zZ, 1) For Input Access Read As #6   'Open file to be read

        Do While Not EOF(6)

            Line Input #6, xR   'Read a line
                 
                 If Left(xR, 13) = "InventoryDate" Then

                    Cells(zZ, 2) = Right(xR, Len(xR) - 22)

                End If
            
                If Left(xR, 12) = "Computername" Then
                    
                    Cells(zZ, 3) = Right(xR, Len(xR) - 22)

                End If

                If Left(xR, 14) = "CsSerialNumber" Then

                    Cells(zZ, 4) = Right(xR, Len(xR) - 22)

                End If
                
                If Left(xR, 15) = "CsComputerModel" Then

                    Cells(zZ, 5) = Right(xR, Len(xR) - 22)

                End If
                
                If Left(xR, 8) = "UserName" Then

                    Cells(zZ, 6) = Right(xR, Len(xR) - 22)

                End If


                If Left(xR, 11) = "PrimaryUser" Then
                    
                    Cells(zZ, 7) = Right(xR, Len(xR) - 22)

                End If

                If Left(xR, 8) = "Location" Then

                    Cells(zZ, 8) = Right(xR, Len(xR) - 22)

                End If
                
                If Left(xR, 7) = "Company" Then

                    Cells(zZ, 9) = Right(xR, Len(xR) - 22)

                End If
                
                If Left(xR, 10) = "Department" Then

                    Cells(zZ, 10) = Right(xR, Len(xR) - 22)

                End If

                If Left(xR, 7) = "CpuName" Then

                    Cells(zZ, 11) = Right(xR, Len(xR) - 22)

                End If

                If Left(xR, 13) = "CpuClockSpeed" Then

                    Cells(zZ, 12) = Right(xR, Len(xR) - 22)
                        

                End If

'Placeholder for Drive0 and Drive1


If Left(xR, 24) = "HardDrive...........: C:" Then
                
                    'Initialize variables
                    driveSize = ""

                    'Finds the starting position of the hard drive size
                    startPos = InStr(1, xR, "size=")
                    startPos = startPos + 5
                    
                    Do While Asc(Mid(xR, startPos, 1)) >= 48 And _
                        Asc(Mid(xR, startPos, 1)) <= 57 Or _
                        Asc(Mid(xR, startPos, 1)) = 45
                        
                        driveSize = driveSize & Mid(xR, startPos, 1)
                        startPos = startPos + 1
                    Loop
                                       
                    If IsNumeric(driveSize) Then
                        driveSize = (driveSize)
                    End If
                    
                    
                    Cells(zZ, 16) = (driveSize) / 1024 / 1024 / 1024

                End If
                                      
           If Left(xR, 24) = "HardDrive...........: C:" Then
                
                    'Initialize variables
                    freeSpace = ""
                
                
                    'Finds the starting position of the hard drive free space
                    startPos = InStr(1, xR, "free=")
                    startPos = startPos + 5
                    
                    Do While Asc(Mid(xR, startPos, 1)) >= 48 And _
                        Asc(Mid(xR, startPos, 1)) <= 57 Or _
                        Asc(Mid(xR, startPos, 1)) = 45
                        
                        freeSpace = freeSpace & Mid(xR, startPos, 1)
                        startPos = startPos + 1
                        
                        If startPos > Len(xR) Then Exit Do
                    Loop
                                       
                    If IsNumeric(freeSpace) Then
                        freeSpace = (freeSpace)
                       Cells(zZ, 17) = (freeSpace) / 1024 / 1024 / 1024
                    End If
                    
                End If

        If Left(xR, 6) = "OsName" Then

                    Cells(zZ, 18) = Right(xR, Len(xR) - 22)

                End If


                If Left(xR, 43) = "InstalledApp........: Microsoft Office Stan" Then

                    Cells(zZ, 19) = Right(xR, Len(xR) - 22)

                End If

                If Left(xR, 43) = "InstalledApp........: Microsoft Office Prof" Then

                    Cells(zZ, 20) = Right(xR, Len(xR) - 22)

                End If


                If Left(xR, 9) = "IPAddress" Then

                    Cells(zZ, 21) = Right(xR, Len(xR) - 22)

                End If


                If Left(xR, 9) = "SAVClient" Then

                    Cells(zZ, 22) = Right(xR, Len(xR) - 22)

                End If
                
                If Left(xR, 13) = "MappedPrinter" Then

                    Cells(zZ, 23) = Right(xR, Len(xR) - 22)

                End If
                

                If Left(xR, 21) = "MemoryModule........:" Then
                    'Initialize variables
                    memSubString = ""
                    
                    'Finds the starting position of the memory amount
                    startPos = InStr(1, xR, "size=")
                    startPos = startPos + 5
                    
                    Do While Asc(Mid(xR, startPos, 1)) >= 48 And _
                        Asc(Mid(xR, startPos, 1)) <= 57 Or _
                        Asc(Mid(xR, startPos, 1)) = 45
                        
                        memSubString = memSubString & Mid(xR, startPos, 1)
                        startPos = startPos + 1
                    Loop
                    
                    If IsNumeric(memSubString) Then
                        memoryTotal = memoryTotal + CInt(memSubString)
                    End If
                    Cells(zZ, 13) = Abs(memoryTotal)

                End If


        Loop

        Close #6    'Close the file
        memoryTotal = 0
    
    Next    'Move to next line
    

    'Add column headers
    
    Cells(1, 2) = "ReportDate"
    Cells(1, 3) = "ComputerName"
    Cells(1, 4) = "SerialNumber"
    Cells(1, 5) = "Model"
    Cells(1, 6) = "UserName"
    Cells(1, 7) = "PrimaryUser"
    Cells(1, 8) = "Location"
    Cells(1, 9) = "Company"
    Cells(1, 10) = "Department"
    Cells(1, 11) = "CPUType"
    Cells(1, 12) = "ClockSpeed"
    Cells(1, 13) = "Memory"
    Cells(1, 14) = "Drive0"
    Cells(1, 15) = "Drive1"
    Cells(1, 16) = "CSize"
    Cells(1, 17) = "CFree"
    Cells(1, 18) = "OperSystem"
    Cells(1, 19) = "OfficeStandard"
    Cells(1, 20) = "OfficePro"
    Cells(1, 21) = "IPAddress"
    Cells(1, 22) = "AntiVirus"
    Cells(1, 23) = "MappedPrinter"
    
    MsgBox "Finished Processing!"

End Sub

Answer : VBS script in Macro to pull multiple lines of data needed from file

Replace your If statement with the below snippet... this will use a semi-colon delimiter.

You could repeat this with each of your sections.
1:
2:
3:
4:
5:
6:
7:
If Left(xR, 13) = "MappedPrinter" Then
                If Cells(zZ, 23) = "" Then
                    Cells(zZ, 23) = Right(xR, Len(xR) - 22)
                Else
                    Cells(zZ, 23) = Cells(zZ, 23) & "; " & Right(xR, Len(xR) - 22)
                End If
            End If
Random Solutions  
 
programming4us programming4us