Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

URGENT: Need vbscript to filter text file

Posted on 2004-09-15
9
Medium Priority
?
1,777 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Accepted Solution

by:
gilbar earned 2000 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

885 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