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
Solved

manipulate txt file with VB Script

Posted on 2011-10-01
5
251 Views
Last Modified: 2012-05-12
I have a tab-delimited text file (with headers) where the third column is a check number.

Is there a way using VB Script to open/read the txt file, pad all the check numbers out to n digits using zeros and close it?

Thanks,
McOz
0
Comment
Question by:McOz
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36896960
Sure.  Can you provide a sample test data file?

~bp
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36896970
Need to know how many columns there are in total also.

~bp
0
 
LVL 9

Author Comment

by:McOz
ID: 36896977
Here is a very simple example. The user wants to be able to just drop the txt file onto the script, so you can just grab the filepath from WScript.Arguments(0)

Tx McOz
SampleDataFile.txt
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 36896992
Here's my first shot at this, should get the job done.  You just have to adjust the length in the LeftPad call.

It can be run with either 1 or 2 command line parms.  If one it rewrites the input file.  If 2, then it preserves the input file, and writes the changed version to the second parm.  Helpful for testing...

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
Delim = Chr(9)
 
' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  strInFile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  strOutFile = WScript.Arguments(1)
Else
  strOutFile = strInFile
End If

' Read file into an array
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
arrLines = Split(objFile.ReadAll, VbCrLf)
objFile.Close

' Output file with any changes made
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)

' Loop through all lines of the file, pad 3rd field on left with leading zeros
for i = 0 To UBound(arrLines)
    If arrLines(i) <> "" Then
        arrFields = Split(arrLines(i), Delim)
        ' First line will be header, don't pad on it
        If i > 0 Then
          arrFields(2) = LeftPad(arrFields(2), 20, "0")
        End If
        objFile.WriteLine Join(arrFields, Delim)
    End If
Next

' Close output file
objFile.Close

Function RightPad( strText, intLen, chrPad )
  RightPad = Left( strText & String( intLen, chrPad ), intLen )
End Function

Function LeftPad( strText, intLen, chrPad )
  LeftPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function

Open in new window

~bp
0
 
LVL 9

Author Closing Comment

by:McOz
ID: 36897554
Perfect!! Thanks a lot.

-McOz
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

837 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