Results 1 to 6 of 6

Thread: Saving a file

  1. #1

    Thread Starter
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305
    I'm saving a listbox to a normal Text-File
    with a common dialogue.
    Now I want, that a message box appears when a
    file(name) alredy exists. There, the user can decide,
    if he wants to overwright the old file.

    How do I program this message box in VB3 ???

    Thanks for some help, Matt

  2. #2
    Lively Member
    Join Date
    Apr 2000
    Posts
    110

    Set this flag

    I don't know if this will work in VB3, but give it a go... Set the following flag for the file save dialogue.

    cdlOfNOOverwritePrompt

    Code:
    Private sub cmdSave_Click()
    Dim strFilename as string
    
    'common dialogue called cdl1
    
    'Prompt the user if they select a file that already exists.
    cdl1.Flags = cdlOfNOOverwritePrompt
    cdl1.ShowSave
    strFilename = cdl1.Filename
    
    End sub
    If you just want to check the existence of a file, use DIR as Batman mentioned.

    Code:
    'Function...
    Public Function FileExists(ByVal strFilename as string) As Boolean
        
        If Dir(strFilename, vbNormal) <> "" Then
            FileExists = True
        Else
            FileExists = False
        End If
    
    End Function
    
    'To use call the following.
    Dim intRet as integer
    
    If FileExists("C:\Text.txt") Then
         intRet = Msgbox("Are you sure you want to overwrite the file " & strFilename & " ?", vbYesNO + vbdefaultButton2)
         Select Case intRet
              Case vbYes
              'Overwrite file
         Case Else
              exit sub
         End select
    Else
    'Save file, it doesn't al ready exist.
    End IF
    Hope this helps in some way.

    Laterz

    REM


  3. #3
    New Member mwildam's Avatar
    Join Date
    Feb 2000
    Location
    Austria
    Posts
    14

    Smile

    Here is another method for checking file existence as I read somewhere that the GetAttr method is a little faster than the Dir function.

    Public Function Exist(FileOrDirName As String) As Boolean
    Dim FNum As Integer
    Dim a As Integer

    On Error Resume Next
    Err.Clear
    If InStr(1, FileOrDirName, "*") = 0 And InStr(1, DateiOderVerzName, "?") = 0 Then
    a = GetAttr(FileOrDirName)
    If Err.Number <> 0 Then
    Exist = False
    Err.Clear
    Else
    Exist = True
    End If
    Else
    If Dir(FileOrDirName) <> "" Then Exist = True Else Exist = False
    End If
    End Function


    Here 2 functions for separate check if a file or folder exists:

    Public Function CheckFile(FileName As String) As Boolean
    Dim a As Integer

    On Error Resume Next
    Err.Clear
    a = GetAttr(FileName)
    If Err.Number <> 0 Or ((a And vbDirectory) = vbDirectory) Then
    Err.Clear
    CheckFile = False
    Else
    CheckFile = True
    End If
    End Function

    Public Function CheckDir(DirName As String) As Boolean
    Dim a As Integer

    On Error Resume Next
    Err.Clear
    a = GetAttr(DirName)
    If Err.Number <> 0 Or ((a And vbDirectory) <> vbDirectory) Then
    Err.Clear
    CheckDir = False
    Else
    CheckDir = True
    End If
    End Function

    Using VBS, Java, JavaScript

  4. #4
    New Member
    Join Date
    Jun 2000
    Location
    SWEDEN
    Posts
    1

    Lightbulb

    Use the FileLen function.

    If it returns a error then the file does not exist.

  5. #5
    New Member
    Join Date
    Aug 1999
    Location
    Denver, CO, US
    Posts
    2
    If you OR the Dir arguments together, then one function can be used for all types of files or directories.

    Code:
    'Find out if a file (or directory) exists
    Private Function Exists(ByVal sFileNameA As String) As Boolean
    
        Dim bExists As Boolean
        
        bExists = False
        If Dir$(sFileNameA, vbNormal + vbHidden + vbSystem + vbVolume + _
            vbDirectory) <> "" Then
            bExists = True
        End If
    
        Exists = bExists
        
    End Function
    The most likely way for the world to be destroyed, most experts agree, is by
    accident. That's where we come in; we're computer professionals. We cause
    accidents.

    - Nathaniel Borenstein

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    As a rule use OR operator instead of + when you add flags,
    Code:
    Fileexist=cbool(len(Dir$(FileName, vbNormal OR vbHidden OR vbSystem OR vbVolume OR vbDirectory)))
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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