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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search if a file exist

Status
Not open for further replies.

devinci

MIS
Feb 15, 2002
46
CA
I'm trying to code this a function....

I have the name of the file, just the name not the path of the file...I need to search starting from the c:\ and searching all is directories to see if the file exists...If the file exists, I want to know the full path. If the file doesn't exist, I want to create it.
 
Following is a recursive procedure which will search all directories and all drives thru the last drive to search.

You would call it as follows:

ExcelPath = FindFile("EXCEL.EXE", "E")

This will search the C, D, and E drives for excel.exe



Public Function FindFile(rStr_FileName As String, rStr_LastDrive As String) As String

Dim pStr_FullPath As String
Dim pInt_DriveLetter As Integer
Dim pBol_FindFile As Boolean

pBol_FindFile = True
pInt_DriveLetter = 66
While (pBol_FindFile)
pInt_DriveLetter = pInt_DriveLetter + 1
pStr_FullPath = FindTheFile(Chr(pInt_DriveLetter) & ":", UCase(rStr_FileName))
If (Len(pStr_FullPath) > 0) Then
pBol_FindFile = False
Else
If (UCase(Chr(pInt_DriveLetter)) = rStr_LastDrive) Then
pBol_FindFile = False
End If
End If
Wend

FindFile = pStr_FullPath

End Function

'-------------------------------------------------------

Public Function FindTheFile(rStr_Parent As String, rStr_FileName As String) As String

Dim pStr_CurrentDir As String
Dim pStr_CheckFile As String
Dim pInt_Idx As Integer
Dim pBol_FileFound As Boolean
Dim pInt_FileType As Integer
Dim pCol_SubDirs As New Collection

If (Right(rStr_Parent, 1) <> &quot;\&quot;) Then
rStr_Parent = rStr_Parent & &quot;\&quot;
End If

pStr_CheckFile = Dir(rStr_Parent, vbDirectory)
pBol_FileFound = False
pStr_CurrentDir = &quot;&quot;

While (Len(pStr_CheckFile) > 0)
pStr_CheckFile = UCase(pStr_CheckFile)
If (pStr_CheckFile = &quot;PAGEFILE.SYS&quot;) Then
pStr_CheckFile = Dir
Else
If (Left(pStr_CheckFile, 1) = &quot;.&quot;) Then
pStr_CheckFile = Dir
Else
pInt_FileType = GetAttr(rStr_Parent & pStr_CheckFile) And vbDirectory
If (pInt_FileType = vbDirectory) Then
pCol_SubDirs.Add (rStr_Parent & pStr_CheckFile)
pStr_CheckFile = Dir
Else
If (pStr_CheckFile = rStr_FileName) Then
pBol_FileFound = True
pStr_CurrentDir = rStr_Parent
pStr_CheckFile = &quot;&quot;
Else
pStr_CheckFile = Dir
End If
End If
End If
End If
Wend

If (pBol_FileFound = False) Then
pInt_Idx = 1
While (pInt_Idx <= pCol_SubDirs.Count)
pStr_CurrentDir = FindTheFile(pCol_SubDirs.Item(pInt_Idx), rStr_FileName)
If (Len(pStr_CurrentDir) < 1) Then
pInt_Idx = pInt_Idx + 1
Else
pInt_Idx = pCol_SubDirs.Count + 1
End If
Wend
End If

pStr_ReferMsg = pStr_CurrentDir
FindTheFile = pStr_CurrentDir

End Function

'=========================================================

Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
Of course, if I were to write this today, I would most likely use the FileSystemObject
Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
I have to go with Cajun on this one. Use the FileSystemObject. The example below checks to see if the file &quot;C:\Temp\SomeFile.txt&quot; exists or not.

-------------------------
Option Explicit

Private FSO as Object
Private Const MyFile = &quot;C:\Temp\SomeFile.txt&quot;

Private Sub Form_Load()
'------------------------------
Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

If FSO.FileExists(MyFile) Then
'..............................
MsgBox &quot;The File 'SomeFile.txt' EXISTS!&quot;
Else
MsgBox &quot;The File 'SomeFile.txt' DOES NOT EXIST!&quot;
'..............................
EndIf
'------------------------------
End Sub

Piece of Cake! Thanks!
LeGo PiEcE

&quot;The Computer Spock! Destroy it!&quot; - Captain James T. Kirk
 
I think you would find, if you think about it for a bit that using Dir in the previous code would be a bit shorter. Nicer cake. And I assume a shorter executable.

Option Explicit

Private Const MyFile = &quot;C:\Temp\SomeFile.txt&quot;

Private Sub Form_Load()
'------------------------------

If len(dir(MyFile )) > 0
Then
'..............................
MsgBox &quot;The File 'SomeFile.txt' EXISTS!&quot;
Else
MsgBox &quot;The File 'SomeFile.txt' DOES NOT EXIST!&quot;
'..............................
EndIf
'------------------------------
End Sub Peter Meachem
peter@accuflight.com
 
Lego and Peter, I think you might have missed a crucial part of the post - the directory is NOT known, only the filename. The key is to find which directory the file is in, if any

Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
I know, I was just pointing out that using Dir can mean shorter code. Lego's code with fso looks much easier than yours with dir, but misses 98% of the work out. Peter Meachem
peter@accuflight.com
 
Thank you very much Cajun...it works like a charm.
 
Whoops... should've read the question more closely instead of jumping to conclusions, like I always do! Thanks!
LeGo PiEcE

&quot;The Computer Spock! Destroy it!&quot; - Captain James T. Kirk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top