0

I'm trying to open a file using FileSystemObject, but when I'm trying to run it, the system does nothing. Doesn't show me Debug, No runtime error, doesn't compile. It just remains as it is. I have also checked the "MS Scripting Runtime" in References. Below is the code I've used for this:

Sub fsoobject()
Dim fso As New FileSystemObject, f As Folder, sf As Folder, myFile As File
Set f = fso.GetFolder("C:\Users\jpmehta\Desktop")

For Each sf In f.SubFolders
    For Each mySubFolder In myFolder.SubFolders
        For Each myFile In mySubFolder.Files
            If myFile.Name Like "Done" Then
                MsgBox myFile.Name
                Exit For
            End If
        Next

        MsgBox "Else"
    Next
Next

End Sub
Community
  • 1
  • 1
JayyM
  • 155
  • 1
  • 3
  • 13
  • 2
    Replace `myFolder` with `sf` – Rory Oct 30 '17 at 12:06
  • Still not happening :( – JayyM Oct 30 '17 at 12:09
  • 1
    The only thing `Like "Done"` is "Done" - perhaps you need wildcards. – SJR Oct 30 '17 at 12:12
  • As SJR said, [wildcard](https://msdn.microsoft.com/en-us/library/ee440632(v=office.12).aspx) such as `Like "*Done*"` – danieltakeshi Oct 30 '17 at 12:16
  • @JayyM shouldn't `For Each mySubFolder In myFolder.SubFolders` be `For Each mySubFolder In sf.SubFolders` ? – Shai Rado Oct 30 '17 at 12:19
  • @ShaiRado Yes, I tried replacing that too, but it's not happening. – JayyM Oct 30 '17 at 12:23
  • @SJR here, "Done" is my .jpg file name... is there something in that name that hinders the code? shall I change the file name? – JayyM Oct 30 '17 at 12:25
  • Are you looking for a file called "Done" or something containing "Done" such as "Undone" or "Not done yet"? As @danieltakeshi says `"Like "*Done*"` will match something containing "Done". If `myFile.Name` includes the file extension (I can't remember whether it will but easy to check) then you will need a wildcard, at least at the end. – SJR Oct 30 '17 at 12:29
  • Try if instr(1, myFile.Name , "Done") > 0 then MsgBox myFile.Name. Remember that case is important. Perhaps try instr(1, ucase(myFile.Name) , "DONE") > 0 then MsgBox myFile.Name. – John Muggins Oct 30 '17 at 12:31
  • I heard a function called Shell which is used to open a file.. If that is so, what will be the line for that? – JayyM Oct 30 '17 at 12:52

3 Answers3

0
Sub fsoobject()
Dim fso As New FileSystemObject, f As Folder, sf As Folder, myFile As File
Set f = fso.GetFolder("C:\Users\jpmehta\Desktop")

'For Each sf In f.SubFolders
'    For Each mySubFolder In sf.SubFolders
'        For Each myFile In mySubFolder.Files
'            If myFile.Name Like "Done" Then
'                MsgBox myFile.Name
'                Exit For
'            End If
'        Next
'
'        MsgBox "Else"
'    Next
'Next

    Dim File
    For Each File In f.Files
        If File.Name = "Done.PNG" Then
            Call Shell("Explorer.exe """ & File.Path & """", vbNormalFocus)
            Exit For
        End If
    Next

End Sub

Well, I searched a bit, and found this keyword "Shell" to open a file. On applying this code, now it executes successfully...

JayyM
  • 155
  • 1
  • 3
  • 13
0
Sub fsoobject()
Dim fso As New FileSystemObject
dim f As Folder
dim  sf As Folder
dim mySubFolder as folder 
dim  myFile As File
 Set f = fso.GetFolder("C:\Users\jpmehta\Desktop")
 dim found as boolean 'flag if found 
 For Each sf In f.SubFolders
     For Each mySubFolder In myFolder.SubFolders
         For Each myFile In mySubFolder.Files
             If myFile.Name Like "Done*.*" Then
                  MsgBox mysubfolder.path & "\" & myFile.Name
                  found = true 'we found it
                  Exit For 'so stop
              End If
        Next myFile
        if found then exit for  'cascade exit
    Next MySubFolder
    if found then exit for 'and again
 next sf

 End Sub
Harassed Dad
  • 4,669
  • 1
  • 10
  • 12
0

This code works for me. Clear and simple:

Sub fsoobject()
Dim fso As New FileSystemObject, f As Folder, sf As Folder, ssf As Folder, 
myFile As File
Set f = fso.GetFolder("C:\Users\tomek\Desktop")

For Each sf In f.SubFolders
    Set ssf = sf

    For Each myFile In ssf.Files
        If myFile.Name Like "Done" Then
            Debug.Print myFile.Name
            Exit For
        End If

    Debug.Print "Else:" & myFile.Name

    Next
Next

End Sub
  1. set subsubfolder (Set ssf = sf)
  2. I recommend to use Debug.Print to check loops in VBE