Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text File Manipulation again...

Status
Not open for further replies.

alspoll

Technical User
Joined
Feb 4, 2006
Messages
6
Location
US
Hello all,

I have been searching these threads for a script to format a list i have in a text file. The text is formatted as

3RD AND THE MORTAL, THE\MEMOIRS

What I wold like is

MEMOIRS\3RD AND THE MORTAL, THE

I have this script which i used for a names list, but I do not know how to change it to reflect the new text.

Dim FSO, objFile, RegEx, strDestin, strFile, strSource

Set FSO = CreateObject("Scripting.FileSystemObject")
Set RegEx = New RegExp

RegEx.Global = True
RegEx.Pattern = "^([^,]*), (.*)$"

Const ForReading = 1
strSource = "c:\A.txt"
strDestin = "c:\A.txt"

Set objFile = FSO.OpenTextFile(strSource, ForReading)
strFile=""
if not objFile.atendofstream then
strFile = objFile.ReadAll
end if
objFile.Close

dim a
a=split(strFile,vbcrlf)
for i=0 to ubound(a)
a(i)=RegEx.Replace(a(i), "$2 $1")
next

Set objFile = FSO.CreateTextFile(strDestin, True)
objFile.Write join(a,vbcrlf)
objFile.Close


Any help would be most appreciated.

TIA,
AL
 
Why not simply use the Split function ?
...
Dim myArray
For i = 0 To UBound(a)
myArray = Split(a(i), "\", 2)
If UBound(myArray) > 0 Then
a(i) = myArray(1) & "\" & myArray(0)
End If
Next
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you...

not to ask a stupid question, but do i just insert that text into the script and do not chnage the regex pattern?

Dim FSO, objFile, RegEx, strDestin, strFile, strSource

Set FSO = CreateObject("Scripting.FileSystemObject")
Set RegEx = New RegExp

RegEx.Global = True
RegEx.Pattern = "^([^,]*), (.*)$"

Const ForReading = 1
strSource = "c:\A.txt"
strDestin = "c:\A.txt"

Set objFile = FSO.OpenTextFile(strSource, ForReading)
strFile=""
if not objFile.atendofstream then
strFile = objFile.ReadAll
end if
objFile.Close

Dim myArray
For i = 0 To UBound(a)
myArray = Split(a(i), "\", 2)
If UBound(myArray) > 0 Then
a(i) = myArray(1) & "\" & myArray(0)
End If
Next

Set objFile = FSO.CreateTextFile(strDestin, True)
objFile.Write join(a,vbcrlf)
objFile.Close
 
My suggestion was to NOT use RegExp ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Told you it was a stupid question...

Dim FSO, objFile, strDestin, strFile, strSource

Set FSO = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1
strSource = "c:\A.txt"
strDestin = "c:\A.txt"

Set objFile = FSO.OpenTextFile(strSource, ForReading)
strFile=""
if not objFile.atendofstream then
strFile = objFile.ReadAll
end if
objFile.Close

Dim myArray
For i = 0 To UBound(a)
myArray = Split(a(i), "\", 2)
If UBound(myArray) > 0 Then
a(i) = myArray(1) & "\" & myArray(0)
End If
Next

Set objFile = FSO.CreateTextFile(strDestin, True)
objFile.Write join(a,vbcrlf)
objFile.Close

this would be correct then?

Thanks again.
 
Depends... If you still want to apply tsuji's suggestion from your other thread (thread329-1187084), then try this:

Code:
For i = 0 To UBound(a)
  [COLOR=red]a(i)=RegEx.Replace(a(i), "$2 $1")[/color]
  myArray = Split(a(i), "\", 2)
  If UBound(myArray) > 0 Then
    a(i) = myArray(1) & "\" & myArray(0)
  End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top