read-modify-write textfile problem

G

GerryM

I can read fileA with OpenTextFile etc. and Close it.
I can then modify the textstream and write it to fileB using either
OpenTextFile("fileB", ForWriting, True) or CreateTextFile("fileB", True).
However, when fileA and fileB are the same I get Error 0x800A0046,
Permission Denied. This does not seem to be a "Permission" problem since I
can read and write as long as the files are different, but what is it and how
do I get around it?

Thanks in advance.

Gerry Metze
 
S

Steve Easton

Ok.
I don't understand "when fileA and fileB are the same"

However one thing I did notice, if "fileA" is a String variable that
is a path to a file like this:

Dim fileA As String

fileA = "C:\somefolder\myfile.txt"

Then you don't need the quotes "" in the function.
It should be:
OpenTextFile(fileA, ForWriting, True)


Unless, fileA lives in the same directory as the application, then you could use
OpenTextFile("fileA.txt", ForWriting, True)

with the quotes.

--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
 
G

GerryM

You are exactly right; I should have been more explicit. I had exactly what
you showed, namely
fileA = "C:\somefolder\myfileA.txt"
fileB = "C:\somefolder\myfileB.txt" ' <<<<< 1 >>>>>
'''' and further:
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set fA = fso_OpenTextFile(fileA, ForReading)
If Not fA.AtEndOfStream Then
TxtArray = Split(fA.ReadAll, vbCrLf)
End If
fA.Close
''' ... now modify TxtArray and write it to fileB:

Set fB = fso.CreateTextFile(fileB, True) ' <<<<< 2 >>>>>
For i = 0 To UBound(TxtArray)
fB.WriteLine TxtArray(i)
Next
fB.Close

This works fine; fileB indeed contains the modified information.
Now if I replace the statement marked <<<<< 1 >>>>> with
fileB = fileA
the program hangs at statement <<<<< 2 >>>>> with Error 0x888A0046,
Permission Denied.

Sorry about being too brief.

Thanks

Gerry Metze
 
S

Steve Easton

OK.

Not sure where the constant comes in to play:
Const ForReading = 1

However, if you want fileB to be the same asFileA you need to use CopyFile
and use CopyFile to remane the file

Dim oldfile As String
Dim newfile As String
oldfile = "C:\path\to\file\fileA.txt"
newfile = "C:\path\to\file\fileB.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(oldfile) Then
fso.CopyFile oldfile, newfile
Else
End If


Keep in mind, if you've already instantiated the FileSystemObject ( fso )
you do not need to recreate it.

You can:
Set fso = CreateObject("Scripting.FileSystemObject")

Dim oldfile As String
Dim newfile As String
oldfile = "C:\path\to\file\fileA.txt"
newfile = "C:\path\to\file\fileB.txt"

If fso.FileExists(oldfile) Then
fso.CopyFile oldfile, newfile
Else
End If

and use the fso anywhere in your routine


--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
 
G

GerryM

Not to drag this out too far --
(1) The constant ForReading is used in the OpenTextFile statement, for
clarity(?). Normally I don't use it and would just write
fso_OpenTextFile(fileA, 1).
(2) CopyFile doesn't work either; I get the same "Permission Denied" error.
I.e., I can't copy to the file I read from.
(3) Back to the original question: Why can't I write (or copy) back to the
same file I read from? (I.e., when fileA and fileB have the same filespec
in my last message.) By the way, I *did* close fileA before trying to write
(or copy) back to it.

Gerry Metze
 
S

Steve Easton

If I understand this correctly, your trying to write back to the file from text array.

You would need to use WriteLine with a Do Until loop until the array is empty.

Also you need to create a second object to control the fso object.

So it takes two objects
Dim fso As Object
Dim WriteIt as object

Set fso = CreateObject("Scripting.FileSystemObject")
Set WriteIt = fso.CreateTextFile(fileA, True)

Do Until ArrayValue = 0
WriteIt.WriteLine (ArrayValue)
Next ArrayValue

' Also when closing the file, make sure you close "both" Objects
' in the reverse order they were opened
WriteIt.Close
fso.close

End Sub



--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top