Results 1 to 9 of 9

Thread: [RESOLVED] how to check if a txt file is not in the path and create it

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] how to check if a txt file is not in the path and create it

    i have a code that opens a txtfile
    now my question is if this file dosnt exists how do i create it?
    this is my code

    e.x
    if txtdata.txt dosnt exists then to create it and continue with the code

    Code:
            Dim NameFile
            NameFile = FreeFile
            Open "C:\PremiumGold_USER_DATA\txtdata.txt" For Output As #NameFile
            Print #1, TxtCust.Text
            Print #1, TxtPay.Text
            Print #1, CmbMethod.Text
            Print #1, CmbPay.Text
            Close #1
    
            Dim Slika As String
            Dim Filename As String
            Filename = "C:\PremiumGold_USER_DATA\pfs.exe"
            Slika = Shell(Filename, vbNormalFocus)
    any help will be appreciated
    regards salsa 31
    Last edited by salsa31; Dec 23rd, 2014 at 05:29 AM.

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: how to check if a txt file is not in the path and create it

    The above code will create a file if it does not exist.

    What do you want to happen when a file does exists?

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to check if a txt file is not in the path and create it

    msgbox or overwrite it or both
    but i want to check to see before if txtdata exist
    if exist then display a msg box or overwrite it

  4. #4
    Addicted Member Wolfgang Enzinger's Avatar
    Join Date
    Apr 2014
    Location
    Munich, Germany
    Posts
    160

    Re: how to check if a txt file is not in the path and create it

    Quote Originally Posted by salsa31 View Post
    but i want to check to see before if txtdata exist
    This is a very common topic, lots of information available on the net:
    https://duckduckgo.com/?q=fileexists+vb5%2F6

    Wolfgang

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to check if a txt file is not in the path and create it

    Tnx WE & Arnoutdv

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: [RESOLVED] how to check if a txt file is not in the path and create it

    Basic FileExists() function
    Code:
    Public Function FileExists(ByVal sFileName As String) As Boolean
      FileExists = (Len(Dir$(sFileName)) > 0)
    End Function

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: [RESOLVED] how to check if a txt file is not in the path and create it

    tnk you for time

  8. #8
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [RESOLVED] how to check if a txt file is not in the path and create it

    Quote Originally Posted by Arnoutdv View Post
    Basic FileExists() function
    Code:
    Public Function FileExists(ByVal sFileName As String) As Boolean
      FileExists = (Len(Dir$(sFileName)) > 0)
    End Function
    Note that the Dir function may return a filename when an empty or null string is passed to it. The GetAttr function is better (and faster) in this regard:

    Code:
    Public Function FileExists(ByRef sFileName As String) As Boolean
        On Error Resume Next
        FileExists = (GetAttr(sFileName) And vbDirectory) <> vbDirectory
        On Error GoTo 0
    End Function
    The Unicode version of the underlying API is even faster (on NT-based OSs):

    Code:
    Private Declare Function GetFileAttributesW Lib "kernel32.dll" (ByVal lpFileName As Long) As Long
    
    Public Function FileExists(ByRef sFileName As String) As Boolean
        Const ERROR_SHARING_VIOLATION = 32&
    
        Select Case (GetFileAttributesW(StrPtr(sFileName)) And vbDirectory) = 0&
            Case True: FileExists = True
            Case Else: FileExists = Err.LastDllError = ERROR_SHARING_VIOLATION
        End Select
    End Function
    Last edited by Bonnie West; Feb 7th, 2016 at 03:50 AM. Reason: Added On Error GoTo 0
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  9. #9

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: [RESOLVED] how to check if a txt file is not in the path and create it

    tnx BW

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