Excel VBA-使用Dir()的多个实例吗? [英] Excel VBA - Using multiple instances of Dir()?

查看:281
本文介绍了Excel VBA-使用Dir()的多个实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用excel将大型文件结构迁移到新文件夹并重新排序许多文件夹。我正在使用Dir()函数循环遍历每个文件夹,并且还在文件之间循环...但是我遇到了一个问题,其中第二个Dir()函数会覆盖第一个函数。有没有办法设置两个Dir()实例?

I am using excel to migrate a large file structure into a new folder and re-order many of the folders. I am using the Dir() function to cycle through every folder, and also cycle through files... but I am running into an issue where the second Dir() function overwrites the first. Is there a way to setup two instances of Dir()?

Sub GetFolders()
    Dim oldFolderPath As String
    Dim folder As String
    Dim copyFolderDir As String
    Dim newFolderDir As String
    Dim strFile As String

    oldFolderPath = "C:\Users\jordanharris\Desktop\PATIENT FILES\A\"

    newFolderDir = "C:\Users\jordanharris\Desktop\PATIENT FILES\A v2\"

    'The goal here is to loop through every file in a folder (without knowing how many or their names)
    folder = Dir(oldFolderPath, vbDirectory) 'First Dir()

    Do While folder <> ""
       If (GetAttr(oldFolderPath & folder) And vbDirectory) = vbDirectory Then

           MkDir newFolderDir & folder & "\APPS-AWARDS\"

           copyFolderDir = oldFolderPath & folder & "\DWSS-EA\"

           'The goal here is to copy every file in the folder 'DWSS-EA' to the new folder 'APPS-AWARDS'
           strFile = Dir(copyFolderDir & "*.*") ' This Dir is overwriting the Dir above
           Do While Len(strFile) > 0 
              Name copyFolderDir & strFile As newFolderDir & folder & "\APPS-AWARDS\" & strFile
              'Get next file using Dir
              strFile = Dir()
           Loop
       End If

        'Get Next Folder using Dir 
        folder = Dir() 'Error on this line because Dir is being overwritten
    Loop

End Sub

如您所见,我正在使用两个Dir实例,这导致此错误,导致我无法转到下一个文件夹。我原本以为我会将Dir的第二个实例放在自己的Sub中,就像这样……

As you can see, I am using two instances of Dir, which is leading to this error where I cannot go to the next folder. I originally thought I would just put the second instance of Dir in its own Sub, like so...

Sub AppsAwards (newFolderDir As String, oldFolderPath As String, folder As String)
    MkDir newFolderDir & folder & "\BENEFITS\APPS-AWARDS\"

    copyFolderDir = oldFolderPath & folder & "\DWSS-EA\"

    strFile = Dir(copyFolderDir & "*.*")

    Do While Len(strFile) > 0
        Name copyFolderDir & strFile As newFolderDir & folder & "\BENEFITS\APPS-AWARDS\" & strFile
        strFile = Dir()
    Loop
End Sub

...并用它代替原始代码...

... and call this in place of the original code ...

...
AppsAwards newFolderDir, oldFolderPath, folder
...

但是它的作用完全相同,Dir的调用子覆盖原始Dir。

But it acts exactly the same, the calling of Dir within the sub overwrites the original Dir.

有没有办法拥有两个Dir()实例?如果没有,是否有解决方法?

Is there a way to have two instances of Dir()? And if not, is there a workaround for this?

编辑(解决方案):

Edit (Solution):

谢谢到面条这是一个很好的解决方法。这就是我在代码中实现的方式。

Thanks to Noodles for a good workaround. This is how I implemented it in my code...

Sub ProcessFolder(FolderPath As String, newFolderPath As String)
    On Error Resume Next
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Set fldr = fso.GetFolder(FolderPath)
    Set fls = fldr.Files

    For Each Thing In fls
        Name FolderPath & Thing.Name As newFolderPath & Thing.Name
    Next

End Sub    

然后我将此行放在我的原始代码中...

And then I placed this line in my original code...

...
ProcessFolder oldFolderPath & folder & "\DWSS-EA\", newFolderDir & folder & "\BENEFITS\APPS-AWARDS\"
...


推荐答案

您可以使用递归来行走一棵树。这是VBScript,可粘贴到VBA中。 PS帮助显示 Visual Basic允许您以两种不同方式处理驱动器,文件夹和文件:通过传统方法(例如Open语句,Write#等),以及通过一组新工具,即File系统对象(FSO)对象模型。

You use recursion to walk a tree. This is VBScript so pastable into VBA. PS The help says Visual Basic allows you to process drives, folders, and files in two different ways: through traditional methods such as the Open statement, Write#, and so forth, and through a new set of tools, the File System Object (FSO) object model.

'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
'Searchterm = Inputbox("Enter search term")
ProcessFolder DirName

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files

    For Each thing in Fls
         msgbox Thing.Name & " " & Thing.path 
    Next


    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub

这篇关于Excel VBA-使用Dir()的多个实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆