如何在VB脚本中检索不同扩展名的文件?

时间:2013-12-10 09:49:07

标签: vbscript

您好我必须编写一个代码,我可以输入我想要的文件的扩展名,并将这些扩展名的文件只拉到我的excel中。该文件夹包含各种扩展名的文件。 我写的到现在是用户可以输入*如果他想要所有文件。但我要做的就是用户可以输入.txt / .pdf等,并只提取那些fils.Plz帮助。

Do while x=0
strAnswer = InputBox("Please enter the file extension  *  For all files:", _
    "File Extension")
If strAnswer = "" Then
        MsgBox"You must enter an extension."
Else
        a=strAnswer
        Exit Do
    End If
Loop
If a="*" Then
intRow = 2
'strFileName = "T:\public\Madhumita\New.xls"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
'objWorkbook.SaveAs(strFileName)
objExcel.Cells(1, 1).Value = "Folder"
objExcel.Cells(1, 2).Value = "File Name"
objStartFolder = "T:\public\Madhumita\Madhu"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
objExcel.Cells(intRow, 1).Value = objfolder.Name
objExcel.Cells(intRow, 2).Value = objFile.Name
intRow = intRow + 1
Next
objExcel.Range("A1:B1").Select
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
Sub SaveAs() 
    Application.Dialogs(xlDialogSaveAs).Show 
End Sub 
End If


objExcel.Quit
MsgBox "Done"

1 个答案:

答案 0 :(得分:1)

使用objFSO.GetExtensionName( objFile.Path )获取文件的扩展名。与用户提供的值进行比较,如果相等,则添加到工作表。