|
-
Aug 29th, 2000, 09:36 PM
#1
How would I test to see if a file exists before I try to open it??
-
Aug 29th, 2000, 09:45 PM
#2
Junior Member
Code:
If Dir("C:\Windows\file.txt", vbHidden) = "" Then
Open "C:\Windows\file.txt" For Output As #1
print #1, variable
Close #1
Else
Open "C:\Windows\file.txt" For Input As #1
Line Input #1, variable
Close #1
End If
[Edited by sp007 on 08-29-2000 at 10:48 PM]
-
Aug 29th, 2000, 09:48 PM
#3
Lively Member
-
Aug 29th, 2000, 10:44 PM
#4
An easier why is to use the filesystemobject
you'll have to reference the Microsoft Scripting Runtime thing first
Private Sub cmdCheckForFile_Click()
Dim fs
Set fs = New Scripting.FileSystemObject
Exists = fs.FileExists(fs.BuildPath(App.Path, "YourFileHere.txt"))
'Exists is now a boolean variable with the results T/F of
'if the file exists or not
MsgBox Exists
End sub
-
Aug 30th, 2000, 07:03 PM
#5
not working
This method is returning "false" whether the file is there or not...any thoughts??
-
Aug 30th, 2000, 07:11 PM
#6
Try this:
Code:
If Dir("C:\file.txt") <> "" Then
Open "C:\file.txt" For Input As #1
Text1.Text = Input$(LOF(1), 1)
Close #1
Else
Msgbox "File does not exist!", vbCritical
End If
-
Aug 30th, 2000, 07:52 PM
#7
Fanatic Member
Re: not working
Originally posted by crptcblade
This method is returning "false" whether the file is there or not...any thoughts??
Be careful with the code above that you used. you'll notice that it had app.path etc so the position of the file was preset (which is likely why you never found a file you knew was there).
try it like this
Code:
Function ReportFileStatus(filespec)
Dim fso, msg
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
msg = filespec & " exists."
Else
msg = filespec & " doesn't exist."
End If
ReportFileStatus = msg
End Function
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Aug 30th, 2000, 08:15 PM
#8
Member
Reusable Function from Books Online -- VB5 SP4
Function FileExists (p As String) As Long
If Dir (p) <> " " Then
FileExists = conSuccess ' Return a constant
' indicating the
Else ' file exists.
FileExists = conFailure ' Return failure
' constant.
End If
End Function
Dim ResultValue As Long
ResultValue = FileExists ("C:\Testfile.txt")
If ResultValue = conFailure Then
.
. ' Handle the error.
.
Else
.
. ' Proceed with the program.
.
End If
-
Aug 30th, 2000, 08:19 PM
#9
Member
Even Smaller
Function FileExists (filename) As Boolean
FileExists = (Dir(filename) <> "")
End Function
'Returns true if file exists, false if it doesn't
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
|