-
i realize this is probabily an easy question but here goes:
how do i get the file path of a file?
i know the file i need (test.txt) is in a subdirectory under
c:\windows\temporary internet files\
i need to get the full path of the file, how do i make vb search for it?
Here's why:
my program has just opened microsoft internet explorer and internet explorer saved the file in the temporay internet files (like it always does when you look at a webpage).
the problem is there are hidden folders in the temporay internet files (you can't unhide them and they all have random names like h6xs4h8bm3) it is complicated, but i need to search search the temporarty internet files.
i need to copy the file from the temporary internet files into a different folder.
thanks in advance
-
If you want to keep the path hard-coded like that then you can simply use the Dir function, as follows:
Code:
Dim sDir As String
Dim sFile As String
sDir = "c:\windows\temporary internet files\"
sFile = Dir(sDir & "test.txt", vbNormal Or vbHidden Or vbReadOnly Or vbSystem)
If sFile = vbNullString Then
Call MsgBox("File not found!")
Else
Call MsgBox(sFile)
End If
Like I said, this will ensure that the file exists in the directory you named. If you want to know how to find the REAL Temporary Internet directory through the Registry, so that your code works on any computer, just ask me and I'll show you (it is a little complicated though, if you don't know how to use API calls).
Hope this does it for you though, later.
-
Mmmmmm... I think I understand what you are wanting but the solution isn't straight forward.
It involves the use of the "Dir" function but also the use of recursion.
It sounds like you will always have a BASE directory to start looking in (ie c:\windows\temporary internet files\) but there will be subdirectories under it and you want to find the ACTUAL subdirectory the filename in question belongs to.
What you need to do is start by looking at the contents of the directory you know and if the file isn't found then you need to repeat the proceedure for each directory you discover beneath that one.
Its a start... if I had time to code it out I would :)
-
thanks for your help but i still have one problem.
that code only searches the temporary internet files folder. the actual file is stored in a directory inside the temporary internet files folder but the search doesn't go deep enough. if there isn't a way to go deeper could you tell me how to use the api. thanks
-
Check out the example project and code at
http://www.vb-world.net/tips/tip59.html
It uses an API call to search for a specified file and will
do a recursive search through a drives directories. You should be able to adapt it for your use.
-
I think you need to read my reply again....
Like I said you need to do "recursion".
What this means is that for each directory it encounters, if it finds another directory it goes inside that one and looks at the files in there... if that finds another directory it goes inside again.
I will give you the pseudo code for it and you can work it out yourself :
Code:
Path = GetPathOf("c:\windows\temporary internet files", "test.txt")
Public Function GetPathOf(TopDirectory, FileName)
NextFile = Dir(TopDirectory & "\" & FileName)
Do While NextFile <> ""
If NextFile <> "." Or ".." Then ' This is the current dir
If IsADirectory(NextFile) Then
sTemp = GetPathOf(TopDirectory & "\" & NextFile,FileName)
If sTemp <> "" Then
GetPathOf = sTemp
Exit Function
End If
Else
If FileName Tacked onto end of sNextFile Then
GetPathOf = everything up to but not including FileName
Exit Sub
End If
End If
End If
NextFile = Dir
Loop
End Function
Its a bit hard to follow (but then again recursion is) but hopefully you will see that it will recurse into subfolders until it finds the filename you are after and will return the portion of the path back up through the recursed function until it pops out the top
-
Cool,
That's the first time I've seen recursion used in something useful AND appropriate !
Usually you just see it used as explanation examples of factoring etc where non-recursive functions would have been adequate.
Dim Gen-X as VBLegend :D