Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking to see if a .dat file is present on c: 1

Status
Not open for further replies.

DeepBlerg

Technical User
Jan 13, 2001
224
0
0
AU
hi,

In an IF statement how would i check that "c:\test.dat" exists on drive c:

thanks.
 
Hi,

If Dir(&quot;c:\test.dat&quot;)<>&quot;&quot; then
'the file exist

end if

Sunaj
 
Use the FileSystemObject. Set a reference to the Microsoft Scripting Runtime and look at the example:

Code:
Dim oFileSystem As Scripting.FileSystemObject
Set oFileSystem = New Scripting.FileSystemObject

If oFileSystem.FileExists(&quot;c:\test.dat&quot;) Then
   'action
End If

Set oFileSystem = Nothing

Or you can use the FindFirstFile API. For code, look here:


Good luck!

Jordi Reineman
 
Hi JJR

Why do you prefer the FileSystemObject method instead of the simpler-to-use dir function?
Is there an advantage of using the FileSystemObject if all you what to do is to test if the file is there?

Sunaj
 
use VB function &quot;Dir&quot;

bye bye
 
Unless you need all of the scripting functionality that the FileSystemObject offers, it will be faster and use less overhead to simply use the Dir command. I would change one thing however . . . user the Dir$ (add the $) to force the function to return a string rather than a variant. This will allow it to perform better and is a good habit to get into in general. - Jeff Marler B-)
 
Hi Sunaj,

I don't prefer the FileSystemObject, but I've had some problems with the Dir function because it can't handle UNC paths (as far as I know). Now that won't be a problem with c:\test.dat (and I have to admit I didn't think of the Dir function right a way), but in a network environment the Dir function can be cause for wrong conclusions.

By the way, does anyone know how to really check if a file (or a folder) exists? Because the filesystemobject will also return false if you just don't have anough rights to access a drive/folder (I have to use UNC paths).

Jordi Reineman
 
Hi JJR,

I use dir with UNC paths and I've never had any problems... (with that!)

As far as I can tell, dir behaves the same as the FileSystemObject when dealing with folder that the user does not have access to (returns empty).

cheers,
Sunaj

ps. jmarler is of course right with the dir$
 
Open &quot;c:\test.dat&quot; For Append As #1

If LOF(1) > 1 Then
Msgbox (&quot;File Exists&quot;)
end if

Regards,

Andrew

 
Fergusona,

What benefit do you see with your method when compared with the Dir$ command or the File Scripting Object's Exists method?
- Jeff Marler B-)
 
Jeff,

Hi, I don't know what benefit if any my method has compared with the Dir$ command or the file scripting object exists method to be honest. I am quite new to VB, did more stuff on Quick Basic years ago. I haven't used those commands you mentioned so they probably are a better/more efficient way of checking.

Regards,

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top