Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am trying to make the script to s

Status
Not open for further replies.

Ruriko

Programmer
Aug 17, 2012
7
I am trying to make the script to scan a folder and list only image files such as jpg, png, gif.
What this script does it reads a txt file contain the path to the folders. It will scan the folder and it will print the list of files into a new txt file. What I want is list only image files.
Code:
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

Can anyone give me the proper code?
 
Not tested, but "GetExtensionName" will give you the extension, and I would write a custom function to test the extension.
Code:
   For Each ObjFile In ObjFiles
      [highlight #FCE94F]If IsImageExtension(fso.GetExtensionName(ObjFile.Path)) Then[/highlight]
         ObjOutFile.WriteLine(ObjFile.Path)
      [highlight #FCE94F]End If[/highlight]
   Next

As for the function, something like this is a start:
Code:
Function IsImageExtension(sExtension)
   Select Case LCase(sExtension)
      Case "jpg", "jpeg", "gif", "png", "tif", "tiff", "bmp"
         IsImageExtension = True
      Case Else
         IsImageExtension = False
   End Select
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top