[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
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?
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
Re: how to check if a txt file is not in the path and create it
Quote:
Originally Posted by
salsa31
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
Re: how to check if a txt file is not in the path and create it
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
Re: [RESOLVED] how to check if a txt file is not in the path and create it
Re: [RESOLVED] how to check if a txt file is not in the path and create it
Quote:
Originally Posted by
Arnoutdv
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
Re: [RESOLVED] how to check if a txt file is not in the path and create it