Solved

URGENT: Need vbscript to filter text file

Posted on 2004-09-15
9
1,707 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
  • 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

756 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