-
Dec 23rd, 2014, 05:25 AM
#1
Thread Starter
Enjoy the moment
[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.
-
Dec 23rd, 2014, 06:04 AM
#2
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?
-
Dec 23rd, 2014, 06:10 AM
#3
Thread Starter
Enjoy the moment
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
-
Dec 23rd, 2014, 06:26 AM
#4
Re: how to check if a txt file is not in the path and create it
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
-
Dec 23rd, 2014, 06:29 AM
#5
Thread Starter
Enjoy the moment
Re: how to check if a txt file is not in the path and create it
-
Dec 23rd, 2014, 08:16 AM
#6
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
-
Dec 23rd, 2014, 08:20 AM
#7
Thread Starter
Enjoy the moment
Re: [RESOLVED] how to check if a txt file is not in the path and create it
tnk you for time
-
Dec 23rd, 2014, 08:10 PM
#8
Re: [RESOLVED] how to check if a txt file is not in the path and create it
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
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)
-
Dec 24th, 2014, 12:16 AM
#9
Thread Starter
Enjoy the moment
Re: [RESOLVED] how to check if a txt file is not in the path and create it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|