Solved

manipulate txt file with VB Script

Posted on 2011-10-01
5
257 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
[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
  • 3
  • 2
5 Comments
 
LVL 54

Expert Comment

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

~bp
0
 
LVL 54

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 54

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

752 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