Home All Groups Group Topic Archive Search About

How to open file for output but not overwrite?

Author
31 Jan 2006 4:33 PM
Darrell C
I want to open a file for output, but not to overwrite
it if it exists?
tia.

Author
31 Jan 2006 4:36 PM
Veign
Try the Append mode.

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
Veign's Blog
http://www.veign.com/blog
--


Show quoteHide quote
<Darrell C> wrote in message news:ObWTPQoJGHA.1848@TK2MSFTNGP12.phx.gbl...
>I want to open a file for output, but not to overwrite
> it if it exists?
> tia.
>
>
Author
31 Jan 2006 4:53 PM
Rick Rothstein [MVP - Visual Basic]
> I want to open a file for output, but not
> to overwrite it if it exists?

Give this example "structure" a try...

Dim FileNum As Long
Dim YourFileNameAndPath As String
YourFileNameAndPath = "c:\Dir1\Dir2\Etc\FileName.txt"
FileNum = FreeFile
Open YourFileNameAndPath For Append As #FileNum
Print #FileNum, "This line is written at end of existing text"
Print #FileNum, "This line will follow the line above"
Close #FileNum

Rick
Author
31 Jan 2006 5:16 PM
Darrell C
Thank you, but you have not understood:
I do not want to "Append", what I do need
is to ask the user

"File already exists, do you want to over-wright it?"

?
tia


Show quoteHide quote
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:OsNksboJGHA.3352@TK2MSFTNGP12.phx.gbl...
> > I want to open a file for output, but not
> > to overwrite it if it exists?
>
> Give this example "structure" a try...
>
> Dim FileNum As Long
> Dim YourFileNameAndPath As String
> YourFileNameAndPath = "c:\Dir1\Dir2\Etc\FileName.txt"
> FileNum = FreeFile
> Open YourFileNameAndPath For Append As #FileNum
> Print #FileNum, "This line is written at end of existing text"
> Print #FileNum, "This line will follow the line above"
> Close #FileNum
>
> Rick
>
>
Author
31 Jan 2006 5:27 PM
Saga
strFileName - contain file name to possibly overwrite
  intFH - contains file handle number

  if dir$(strFileName)="" then
    'File does not exist, just open
    open strFilenama for output as #intFH
  else
    'File exists, ask user.
    select case msgbox("File exists, overwrite?",vbYesNo)
      case vbYes
         'open file for output, overwriting
         open strFileName for output  as #intFH
      case vbNo
        'open file for append
        open strFileName for append  as #intFH
    end select
   end if

Is this what you are looking for?

Saga



<Darrell C> wrote in message
Show quoteHide quote
news:uHOlSooJGHA.2912@tk2msftngp13.phx.gbl...
> Thank you, but you have not understood:
> I do not want to "Append", what I do need
> is to ask the user
>
> "File already exists, do you want to over-wright it?"
>
> ?
> tia
>
>
> "Rick Rothstein [MVP - Visual Basic]"
> <rickNOSPAMnews@NOSPAMcomcast.net>
> wrote in message news:OsNksboJGHA.3352@TK2MSFTNGP12.phx.gbl...
>> > I want to open a file for output, but not
>> > to overwrite it if it exists?
>>
>> Give this example "structure" a try...
>>
>> Dim FileNum As Long
>> Dim YourFileNameAndPath As String
>> YourFileNameAndPath = "c:\Dir1\Dir2\Etc\FileName.txt"
>> FileNum = FreeFile
>> Open YourFileNameAndPath For Append As #FileNum
>> Print #FileNum, "This line is written at end of existing text"
>> Print #FileNum, "This line will follow the line above"
>> Close #FileNum
>>
>> Rick
>>
>>
>
>
Author
31 Jan 2006 5:52 PM
Darrell C
Thank you, yes this has solved the problem very nicely. Thanks.
Author
31 Jan 2006 5:57 PM
Rick Rothstein [MVP - Visual Basic]
> Thank you, but you have not understood:
> I do not want to "Append", what I do need
> is to ask the user
>
> "File already exists, do you want to over-wright it?"

Use this function...

Function FileExists(PathAndFileName As String) As Boolean
  Dim FileAttributes As Long
  On Error Resume Next
  FileAttributes = GetAttr(PathAndFileName)
  If Err = 0 Then
    If (FileAttributes And vbDirectory) = 0 Then
      FileExists = True
    End If
    Err.Clear
  End If
End Function

like this...

Dim Answer As Long
If FileExists("c:\dir1\dir2\yourfile.txt") Then
  Answer = MsgBox("File exists" & vbCrLf & _
                "Do you want to overwrite it?", vbYesNo)
End If
If Answer = vbYes Then
  ' Open the file for overwriting
End If

Rick
Author
31 Jan 2006 6:32 PM
J French
On Tue, 31 Jan 2006 17:16:20 -0000, <Darrell C> wrote:

>Thank you, but you have not understood:
>I do not want to "Append", what I do need
>is to ask the user
>
>"File already exists, do you want to over-wright it?"

Function FileExists(Fle$) As Boolean
    Dim Q%
    On Error Resume Next
    Q = GetAttr(Fle$)
    If Err = 0 Then
       If (Q And vbDirectory) = 0 Then
          FileExists = True
       End If
    End If
    Err.Clear
End Function

' ---
Function DirExists(ADir$) As Boolean
    Dim Q%
    On Error Resume Next
    Q = GetAttr(ADir$)
    If Err = 0 Then
       If (Q And vbDirectory) = vbDirectory Then
          DirExists = True
       End If
    End If
    Err.Clear
End Function
Author
31 Jan 2006 9:52 PM
needhelp
Thank you all, but is there any special reason why not to use
the simple dir function ?
Author
31 Jan 2006 9:55 PM
Saga
Most likely it has to do with performance, or that the dir function,
although more starightforward, may not contemplate a specific
condition which the other method does.

Saga

<needhelp> wrote in message
Show quoteHide quote
news:u5W7pCrJGHA.3856@TK2MSFTNGP12.phx.gbl...
> Thank you all, but is there any special reason why not to use
> the simple dir function ?
>
>
Author
31 Jan 2006 10:29 PM
Rick Rothstein [MVP - Visual Basic]
> Thank you all, but is there any special reason
> why not to use the simple dir function ?

There is no problem using the Dir function UNLESS you are already using it
elsewhere. For example, if you are executing a loop using the Dir function
(with an initial pattern), and then jump into code that uses the Dir
function (with a different pattern), it will screw up the first loops Dir
pattern and your initial loop will fail. Unless you are attuned to this
problem, it becomes an extremely hard bug to track down.

Rick
Author
1 Feb 2006 2:05 AM
needhelp
I'll put a rem near my dir to alert myself of these replies. Chances are,
in a month time i'll forget it all, and then if one day I borrow from my
own code, I could be bitten. Many thanks!!
Author
1 Feb 2006 11:13 AM
J French
On Wed, 1 Feb 2006 02:05:14 -0000, <needhelp> wrote:

>I'll put a rem near my dir to alert myself of these replies. Chances are,
>in a month time i'll forget it all, and then if one day I borrow from my
>own code, I could be bitten. Many thanks!!

I suggest you replace that Dir() with FileExists ASAP
- better safe than sorry
- also it gets the routine in just one place, which is handy

Actually some people recommend using the FindFirstFile API
- something to do with Rights on Networks
Author
1 Feb 2006 3:16 PM
Ken Halter
<needhelp> wrote in message news:eFc01PtJGHA.3984@TK2MSFTNGP14.phx.gbl...
> I'll put a rem near my dir to alert myself of these replies. Chances are,
> in a month time i'll forget it all, and then if one day I borrow from my
> own code, I could be bitten. Many thanks!!

There are certain circumstances that cause Dir to lock a folder. That means,
if your app happened to be creating a temp folder, writing a bunch of files
to that folder, the app would be unable to clear and delete that folder when
you wanted it to do to Dir's lock.

If you do use Dir, after you're finished using it, use something like...

Call Dir$("C:\*.*")

....to release the lock. Royal PITA. The GetAttr method to test for
'FileExists' doesn't suffer from this problem.

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm