gpt4 book ai didi

vba - Excel VBA - 使用 Dir() 的多个实例?

转载 作者:行者123 更新时间:2023-12-01 09:56:56 24 4
gpt4 key购买 nike

我正在使用 excel 将一个大文件结构迁移到一个新文件夹中并重新排序许多文件夹。我正在使用 Dir() 函数循环浏览每个文件夹,并循环浏览文件......但我遇到了第二个 Dir() 函数覆盖第一个的问题。有没有办法设置两个 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 中,就像这样......
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

...并调用它来代替原始代码...
...
AppsAwards newFolderDir, oldFolderPath, folder
...

但它的行为完全相同,在 sub 中调用 Dir 会覆盖原始 Dir。

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

编辑(解决方案):

感谢 Noodles 提供了一个很好的解决方法。这就是我在代码中实现它的方式......
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

然后我把这一行放在我的原始代码中......
...
ProcessFolder oldFolderPath & folder & "\DWSS-EA\", newFolderDir & folder & "\BENEFITS\APPS-AWARDS\"
...

最佳答案

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

'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

关于vba - Excel VBA - 使用 Dir() 的多个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41068315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com