Results 1 to 7 of 7

Thread: getting a file path - help!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    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

  2. #2
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    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.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    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


  5. #5
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    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.
    JC

  6. #6
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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

  7. #7
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Cool

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width