如何在VB Script中检索不同扩展名的文件



嗨,我必须写一个代码,在这里我可以输入我想要的文件的扩展名,并将这些扩展名的文件拉到我的excel中。文件夹包含各种扩展名的文件。到目前为止,我所写的是,如果用户想要所有文件,他可以输入*。但我要做的是,用户可以输入.txt/.pdf等,然后只提取这些文件。请帮忙。

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:publicMadhumitaNew.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:publicMadhumitaMadhu"
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"

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

最新更新