Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URGENT: Need vbscript to filter text file

Posted on 2004-09-15
9
Medium Priority
?
1,758 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
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 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…
Suggested Courses

715 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