Solved

URGENT: Need vbscript to filter text file

Posted on 2004-09-15
9
1,668 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now