• Status: Solved
• Priority: Medium
• Security: Public
• Views: 277

# 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.
0
cbones
• 7
• 5
• 2
1 Solution

Developer & EE ModeratorCommented:
x="1234567890"
x="("&left(x,3)&")"&mid(x,4,6)&"-"&right(x4)
0

Author 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

Developer & 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
0

Commented:
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

Author 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

Commented:
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

Author Commented:
Bp,

Yes, that would be perfect.

Thank you.
0

Commented:
Okay, I'm going to try and work up a regex approach in VBS, be back in a bit...

~bp
0

Commented:
Is it possible for you to provide a test file?

~bp
0

Commented:
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 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)
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
~bp
0

Author 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

Commented:
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

Author Commented:
Yes, look at one folder, not subfolders.
All files in that folder.
Overwrite each file.

Thank you.
0

Commented:
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 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.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)
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
~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.