Hi All,

I'm working on a script which has a primary function to look for jpg files. I can get it to work if the folder path is just my desktop but if I try the start of the path to be C:\ it errors out. Right now the error has to do with the MID function but I know it is because of me trying to go through all of C:. See the code below and if you can assist me I would really appreciate it.

Set objFSO = CreateObject("Scripting.FileSystemObject")
'objStartFolder is where you enter the folder path string
objStartFolder = "C:\"
'"C:\Users\Mordred\Desktop\"
set objFSO=CreateObject("Scripting.FileSystemObject")
'outputFile is where you enter the path and name of the LogFile text
'file.
outputFile = "C:\Users\Mordred\Desktop\LogFile.txt"
Set objFolder = objFSO.GetFolder(objStartFolder)
set oFile = objFSO.CreateTextFile(outputFile,True)

Set colFiles = objFolder.Files
'Loop through the files in the path from above (objStartFolder)
For Each objFile in colFiles
    if (LCase(Mid(objFile.Name, InstrRev(objFile.Name,"."))) = ".jpg") then
        oFile.Write objFolder & objFile.name & vbCrlf
    end if
Next

Try deleting the following code (lines 15-17):

if (LCase(Mid(objFile.Name, InstrRev(objFile.Name,"."))) = ".jpg") then
    oFile.Write objFolder & objFile.name & vbCrlf
end if

and replace it with this:

if (LCase(objFSO.getextensionname(objFile)) = "jpg") then
    oFile.Write objFolder & objFile.name & vbCrlf
end if

Resource:
VBScript: how do I determine file extensions?

The following may also be of interest:
Display All the Subfolders Within a Folder Using VBscript

commented: Thanks for this help, I really appreciate it. +5

The reason for your error is because Mid throws an error if the value of start is < 1.

Mid Function

start: Character position in string at which the part to be taken begins.

InStrRev Function

Usage: InStrRev(string1, string2[, start[, compare]])

string1: Required. String expression being searched.

string2: Required. String expression being searched for.

...

InStrRev returns a value of 0 if string2 is not
found.

commented: Thanks for clarifying why the MID Function errored out. +0

Thanks a lot for that. Now that the error is gone, I'm finding that the script is not actually searching through each folder in C:, it is just searching through C it'self and not sub-folders. I am going to investigate further but if you have a hint I would really appreciate that too. I like writing VBScripts but I'm still very newbish with them.

Add the following to the end of the script:

'====================================
'Loop through the files in the
'sub-folders in the path
'from above (objStartFolder)
'====================================

ShowSubFolders objFolder


Sub ShowSubFolders(Folder)
    On error resume next

    For Each Subfolder in Folder.SubFolders


        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files

        For Each objFile in colFiles
            if (LCase(objFSO.getextensionname(objFile)) = "jpg") then
            oFile.Write objFolder & "\" & objFile.name & vbCrlf
            end if

        Next

        ShowSubFolders Subfolder
    Next
End Sub

So now you will have:

'====================================
'objStartFolder is where you enter 
'the folder path string
'====================================

objStartFolder = "C:\"
'objStartFolder = "C:\Users\user\Desktop\"
'objStartFolder = "C:\Temp\"

Wscript.Echo objStartFolder

'====================================
'create file system object
'====================================

set objFSO=CreateObject("Scripting.FileSystemObject")

'====================================
'outputFile is where you enter the 
'path and name of the LogFile text
'file.
'====================================

outputFile = "C:\Users\user\Desktop\LogFile.txt"


Set objFolder = objFSO.GetFolder(objStartFolder)
set oFile = objFSO.CreateTextFile(outputFile,True)

Set colFiles = objFolder.Files

'====================================
'Loop through the files in the path 
'from above (objStartFolder)
'====================================


For Each objFile in colFiles

    if (LCase(objFSO.getextensionname(objFile)) = "jpg") then
        oFile.Write objFolder & "\" & objFile.name & vbCrlf
    end if

Next

'====================================
'Loop through the files in the
'sub-folders in the path
'from above (objStartFolder)
'====================================

ShowSubFolders objFolder


Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders

        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files

        For Each objFile in colFiles
            if (LCase(objFSO.getextensionname(objFile)) = "jpg") then
            oFile.Write objFolder & "\" & objFile.name & vbCrlf
            end if
        Next

        ShowSubFolders Subfolder
    Next
End Sub

How Can I Get a List of All the Files in a Folder and Its Subfolders?

May also want to read:
Error Handling in VBScript, Part One

commented: Thank you so much for your assistance here +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.