excel vba dir函数用于查找文档而不是docx
我在excel中使用此vba代码从特定文件夹获取文件名:excel vba dir函数用于查找文档而不是docx,vba,excel,Vba,Excel,我在excel中使用此vba代码从特定文件夹获取文件名: Sub getfilelistfromfolder() Dim varDirectory As Variant Dim flag As Boolean Dim i As Integer Dim strDirectory As String strDirectory = Application.ActiveWorkbook.Path & "\" i = 1 flag = True varDirectory = Dir("C:\
Sub getfilelistfromfolder()
Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String
strDirectory = Application.ActiveWorkbook.Path & "\"
i = 1
flag = True
varDirectory = Dir("C:\Users\USER\Documents\*.doc", vbNormal)
Range("A2:A100").Select
Selection.ClearContents
While flag = True
If varDirectory = "" Then
flag = False
Else
Cells(i + 2, 1) = varDirectory
varDirectory = Dir
i = i + 1
End If
Wend
End Sub
但是,我只想获取.doc而不是.docx。
如何修改此代码以仅获取.doc文件 注:这是我举的一个例子。我的实际文件扩展名是特定于程序的文件类型:.out和.outreview 我只想获取.out文件和而不是.outreview文件的列表。 文件名示例:
file1.out
file2.outreview
file3.out
文件4.outreview
我只想列出.out文件
谢谢看起来dir函数会默默地忽略超过三个字符的扩展名,并将它们视为三个字符(它使用8.3字符的DOS名称-可能使用了一些旧的Windows API),就像DOS dir命令一样,在命令提示符下尝试使用
dir/X*.doc
解决方法可以是测试扩展,类似这样的方法应该可以:
IF (UCase(Right(varDirectory, 3)) = "DOC")
dir函数似乎会默默地忽略长度超过三个字符的扩展名,并将它们视为三个字符(它使用8.3字符的DOS名称-可能它使用的是一些较旧的Windows API),就像DOS dir命令一样,在命令提示符下尝试
dir/X*.doc
解决方法可以是测试扩展,类似这样的方法应该可以:
IF (UCase(Right(varDirectory, 3)) = "DOC")
只需测试所需的扩展:
Sub getfilelistfromfolder()
Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Long
Dim strDirectory As String
Dim Desired As String
Desired = ".doc"
i = 1
flag = True
Range("A2:A100").Select
Selection.ClearContents
While flag = True
If varDirectory = "" Then
flag = False
Else
If Right(varDirectory, 4) = Desired Then
Cells(i + 2, 1) = varDirectory
i = i + 1
End If
varDirectory = Dir
End If
Wend
End Sub
只需测试所需的扩展:
Sub getfilelistfromfolder()
Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Long
Dim strDirectory As String
Dim Desired As String
Desired = ".doc"
i = 1
flag = True
Range("A2:A100").Select
Selection.ClearContents
While flag = True
If varDirectory = "" Then
flag = False
Else
If Right(varDirectory, 4) = Desired Then
Cells(i + 2, 1) = varDirectory
i = i + 1
End If
varDirectory = Dir
End If
Wend
End Sub
如果您选中对“Microsoft脚本运行时”(菜单Extras->References)的引用,则可以使用FileSystemObject:
dim fso as FileSystemObject
dim folder as Scripting.Folder
dim file as Scripting.File
dim extension as String
set fso = new FileSystemObject
set folder = fso.getFolder("C:\Users\USER\Documents\")
extension = ".doc"
for each file in folder.Files
if Right(file.shortname, 4) = extension then
' do stuff
end if
next file
请注意,
file.Type
-属性会产生类似“Word文档”的表达式,在不同的电脑和语言上可能会有所不同。如果您检查对“Microsoft脚本运行时”的引用(菜单附加->引用),您可以使用FileSystemObject:
dim fso as FileSystemObject
dim folder as Scripting.Folder
dim file as Scripting.File
dim extension as String
set fso = new FileSystemObject
set folder = fso.getFolder("C:\Users\USER\Documents\")
extension = ".doc"
for each file in folder.Files
if Right(file.shortname, 4) = extension then
' do stuff
end if
next file
请注意,
file.Type
-属性会产生类似“Word文档”的表达式,在不同的PC机和语言上可能会有所不同。您可以将代码的相应行替换为搜索文件扩展名,仅使用“.Doc”;使用以下行/行
If UCase(Right(Trim(varDirectory), 4)) = ".DOC" Then
'Your Code here
End If
您可以将搜索文件扩展名的代码的相应行替换为仅“.Doc”;使用以下行/行
If UCase(Right(Trim(varDirectory), 4)) = ".DOC" Then
'Your Code here
End If
好的,如果您遵循相同的
*.out
而不是*.out*
或*.*
,那么您应该只获取out文件。但是,如果看不到真实的代码和一些文件名,就很难得到正确的答案。@PaulFrancis:添加了示例文件名。否则,我认为我的问题很清楚。我从来没有说过你的问题不清楚。有时,您遵循的逻辑实际上可能不是您的代码指示编译器执行的操作。因此,最好提供实际的代码。您提供的伪代码将只提供.doc而不是。docx@PaulFrancis:代码与我使用的完全相同。我在示例代码中加入了.doc,在我自己的代码中加入了.out。我替换它的原因是为了网站的SEO目的,这样将来的用户就可以很容易地找到这个问题。如果我在标题中加入了.out和.outreview,就不会有人输入这些未知的文件扩展名。这些扩展只在我们的私有软件中使用。好吧,如果您使用相同的*.out
而不是*.out*
或*.
,那么您应该只获取out文件。但是,如果看不到真实的代码和一些文件名,就很难得到正确的答案。@PaulFrancis:添加了示例文件名。否则,我认为我的问题很清楚。我从来没有说过你的问题不清楚。有时,您遵循的逻辑实际上可能不是您的代码指示编译器执行的操作。因此,最好提供实际的代码。您提供的伪代码将只提供.doc而不是。docx@PaulFrancis:代码与我使用的完全相同。我在示例代码中加入了.doc,在我自己的代码中加入了.out。我替换它的原因是为了网站的SEO目的,这样将来的用户就可以很容易地找到这个问题。如果我在标题中加入了.out和.outreview,就不会有人输入这些未知的文件扩展名。这些扩展只在我们的私有软件中使用。您可以使用后期绑定来避免设置引用。专门针对正确扩展名测试每个文件的技术也可以与OP的原始方法一起使用。专门测试每个文件的正确扩展名的技术也可以与OP的原始方法一起使用。