CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2006
    Posts
    9

    Check for existing file before saving

    I have an application that opens a File Dialog. The dialog is opened as

    FileDialog.showsave

    If the user selects an already existing file I would like to prompt the

    Overwrite? Y/N message box.

    How can this be done in VB6?

    Thanks for all your help

    Andy

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Check for existing file before saving

    You can add a few lines to check to see if the file exists before they save it.
    Code:
    Private Sub cmdSaveFile_Click()
        Dim strFileName As String
        Dim ans As Integer
        CommonDialog1.Flags = &H2 ' Overwrite Flag
        CommonDialog1.Filter = "RTF|*.rtf|Text|*.txt"
        CommonDialog1.ShowSave
        On Error GoTo SaveProblems
        strFileName = CommonDialog1.FileName
        If CommonDialog1.FilterIndex = 1 Then
            CommonDialog1.DefaultExt = "rtf"
            rtb.SaveFile strFileName
        Else
            CommonDialog1.DefaultExt = "txt"
            rtb.SaveFile strFileName, rtfText
        End If
        Exit Sub
    SaveProblems:
            MsgBox "Can’t save the file, try again.", vbCritical
        Exit Sub
    End Sub

    EDIT: Updated it. Now it's version 6 Utilizes an overwrite flag
    Attached Files Attached Files
    Last edited by dglienna; January 31st, 2006 at 12:59 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2006
    Posts
    9

    Re: Check for existing file before saving

    I am not sure if I had phrased my question properly. This is my requirement

    When i click the SAve button on the form, the File Dialog opens. When the user enters a file name to be saved as, I would like to check if the file exists. And if it exists take some action.

    Is there a way this could be done?

    thanks,
    Andy

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Check for existing file before saving

    Code:
    CommonDialog.Flags = &H2
    The above code will display the overwrite prompt MessageBox when a user selects a file that is already present in that folder.

  5. #5
    Join Date
    May 2005
    Location
    Sterling Heights, MI
    Posts
    74

    Re: Check for existing file before saving

    Quote Originally Posted by andysubbu
    I am not sure if I had phrased my question properly. This is my requirement

    When i click the SAve button on the form, the File Dialog opens. When the user enters a file name to be saved as, I would like to check if the file exists. And if it exists take some action.

    Is there a way this could be done?

    thanks,
    Andy
    Try this
    Code:
    Private Sub cmdSave_Click()
    Dim vbResponse As VbMsgBoxResult
    With CommonDialog1
    	.CancelError = False
    	.InitDir = "d:\"
    	.Filter = "Text Files (*.txt)|*.txt|"
    	.ShowSave
    End With
    If Dir$(CommonDialog1.FileName) <> vbNullString Then
    vbResponse = MsgBox(CommonDialog1.FileName & " currently exists.   Do you want to save it anyway?", vbYesNo + vbQuestion, "Overlay existing file?")
    	   If vbResponse = vbYes Then
    		  Kill CommonDialog1.FileName
    		  Open CommonDialog1.FileName For Append As #1
    			 Print #1, Text1.Text
    		  Close #1
    	   End If
    End If
    End Sub

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Check for existing file before saving

    Quote Originally Posted by Shuja Ali
    Code:
    CommonDialog.Flags = &H2
    The above code will display the overwrite prompt MessageBox when a user selects a file that is already present in that folder.
    That's even a better way. For some reason, today, the app.path wasn't needed in my if statement. I don't know why it was working yesterday, though. Very odd. I'm replacing my code with new code utilizing the flag.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Check for existing file before saving

    You can also opt for:
    Code:
    CommonDialog.Flags = &H2 Or &H4
    The only difference from just &H2 is cosmetic, though, as it removes blank space from the bottom of the box (&H4 is also used to remove the "Open file as read only" checkbox from ShowOpen).

  8. #8
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Check for existing file before saving

    Just to clear the things a bit, the value &H2 is the value of a predefined constant you can use:

    Me.CommonDialog1.Flags = cdlOFNOverwritePrompt

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured