+ Reply to Thread
Results 1 to 2 of 2

Search for a file in subfolders

Hybrid View

Roger Roth Search for a file in... 06-15-2015, 01:59 PM
mrice Re: Search for a file in... 06-15-2015, 03:28 PM
  1. #1
    Registered User
    Join Date
    03-08-2013
    Location
    us
    MS-Off Ver
    2007
    Posts
    2

    Search for a file in subfolders

    I am trying to search all subfolders for a file to eliminate possible duplicate file names. Any help would be appreciated.

    [code]
    Sub UPDATE()
    Application.EnableCancelKey = xlInterrupt
    On Error GoTo ENDTHISSUB
    RNC = 2
    Sheets("G-DRIVE").Select
    Do While Cells(RNC, 2) <> ""
    GoSub MOVEGDRIVE
    RNC = RNC + 1
    MSGC = MsgBox("CONTINUE?", vbYesNo)
    If MSGC = vbNo Then Exit Do
    Loop
    GoTo ENDTHISSUB
    MOVEGDRIVE:
    MSGF = vbYes
    GoSub MOVEFILE
    If MSGF = vbNo Then Return
    Cells(RNC, 3).Select
    Range(ActiveCell, ActiveCell.Offset(0, 1)).Select
    Selection.Copy
    Sheets("INVAULT").Select
    RNP = Cells(1, 5)
    Cells(RNP, 2).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    RNP = RNP + 1
    Cells(1, 5) = RNP
    Sheets("G-DRIVE").Select
    Range(Cells(RNC, 1), ActiveCell.Offset(0, 24)).Select
    Selection.Delete Shift:=xlUp
    RNC = RNC - 1
    Return
    MOVEFILE:
    FCS$ = Cells(RNC, 15) & Cells(RNC, 17)
    FPS$ = Cells(RNC, 16) & Cells(RNC, 17)
    FTS$ = "C\USERS\" & Cells(1, 19) & "\DOCUMENTS\VAULT\PROJECTS\*" & Cells(RNC, 17) & "/S"
    Do While Dir(FTS$) <> ""
    MSGF = MsgBox("filename " + FPS$ + " exists.Rename?", vbYesNo)
    If MSGF = vbYes Then FPS$ = FPS$ + "_1"
    If MSGF = vbYes Then FTS$ = FTS$ + "_1"
    If MSGF = vbNo Then MSG = MsgBox("Copy failed. Try again?", vbYesNo)
    If MSGF = vbNo Then If MSG = vbNo Then MsgBox ("File Skipped")
    If MSGF = vbNo Then If MSG = vbNo Then Return
    Loop
    FileCopy FCS$, FPS$
    If Dir(FPS$) <> "" Then
    MSGD = MsgBox("COPY SUCCESS! ERASE " + FCS$ + "?", vbYesNo)
    If MSGD = vbYes Then Kill (FCS$)
    End If
    Return
    ENDTHISSUB:
    End Sub

    [code]

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Search for a file in subfolders

    Maybe this will help

    'This macro uses the technique of recursion to examine the contents of a folder structure no matter how complex it is.
    
    Sub Extract()
    Dim Filename As Variant
    ' Set up the empty spreadsheet
    Cells.Clear
    Cells(1, 1) = "Folder"
    Cells(1, 2) = "File"
    Cells(1, 3) = "Size"
    Cells(1, 4) = "Name"
    
    'Use the GetOpenFilename function to get a full path description of a typical file
    Filename = Application.GetOpenFilename()
    If Filename = False Then Exit Sub
    'The GetSubDirectories subroutine is called recursively using the name of the parent folder as the single argument.
    Call GetSubDirectories(Left(Filename, Len(Filename) - Len(Dir(Filename)) - 1))
    
    
    Columns.AutoFit
    End Sub
    
    Sub GetSubDirectories(folderspec)
    Dim fs As Object, f As Object, s As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Call GetFiles(f.Path)
    For Each SubFolder In f.subfolders
        GetSubDirectories (f.Path & "\" & SubFolder.Name) ' This is a recursive call
    Next SubFolder
    End Sub
    
    Sub GetFiles(folderspec)
    Dim fs As Object, f As Object, s As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    
    For Each File In f.Files
        On Error Resume Next
        Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = folderspec ' next available cell in column 1
        Cells(Rows.Count, 1).End(xlUp).Offset(0, 1).Value = folderspec & "\" & File.Name
        Cells(Rows.Count, 1).End(xlUp).Offset(0, 2).Value = File.Size
        Cells(Rows.Count, 1).End(xlUp).Offset(0, 3).Value = File.Name
        On Error GoTo 0
    Next File
    End Sub
    When prompted, select a file in your top level folder - if there isn't one then create one as an anchor.
    Martin

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 04-21-2014, 04:03 PM
  2. how to search a file in folder and subfolders
    By rakeshredround in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-05-2012, 03:35 AM
  3. Macro to search folder including subfolders for file and open
    By kiraexiled in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 02:45 PM
  4. Replies: 2
    Last Post: 03-26-2012, 07:12 PM
  5. file search in subfolders
    By Pflugs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2005, 12:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1