joha0193
asked on
Editing a text file with VBScript.
I need a vbscript that will take a text file that has data that looks exactly like the following (the ... is just to note that there can be many names in the list, it doesn't actually appear in the list):
"Durand Schools","2363","2 Hours late","","","","","","Scho ol","18:25 "
"Anoka County School District","2543","Closed", "","",""," ","","Scho ol","12:25 "
...
...
And output:
"Durand Schools","2 Hours late"
"Anoka County School District","Closed"
...
...
Im very new to scripting, but this seems like something that should be doable. The tricky part for me is dealing with the random lengths of data. There could be anywhere from 0 to 200+ schools listed so it definitely needs a loop. Thank you in advance for your help.
"Durand Schools","2363","2 Hours late","","","","","","Scho
"Anoka County School District","2543","Closed",
...
...
And output:
"Durand Schools","2 Hours late"
"Anoka County School District","Closed"
...
...
Im very new to scripting, but this seems like something that should be doable. The tricky part for me is dealing with the random lengths of data. There could be anywhere from 0 to 200+ schools listed so it definitely needs a loop. Thank you in advance for your help.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, Sirbounty. Basically exactly the same thing as yours. That'd be me too slow at typing.
Chris
Thanx IM - good spot...
I'm trying to put 'this' down for the evening...I forgot how addicting it can be! lol
I'm trying to put 'this' down for the evening...I forgot how addicting it can be! lol
Here is a version that doesn't use a secondary file:
Option Explicit
Dim fileName
fileName = "c:\someFile.txt"
Dim fso, f
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
If fso.FileExists(fileName) Then
Set f = fso.OpenTextFile(fileName, ForReading, False)
Dim lines
lines = Split(f.ReadAll, vbCrLf)
f.Close
Dim i
Dim values
For i = LBound(lines) To UBound(lines)
values = Split(lines(i), ",")
lines(i) = values(0) & "," & values(2)
Next
lines = Join(lines, vbCrLf)
Set f = fso.OpenTextFile(fileName, ForWriting, True)
f.Write(lines)
f.Close
MsgBox fileName, vbOKOnly, "File Modified"
Else
MsgBox fileName, vbOKOnly, "File Not Found"
End If
Option Explicit
Dim fileName
fileName = "c:\someFile.txt"
Dim fso, f
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.Fi
If fso.FileExists(fileName) Then
Set f = fso.OpenTextFile(fileName,
Dim lines
lines = Split(f.ReadAll, vbCrLf)
f.Close
Dim i
Dim values
For i = LBound(lines) To UBound(lines)
values = Split(lines(i), ",")
lines(i) = values(0) & "," & values(2)
Next
lines = Join(lines, vbCrLf)
Set f = fso.OpenTextFile(fileName,
f.Write(lines)
f.Close
MsgBox fileName, vbOKOnly, "File Modified"
Else
MsgBox fileName, vbOKOnly, "File Not Found"
End If
ASKER
Wow, that was awesomely fast. I meant to divide the points up, but I seem to have screwed up. Is there anyway for me to go back and split them between you two?
Yep, you can stick a post here:
https://www.experts-exchange.com/Community_Support/
Put a link to the question and they'll fix it all for you.
Chris
Change:
PutFile.WriteLine strData(0) & "," & strData(1)
To:
PutFile.WriteLine strData(0) & "," & strData(2)