Solved

URGENT: Need vbscript to filter text file

Posted on 2004-09-15
9
1,716 Views
Last Modified: 2010-05-18
Need a vbscript to filter a text file for hard drive serial numbers.  I only want the serial numbers (one on each line) in a new text file.  

Here is a sample of original text file to filter:

Array Disk info:
Name: Array Disk 0:0
Vendor: MAXTOR  
Status: ONLINE
Capacity: 33.87 GB
Product ID: ATLASU320_36_SCA
Serial Number: 344218950211
Revision: B120
Free Space: 2.00 MB

Array Disk info:
Name: Array Disk 0:1
Vendor: MAXTOR  
Status: ONLINE
Capacity: 33.87 GB
Product ID: ATLASU320_36_SCA
Serial Number: 344223011406
Revision: B120
Free Space: 2.00 MB

Array Disk info:
Name: Array Disk 0:2
Vendor: MAXTOR  
Status: ONLINE
Capacity: 33.87 GB
Product ID: ATLASU320_36_SCA
Serial Number: 344223011497
Revision: B120
Free Space: 2.00 MB

Array Disk info:
Name: Array Disk 0:3
Vendor: MAXTOR  
Status: ONLINE
Capacity: 33.87 GB
Product ID: ATLASU320_36_SCA
Serial Number: 344223011376
Revision: B120
Free Space: 2.00 MB

Array Disk info:
Name: Array Disk 0:4
Vendor: MAXTOR  
Status: ONLINE
Capacity: 33.87 GB
Product ID: ATLASU320_36_SCA
Serial Number: 344223011410
Revision: B120
Free Space: 2.00 MB
0
Comment
Question by:chris_v
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 9

Expert Comment

by:gilbar
ID: 12069002
how about this?
set fso = createobject ("scripting.filesystemobject")
set f = fso.getfile("U:\snum.txt")
fso.createtextfile("u:\short.txt")
set o = fso.getfile("u:\short.txt")
set ts1 = f.openastextstream(1, 0) 'open read only
set ts2 = o.openastextstream(2, 0) 'open readwrite

do while not ts1.atendofstream
      str = ts1.readline
        if left (str,13 ) = "Serial Number" then ts2.writeline(str)
loop
ts1.close
ts2.close
set o = nothing
set f = nothing
set fso = nothing
0
 
LVL 9

Expert Comment

by:gilbar
ID: 12069016
this takes the input file snum.txt on the u:\ drive and writes to short.txt on the u:\ drive
0
 
LVL 2

Author Comment

by:chris_v
ID: 12069063
Close,  I only want the actual numbers though.  Can you take off the "Serial Number:" part?  Also convert to uppercase for serials that may have alpha-numeric.  

Thanks

Great work.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
gilbar earned 500 total points
ID: 12069186
set fso = createobject ("scripting.filesystemobject")
set f = fso.getfile("U:\snum.txt")
fso.createtextfile("u:\short.txt")
set o = fso.getfile("u:\short.txt")
set ts1 = f.openastextstream(1, 0) 'open read only
set ts2 = o.openastextstream(2, 0) 'open readwrite

do while not ts1.atendofstream
     str = ts1.readline
        if left (str,15 ) = "Serial Number: " then
          ts2.writeline(ucase (mid(str,16)))
         end if
loop
ts1.close
ts2.close
set o = nothing
set f = nothing
set fso = nothing
0
 
LVL 2

Author Comment

by:chris_v
ID: 12069216
Works for me.  Thanks.

0
 
LVL 9

Expert Comment

by:gilbar
ID: 12069219
glad to help chris, thanx for points!
0
 
LVL 2

Author Comment

by:chris_v
ID: 12076797
Sorry to bother again,

but what if in the text file the text is indented with several spaces?  

Such as:

           Serial Number:  123456789

vs.  

Serial Number: 123456789

Thanks in advance!!!
0
 
LVL 2

Author Comment

by:chris_v
ID: 12076924
I figured it out.

Thanks
0
 
LVL 9

Expert Comment

by:gilbar
ID: 12077290
chris, i said you've figured it out (and you might be using this already) but here's a function that tells you if one string is in another string:

InStr Function
Returns the position of the first occurrence of one string within another.
**********************************
InStr([start, ]string1, string2[, compare])
Arguments
start
Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start contains Null, an error occurs. The start argument is required if compare is specified.
string1
Required. String expression being searched.
string2
Required. String expression searched for.
compare
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. If omitted, a binary comparison is performed.
Settings
The compare argument can have the following values:

Constant Value Description
vbBinaryCompare 0 Perform a binary comparison.
vbTextCompare 1 Perform a textual comparison.
**********************************
it returns 0 if not found, so if the return is > 0 then it found it.  

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question