vba中dir用法,Excel VBA-使用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

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

...

But it acts exactly the same, the calling of Dir within the sub overwrites the original 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\"

...

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值