Searching a file in "My Computer" and getting the file's path

  • Hi,


    In one of my VBA projects, I'm in need of a code to search for a given filename with its extension in "My Computer" (all the Computer and drives) and when found passing its full path to a string variable, and when not found assigning empty string to that variable. I have googled my question a lot but most of the codes that I have found were using FileSearch method which is no longer valid in Excel 2010.


    I'm using 64 bit Windows 7 Professional with SP1 and Microsoft Excel 2010 (14.0.7109.5000) SP2 MSO (14.0.7113.5005).


    Any help is highly appreciated.


    Regards,


    Levent

  • Re: Searching a file in "My Computer" and getting the file's path


    I got this far and then got completely stuck, maybe someone can elaborate on this for you....


  • Re: Searching a file in "My Computer" and getting the file's path


    Hi SO,


    I have tried it with a test file, the msgbox says "file found", but do not give the full path.
    I have tested it with a non exiting file, the msgbox still says "file found".
    I have called the function with a Command Button:


    Code
    Private Sub CommandButton1_Click()
        GetDrives
    End Sub


    And assigned the file name like this:


  • Re: Searching a file in "My Computer" and getting the file's path


    To be honest, I've messed around with this so much that I'm not even sure what I'm doing anymore :smash: lol


    I'll try and have a look at it again tomorrow - I know one issue with this code is that it is only going 1 level deep on the subfolders.


    The file found error is something to do with the line:


    Code
    strFile = Dir(strFolder & varItem & "\*.*")


    but I'm far too tired to fix any of this tonight, so I'll get some sleep and re-visit tomorrow hopefully.

  • Re: Searching a file in "My Computer" and getting the file's path


    Thanks for your help my friend...I'll be looking for your review and I also be looking and searching till then...Have a nice sleep

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!