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 = flFileT
penAsTextStream(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
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 = flFileT
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