im试图获取具有csv扩展名的文件名,但是当我尝试进行debug.print时,它始终会打印.csv文件和.txt文件
im搜索文件夹和子文件夹中的文件
代码在这里
<div id="box" onclick="rot(this)"></div>
我从网站上获得的代码并添加了Sub LoopAllSubFolders(ByVal folderPath As String)
Dim fileName As String
Dim fullFilePath As String
Dim numFolders As Long
Dim folders() As String
Dim i As Long
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
fileName = Dir(folderPath & "*.*", vbDirectory)
While Len(fileName) <> 0
If Left(fileName, 1) <> "." Then
fullFilePath = folderPath & fileName
If (GetAttr(fullFilePath) And vbDirectory And Not (fullFilePath Like "*csv*")) = vbDirectory Then
ReDim Preserve folders(0 To numFolders) As String
folders(numFolders) = fullFilePath
numFolders = numFolders + 1
Else
'Insert the actions to be performed on each file
'This example will print the full file path to the immediate window
Debug.Print folderPath & fileName
End If
End If
fileName = Dir()
Wend
For i = 0 To numFolders - 1
LoopAllSubFolders folders(i)
Next i
End Sub
也许有人可以更正代码,
谢谢
答案 0 :(得分:0)
您可以使用Dir(folderPath & "*.csv")
在folderPath
中查找所有csv文件。
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
fileName = Dir(folderPath & "*.csv")
While Len(fileName) <> 0
If Left(fileName, 1) <> "." Then
fullFilePath = folderPath & fileName
ReDim Preserve folders(0 To numFolders) As String
folders(numFolders) = fullFilePath
numFolders = numFolders + 1
End If
fileName = Dir()
Wend
FileSystemObject
使用FileSystemObject
获取子文件夹的名称,然后获取每个子文件夹中的所有csv文件
Dim fso as object, mainFolder as object, subFolder as object, file
set fso = createobject("Scripting.FileSystemObject")
if fso.FolderExists("folderPath") Then
set mainFolder = fso.getfolder("folderPath")
else
exit sub
end if
for each subFolder in mainFolder.subfolders
for each file in subFolder.Files
if file.name like "*.csv" then
ReDim Preserve folders(0 To numFolders) As String
folders(numFolders) = file
numFolders = numFolders + 1
end if
next file
next subFolder
答案 1 :(得分:0)
我的建议:
fullFilePath = folderPath & fileName
If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then
ReDim Preserve folders(0 To numFolders) As String
folders(numFolders) = fullFilePath
numFolders = numFolders + 1
Else
If Right(fileName, 4) = ".csv" Then 'new line
'Insert the actions to be performed on each file
'This example will print the full file path to the immediate window
Debug.Print folderPath & fileName
End If