2012-09-02 46 views
0

我想讓腳本掃描一個文件夾,並只列出圖像文件,如jpg,png,gif。 這是代碼掃描文件夾,並只列出圖像文件與vbscript

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 

誰能給我正確的代碼?

回答

3

僅當ObjFile具有匹配的擴展名時才執行命令ObjOutFile.WriteLine(ObjFile.Path)

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您的問題。對你要求幫助的人不太禮貌。