扫描文件夹并仅列出带有vbscript的图像文件

时间:2012-09-02 12:36:33

标签: vbscript

我正在尝试使脚本扫描文件夹并仅列出图像文件,例如jpg,png,gif。 这是代码

Dim fso, ObjFolder, ObjOutFile, ObjFiles, ObjFile, outputFile, inputFileList
Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = true

inputFileList = "list.txt"
outputFile = "C:\Users\Susan\Documents\iMacros\Macros\WindowsFiles.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objTextFile = fso.OpenTextFile(inputFileList, ForReading)

Do Until objTextFile.AtEndOfStream
 sFolderName = objTextFile.Readline
 wscript.Echo "writing contents of " & sFolderName
 writefilenames(sFolderName)
Loop

function writefilenames(sFolderName)
  Set ObjFolder = fso.GetFolder(sFolderName)

  If fso.FileExists(outputFile) Then
    Set ObjOutFile = fso.OpenTextFile(outputFile, ForAppending)
  Else
    Set ObjOutFile = fso.OpenTextFile(outputFile, ForWriting, CreateIfNeeded)
  End If

  Set ObjFiles = ObjFolder.Files

  For Each ObjFile In ObjFiles
    ObjOutFile.WriteLine(ObjFile.Path)
  Next

  ObjOutFile.Close
end function

有人能给我正确的代码吗?

1 个答案:

答案 0 :(得分:3)

仅当ObjOutFile.WriteLine(ObjFile.Path)具有匹配的扩展名时才执行命令ObjFile

Set extensions = CreateObject("Scripting.Dictionary")
extensions.CompareMode = 1 ' make lookups case-insensitive
extensions.Add "jpg", True
extensions.Add "png", True
extensions.Add "gif", True

'...

For Each ObjFile In ObjFiles
  If extensions.Exists(fso.GetExtensionName(ObjFile)) Then
    ObjOutFile.WriteLine(ObjFile.Path)
  End If
Next

请不要broadcast你的问题。对你寻求帮助的人来说,这不是很有礼貌。