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

Replace existing file with save

Status
Not open for further replies.

gismo

Technical User
Jul 24, 2002
35
0
0
US
I am passing a new or existing file to the sub listed below. If the file exists I want to replace it with the new data while keeping the original file name. It currently appends data to an existing file with each call to the sub. Thanks in advance

Code:
Sub SaveToFile(FileNameOrNumber As Variant)
    Dim var As Variant, fnum As Integer
    
    ' store contents into a variant
    var = Contents()
    ' store it to a file
    If VarType(FileNameOrNumber) = vbString Then
        fnum = FreeFile
        Open FileNameOrNumber For Binary As #fnum
        Put #fnum, , var
        Close #fnum
    Else
        Put #fnum, , var
    End If
End Sub
 
Try Changing

Open FileNameOrNumber For Binary As #fnum

to

Open FileNameOrNumber For Output As #fnum

Greg Palmer

----------------------------------------
Any feed back is appreciated.
 
I need binary file format, I believe Ouput only supports an ascii file format.

gismo
 

First get rid of the file if it exists and then open it for binary

If Dir(FileNameOrNumber) <> &quot;&quot; Then Kill FileNameOrNumber
fnum = FreeFile
Open FileNameOrNumber For Binary As #fnum
 
No error and no luck. I watch the file size increase with every save. I don't understand how that could not work.

gismo
 
Nor do I. What does &quot;FileNameOrNumber&quot; contain when the Sub is invoked?
 
I was just checking that. It contains a string that is the full path and file name such as &quot;d:\mystuff\myfile.jnk&quot;
 
Try this ......
To write

Open &quot;file.bin&quot; For Binary Access Write As #1
Put #1, , Text1.Text
Close #1

To read

Dim data
Open &quot;file.bin&quot; For Binary Access Read As #1
Input #1, data
Text1.Text = data
Close #1
 
I'm just an idiot sometimes(more times than I want to admit). Contents() was growing in size everytime I saved ttat was the reason my file size grew with each save. I wrongfully assumed that the file was appending. Thank's anyways guys and yeah I gave myself a kick.

gismo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top