|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to open file for output but not overwrite?I want to open a file for output, but not to overwrite
it if it exists? tia. Try the Append mode.
-- Show quoteHide quoteChris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp Veign's Blog http://www.veign.com/blog -- <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. > > > I want to open a file for output, but not Give this example "structure" a try...> to overwrite it if it exists? 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 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 > > 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 >> >> > > > Thank you, but you have not understood: Use this function...> 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(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 On Tue, 31 Jan 2006 17:16:20 -0000, <Darrell C> wrote:
>Thank you, but you have not understood: Function FileExists(Fle$) As Boolean>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?" 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 Thank you all, but is there any special reason why not to use
the simple dir function ? 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 ? > > > Thank you all, but is there any special reason There is no problem using the Dir function UNLESS you are already using it> why not to use the simple dir function ? 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 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!! 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, I suggest you replace that Dir() with FileExists ASAP>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!! - 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 <needhelp> wrote in message news:eFc01PtJGHA.3984@TK2MSFTNGP14.phx.gbl... There are certain circumstances that cause Dir to lock a folder. That means, > 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!! 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 |
|||||||||||||||||||||||