【发布时间】:2014-07-07 21:59:18
【问题描述】:
我正在尝试循环通过使用以下 HTML 进行上传(文件夹/文件)的 FTP:
<pre>
<a href="/Example%20Folder/">Example Folder</a>
<a href="/Example%20File.xlsx">Example File.xlsx</a>
<a href="/Example%20Folder/Example%20File%20In%20Folder.xlsx">Example File In Folder.xlsx</a>
</pre>
我的代码尝试遍历站点上的所有文件夹(如果存在)并下载每个文件。问题是进入然后导航 back 到根目录后,我收到错误 70“Permission Denied”。相关代码如下:
Dim fso As New FileSystemObject
Dim oFolder, oSubfolder, oFile, bButton, queue As Collection
Dim oFileName As String
Dim processed As Boolean
Dim processedList As String
Dim toPath As String
Dim fromPath As String
Dim HWNDsrc As Long
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add IE.Document.getElementsByTagName("a")
Do While queue.Count > 0
Set oFolder = queue(1)
Set bButton = Nothing
queue.Remove 1
If Right(oFolder, 1) = "/" Then 'Check if the link is a folder ***ERROR HERE
IE.Navigate oFolder
Do While IE.Busy: DoEvents: Loop
Do Until IE.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop 'readystate=4
Set bButton = IE.Document.getElementById("goParent") 'Back button in browser
Set oFolder = IE.Document.getElementsByTagName("a")
End If
For Each oSubfolder In oFolder
If Right(oSubfolder, 1) = "/" Then
queue.Add oSubfolder
End If
Next oSubfolder
For Each oFile in oFolder
If InStr(oFile, ".") > 0 Then 'Check if link is file
oFileName = Replace(Right(oFile, Len(oFile) - InStrRev(oFile, "/")), "%20", " ")
fromPath = DOWNLOADS_FOLDER & oFileName 'downloads_folder defined earlier
toPath = DESTINATION_FOLDER & oFileName 'destination_folder defined earlier
With IE
.Visible = True
.Navigate oFile
End With
HWNDsrc = IE.HWND
SetForegroundWindow HWNDsrc
Sleep (1500)
Application.SendKeys ("%s") 'Used because URLDownloadToFile can't handle FTP
Do While IE.Busy: DoEvents: Loop
Do Until IE.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
processed = True
processedList = processedList & vbCrLf & _
oFileName
If Len(Dir(toPath)) = 0 Then
fso.MoveFile fromPath, toPath
End If
End If
Next oFile
If Not bButton Is Nothing Then 'If in subfolder, return to main directory
With IE
bButton.Click
Do While .Busy: DoEvents: Loop
Do Until .ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
End With
End If
Loop
执行 bButton 代码块后,无法打印 oFolder 并返回“Permission denied”错误。有没有办法解决这个问题?
【问题讨论】:
-
尝试捕获主目录的 url 并直接导航,而不是单击后退按钮?
-
@DavidZemens 感谢您的建议。不幸的是,没有骰子。似乎每次刷新 IE 页面时都会重置队列对象。尝试将每个文件夹链接存储在一个数组中并分别导航到每个页面,而不是循环遍历 HTML 元素。
-
将它们存储在字典而不是数组中。它们已经过优化,您无需保留
ReDim来调整它们的大小等:)
标签: internet-explorer excel ftp permission-denied vba