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!

Check if file exists?

Status
Not open for further replies.

MrPeds

Programmer
Jan 7, 2003
219
0
0
GB
Hi,

I would like to write a function that returns if a file exists on a computer and checks all the hard drive partitions to see if it is there, and returns where it is.

I know that the file will always be in the windows folder e.g. C:\Windows\theFile.txt or
D:\Windows\theFile.txt etc etc etc

ANy suggestions on how to do this??

Thanks,

MrPeds
 
Maybe there's also an "intrinsic" VB function that can retrieve the windows directory, but if you can't find that you can always use the API GetWindowsDirectory to retrieve it. Append your filename and you've got your full path and filename, which for instance you can supply to the FileLen function (wrapped in an error handler of course) and check its return value.
Greetings,
Rick
 
Here's a function to check for a file's existence.

Public Function FileExists(FileName As String) As Boolean
Dim intFileNum As Integer
intFileNum = FreeFile
On Error GoTo NoSuchFile:
Open FileName For Input As #intFileNum
Close intFileNum
FileExists = True
Exit Function
NoSuchFile:
FileExists = False
End Function

Like LazyMe says, you can retrieve the windows directory with the API GetWindowsDirectory.
I have a question though: Why would you want to check all of your harddrives if you allready know it will be in the windowsdir? [surprise]

Hope this one helps you out.
 

Here is just 1 thread222-358873 from doing a search on this site with using all words, just my threads, and any date using the keywords "search hard drive". There are many examples contained within this site on how to search a hardrive with controls, API, regular expressions, recursive intrinsic Dir function, and the file system object.

Good Luck

 
in line with Merlijn function its also quite handy to have a isFileWritable function as well.

Just open the file for append if the file isn't writable the an error will be raised and so the function returns false.

There is also some way to get environment variables in VB (cant remember what it is though) but the environment variable that you want is systemroot
 
Thanks for everybodies help!!

It's exactly what I need!

It is my inexperience with VB that I didn't know how to go about finding the Windows folder.

MrPeds
 
Here is a function that I created for this:


Public Function Already_Running(AppName As String) As Boolean
Dim FileName As String
Dim dirname As String
Dim dirskip As Integer
Dim newfile As String
Dim icount As Integer

newfile = ""

'Find newest file
dirname = Dir("O:Reports\Auto_Rip\", vbDirectory)

For icount = 1 To dirskip + 2
FileName = Dir
Next

Do Until FileName = "" Or UCase(newfile) = UCase(AppName)
If UCase(FileName) = UCase(AppName) Then
newfile = FileName
End If
FileName = Dir()
Loop


If newfile <> &quot;&quot; Then
Already_Running = True
Else
Already_Running = False
End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top