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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

edit text file with Scripting Runtime 2

Status
Not open for further replies.

p27br

Programmer
Aug 13, 2001
516
GB
hello,

I am trying to achieve the following : I have a text file in which I have file names with full path. I nees to strip the full path and leave only the file name.
But the file object cannot be opened for modifying, only read,write or append.
So I figured I should read the original file and rewrite it line by line to a new file changing the lines I need to change.
My question is , is this the best way to do it ?
here is my code so far :

Sub RecreateFile(ByVal strFile As String, ByVal strSearchString As String)

Dim fs As FileSystemObject
Dim flFileFrom, flFileTo As file
Dim txsStreamFrom, txsStreamTo As TextStream
Dim strTextLine As String
Dim arrPath()


Set fs = New Scripting.FileSystemObject
Set flFileFrom = fs.GetFile(strFile)
fs.CopyFile strFile, "temp.txt"
Set flFileTo = fs.CreateTextFile(strFile)
Set txsStreamFrom = flFileFrom.OpenAsTextStream(ForReading)
Set txsStreamTo = flFileTo_OpenAsTextStream(ForWriting)

With txsStreamFrom
Do
strTextLine = .ReadLine
If strTextLine = strSearchString Then
arrPath() = Split(strTextLine, "\")
strTextLine = Left$(arrPath(UBound(arrPath)), 8) & Right$(arrPath(UBound(arrPath)), 4)
End If
txsStreamTo.WriteLine strTextLine
Loop Until .AtEndOfStream
.Close
End With

End Sub


I get a type mismatch on the following line :
Set flFileTo = fs.CreateTextFile(strFile)


thanks for any suggestions
 
Hi,

Just a note to do with variable declarations (probably has nothing to do with your problem directly).

Dim flFileFrom, flFileTo As file
Dim txsStreamFrom, txsStreamTo As TextStream

In these declarations flFileFrom and txsStreamFrom will remain uninitialized until you assign values to them. You can do multiple assignments on one line but you still need to difine the types for each...

eg.

Dim flFileFrom As File, flFileTo As file

You can prove this with the following example using VarType (VarType returns a value indicating the Type of the passed variable 2 = integer 0 = uninitialized)...

Dim a As Integer
Dim b As Integer

MsgBox VarType(a) & " " & VarType(b)

'Displays: 2 2 (both variables are integers)

Where as...

Dim a, b As Integer

MsgBox VarType(a) & " " & VarType(b)

'Displays: 0 2 (a is uninitialized, b is an integer)

'If you subsequently do...

a = b

MsgBox VarType(a) & " " & VarType(b)

'Now Displays: 2 2 (both variables are integers)
'As a has been cast to the same data type as b
'this can be as error prone as not defining variables
'as all.
'This can also slow down your code execution.





There are two ways to write error-free programs; only the third one works.
 
GHolden is definately onto something there that's easy to forget. I'll try to address your challenge, allthough I don't use the filesystemobject properties and methods the same way as you do.

One of the challenges here, is that both your file variables are assigned the strFile.

What I do, is assign the textfiles directly to textstream objects, and in the case of creating a new one from an existing one, I create two different files. Read from one and append to the other, and copy, move, delete... afterwards. Something like this (note the little disclaimer at the bottom)

[tt] Dim fs As FileSystemObject
Dim txsFileFrom As TextStream
Dim txsFileTo As TextStream
Dim strNewFile as String
Dim strTextLine As String
Dim arrPath()

' assigning name to temporary file
strNewFile = mid$(strPath, 1, len(strPath)-3) & "tmp"
Set fs = New Scripting.FileSystemObject
' open file and create new
Set txsFileFrom = fs.opentextfile(strFile, ForReading)
Set txsFileTo = fs.CreateTextFile(strNewFile, True)

With txsFileFrom
Do While Not .AtEndOfStream
strTextLine = .ReadLine
If strTextLine = strSearchString Then
arrPath() = Split(strTextLine, "\")
strTextLine = Left$(arrPath(UBound(arrPath)), 8) & Right$(arrPath(UBound(arrPath)), 4)
End If
txsFileTo.WriteLine strTextLine
Loop
.Close
End With
txsNewFile.close
set txsNewFile = nothing
set txsFile=nothing
fs.CopyFile strNewFile, strFile, True
set fs = nothing[/tt]

Please not that these amendments are typed not tested, but (very) similar (exept for typos;-)) works on my setup.

Hope this gets you nearer your goal.

Roy-Vidar
 
I just did something similar. Here is my code, maybe that will help.

Function PrepareTextFile()
''-- Read the text file and strip out all unneeded lines.
Dim objFSO As Object
Dim objTextStream As Variant

Dim tempErr As String
Dim tempdate As String
Dim twoAlpha As String
Dim fourNumber As String

''''''Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

Const strFileName = "C:\MyDir\BCBS.txt"
Const fsoForReading = 1

If objFSO.FileExists("C:\MyDir\BCBS.txt") Then
'The file exists, so open it
Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading)
Else
MsgBox "File not found - check directory for file"
Exit Function
End If

Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\Mydir\bcbsout.txt", True)

While Not objTextStream.AtEndOfStream
textData = objTextStream.ReadLine()
'-- check for line to keep
testLine = Left(LTrim(textData), 8)
If Len(testLine) < 1 Then
'- skip line
GoTo LoopBack
End If
'------- invoice line -------.
If IsNumeric(testLine) Then
tf.WriteLine (textData)
End If
tempdate = Mid(textData, 60, 8)
If IsDate(tempdate) Then
tf.WriteLine (textData)
End If
'-- check for the Error content
twoAlpha = Mid(textData, 105, 2)
fourNumber = Mid(textData, 107, 4)
If ((twoAlpha >= &quot;A&quot; And twoAlpha <= &quot;z&quot;) And IsNumeric(fourNumber)) Then
tf.WriteLine (textData)
End If
LoopBack:
Wend

tf.WriteLine (&quot;---- End of Records&quot;)

tf.Close
Set tf = Nothing
objTextStream.Close
Set objTextStream = Nothing

MsgBox &quot;Text File finished processing&quot;

End Function
 
thank you all for your help.
I used RoyVidar's code and it works great.

thanks
 
what reference do i need to load to get the FSO?
my debug is debugging me!
 
Thanks Roy, That was self explanatory!
Thanks Mr. Gates for making it intuitive!

Perfect!
Heres what i got...
******************
Private Sub newsletterBtn_Click()
Dim objOutlook As Object 'Outlook.Application
Dim objEmail As Object 'Outlook.MailItem
Dim subject, email As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String

email = Me.emailTo
subject = Me.emailSubj

Set fso = New FileSystemObject
Set MyBody = fso_OpenTextFile("c:\databaseHS\index.txt", ForReading, False, TristateUseDefault)
MyBodyText = MyBody.ReadAll
MyBody.Close

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = email
.subject = subject
.HTMLBody = MyBodyText
.Send
End With

Set objEmail = Nothing

'objOutlook.Quit

End Sub
******************

Thanks all for your helpful posts!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top