File Format

Hello,

I was looking for a way in vbscript or a batch file to change any 10 digit numbers inside a .txt file from this format 1234567890 to this (123)456-7890.

Can this be done?

Thank you.
cbonesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
x="1234567890"
x="("&left(x,3)&")"&mid(x,4,6)&"-"&right(x4)
0
cbonesAuthor Commented:
Thank you for your reply.  The numbers aren't always in that order.  It can be any combination.   How can I get it to reformat it with any 10 digits?

Thank you.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
It does not matter the order.

Try this as a function instead.  Place this on your page/code.  Then save it to a variable like below where the number can be any order.

MyGoodNumbe=formatPhone("2223338888")



function formatPhone(x)
    if len(x)=10 then
         formatPhone=("&left(x,3)&")"&mid(x,4,6)&"-"&right(x4)
    else
         formatPhone="Error:I need 10 numbers"

    end if
end function

Open in new window

0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Bill PrewCommented:
Will the 10 digit numbers always have spaces on either side of them?  Or something else?  If not, can a case like this ever exist?

123456789012345

~bp
0
cbonesAuthor Commented:
Bp,

They will never have a space on either side.  They will also have a line (|) in front of them and always be 10 digits.   There may be other number sequences in the file like you have above, but the numbers that need to be adjusted are 10 digits.

Thank you.
0
Bill PrewCommented:
Well, I'm trying to get my head around which 10 digit clusters we adjust, since there can be other clusters of 11+ digits.  So should we look for any grouping of 10 digits where the character before and after that 10 digit group is NOT a digit?

~bp
0
cbonesAuthor Commented:
Bp,

Yes, that would be perfect.

Thank you.
0
Bill PrewCommented:
Okay, I'm going to try and work up a regex approach in VBS, be back in a bit...

~bp
0
Bill PrewCommented:
Is it possible for you to provide a test file?

~bp
0
Bill PrewCommented:
Okay, give this a try, it does what you asked for.  Save as a VBS, and run as follows:

cscript ee28225271.vbs in.txt out.txt

or

cscript ee28225271.vbs in.txt

In the first, it will read the first file on the command line, make the changes, and then write the results to the second file on the command line.  In the second example it will read the first file, and overwrite it with the changed version.
' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' 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
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

' Replace desired strings
Set oRegExp = New RegExp
oRegExp.Global = True
oRegExp.IgnoreCase = False
oRegExp.Pattern = "(\D)(\d\d\d)(\d\d\d)(\d\d\d\d)(\D)"
sData = oRegExp.Replace(sData, "$1($2)$3-$4$5")

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
Wscript.Quit

Open in new window

~bp
0
cbonesAuthor Commented:
Bp,

That worked perfect.  Can that be changed to search a directory for files, instead of one file at a time?

Thank you.
0
Bill PrewCommented:
Yes.  So let me double check.  

You want to look at the files in one folder, not also all the files in any subfolders?

And, do you want to process all files in that folder, or just ones that match some type of naming patter, maybe a certain extension, etc?

And you would want to overwrite each file with the updated version?

~bp
0
cbonesAuthor Commented:
Yes, look at one folder, not subfolders.
All files in that folder.
Overwrite each file.

Thank you.
0
Bill PrewCommented:
Okay, give this a try.  It now takes one command line parameter which is the folder where the files exist to process.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input folder name from command line parm
If (WScript.Arguments.Count > 0) Then
   sInDir = WScript.Arguments(0)
Else
   WScript.Echo "No folder specified."
   WScript.Quit
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Make sure specified folder to process exists
If Not oFSO.FolderExists(sInDir) Then
   WScript.Echo "Folder [" & sInDir & "] not found."
   WScript.Quit
End If

Set oFolder = oFSO.GetFolder(sInDir)
For Each oFile in oFolder.Files

   Wscript.Echo "Processing:[" & oFile.Path & "]"

   ' Read entire input file into a variable and close it
   Set oInfile = oFSO.OpenTextFile(oFile.Path, ForReading, False, TriStateUseDefault)
   sData = oInfile.ReadAll
   oInfile.Close
   Set oInfile = Nothing

   ' Replace desired strings
   Set oRegExp = New RegExp
   oRegExp.Global = True
   oRegExp.IgnoreCase = False
   oRegExp.Pattern = "(\D)(\d\d\d)(\d\d\d)(\d\d\d\d)(\D)"
   sData = oRegExp.Replace(sData, "$1($2)$3-$4$5")

   ' Write file with any changes made
   Set oOutfile = oFSO.OpenTextFile(oFile.Path, ForWriting, True)
   oOutfile.Write(sData)
   oOutfile.Close
   Set oOutfile = Nothing

Next

' Cleanup and end
Set oFSO = Nothing
WScript.Quit

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.