0

我正在尝试使脚本扫描文件夹并仅列出 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

谁能给我正确的代码?

4

1 回答 1

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

请不要传播您的问题。对你寻求帮助的人不是很有礼貌。

于 2012-09-02T13:08:47.500 回答