Results 1 to 4 of 4
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Overwriting files recursively (VBA Word 2000)

    I've toyed with some functions I found, but I guess I don't understand it enough to do what I want.

    1. I want to search in a given directoryA and all its subdirectories for one of 7 files.

    2. When found, I want to replace this file with a file from a specific directoryB. This specific directoryB will contain the replacement files for all 7 files.

    I tried using an FSO procedure but could not figure out how to find the path of the files I found. I've included what I have and where I got stuck (with some extraneous variables) below:

    <pre>Public Sub ChangeAllFiletoRemoveContextMenuETC()

    Dim myFile As String
    Dim PathToUse As String
    Dim myDoc As Document
    Dim Response As Long
    Dim I As Long

    Dim objFSO As New FileSystemObject
    Dim fldFolder As Folder
    Dim fldSubFolder As Folder
    Dim strFileName As String

    Dim strFilePath As String

    PathToUse = "C:Test"

    'Error handler to handle error generated whenever
    'the FindReplace dialog is closed

    On Error Resume Next

    'Set the directory and type of file to batch process

    With Application.FileSearch
    .NewSearch
    .LookIn = PathToUse
    .SearchSubFolders = True
    .FileName = "*.*"
    .MatchTextExactly = True
    .FileType = msoFileTypeAllFiles

    If .Execute() Then

    For I = 1 To .FoundFiles.Count

    If .FoundFiles(I) = "whibody.htm" Or _
    .FoundFiles(I) = "whfbody.htm" Or _
    .FoundFiles(I) = "whtdhtml.htm" Or _
    .FoundFiles(I) = "whiform.htm" Or _
    .FoundFiles(I) = "whfform.htm" Or _
    .FoundFiles(I) = "whd_tabs.htm" Or _
    .FoundFiles(I) = "whmozemu.js" Then

    'TROUBLE: Need to find the path of the found file so I can _
    overwrite it with a file from a fixed directory.

    'Then I need to copy a file from a fixed location into that folder, _
    overwriting the file without any prompts.

    End If

    Next I

    End If

    End With

    End Sub</pre>


    Feel free to start from scratch with something else!!

    Thanks for your help!!
    Troy

  2. #2
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Eugene, Oregon, USA
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwriting files recursively (VBA Word 2000)

    Using the FSO requires setting a reference to "Microsoft Scripting Runtime".

    After setting the reference, does the following code do what you want?

    'Module-Level Variables/Constants
    Private fso As Scripting.FileSystemObject
    'Change the following constants to the appropriate folder names
    Const cstrMainFolderNameWithOlderFiles As String = "C:Temp"
    Const cstrFolderNameWithUpdatedFiles As String = "I:temp"

    'This sub gets the whole process started...
    Sub Main()
    Dim myFolder As Scripting.Folder
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set myFolder = fso.GetFolder(cstrMainFolderNameWithOlderFiles)
    CopyAndReplaceFiles myFolder
    End Sub

    'This sub does all the work
    Private Sub CopyAndReplaceFiles(oFolder As Scripting.Folder)
    Dim oSubFolder As Scripting.Folder
    Dim oFile As Scripting.File
    Dim strFilesToFind(6) As String
    Dim i As Integer

    'Populate the array with the names of the files you're looking for
    strFilesToFind(0) = "whibody.htm"
    strFilesToFind(1) = "whfbody.htm"
    strFilesToFind(2) = "whtdhtml.htm"
    strFilesToFind(3) = "whiform.htm"
    strFilesToFind(4) = "whfform.htm"
    strFilesToFind(5) = "whd_tabs.htm"
    strFilesToFind(6) = "whmozemu.js"


    'For each subfolder in the main folder, run this sub recursively
    If oFolder.SubFolders.Count Then
    For Each oSubFolder In oFolder.SubFolders
    CopyAndReplaceFiles oSubFolder
    Next 'oSubFolder
    End If

    'scan each file in the folder and if it's one of the old files, replace it
    For Each oFile In oFolder.Files
    For i = 0 To 6
    If LCase$(oFile.Name) = strFilesToFind(i) Then
    fso.CopyFile Source:=cstrFolderNameWithUpdatedFiles & "" & oFile.Name, _
    Destination:=oFile.Path, OverwriteFiles:=True
    Exit For
    End If
    Next 'i
    Next 'oFile

    End Sub

  3. #3
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwriting files recursively (VBA Word 2000)

    That works great!!

    One question: why two subs instead of one?

    Troy

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Eugene, Oregon, USA
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwriting files recursively (VBA Word 2000)

    The second sub is recursive--it repeatedly calls itself to ensure that all the subfolders in the main folder and all the subfolders of each subfolder are taken care of. The main folder can't be defined or referenced in this recursive processing or the recursive behavior would never have an ending point. That is, the sub would reference the main folder which would contain a subfolder which would recursively call the sub which would reference the main folder which would contain a subfolder and so on.

    Hope that makes sense! this isn't always clear to me either...

Posting Permissions

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