سؤال

I have written the following subroutine:

Sub Openf(fldr,fso,ByRef newf)
  Dim subf,fpath,ext,fname,IsDone
  Set subf=fldr.SubFolders
  If(Not subf Is Nothing) Then
    For Each sf in subf
      fpath=fso.GetAbsolutePathName(sf.Path)
      fname=fso.GetBaseName(fpath)
      If(StrComp(fname,"mm")=0) Then
        IsDone=Delfldr(sf.SubFolders,fso,newf)
      End If
      Openf sf,fso,newf
    Next
  Else
    Set subf=fldr.Files
    For Each sf in subf
      fpath=fso.GetAbsolutePathName(sf.Path)
      ext=CStr(fso.GetExtensionName(fpath))
      If(StrComp(ext,"tar.gz")=0) Then
        Delfile subf,fso,newf
      End If
    Next
  End If
End Sub

It is supposed to recursively iterate through all the subfolders within a given folder and stop when no more subfolders can be found. At that level, it must get the collection of files that will be present (with the extension tar.gz) and then call the Delfile subroutine. One more condition is that the subfolders in the mm folder must be deleted directly (using Delfldr function) without going into them to the level where files can be found.

The problem is that only the folders in the mm folders are getting deleted. The script doesn't seem to go through the other subfolders that are present at the same level as the mm folder. In other words, the files with the tar.gz extension are not getting deleted.

The hierarchy is as follows:

C:\backups → c6,mm → es → at01 → files with tar.gz extension

Also, is there an equivalent of the return() statement in VBScript? I initially thought the problem was because the control wasn't returned back to the calling statement after the Delfldr subroutine was executed. So I changed it to a function and returned a bool value to IsDone, thinking it will return control to the Openf subroutine. However this did not work.

هل كانت مفيدة؟

المحلول

I see 2 issues with your procedure:

  1. SubFolders is always a Folders collection, so sf will never be Nothing and your code will never enter the Else branch (thus never processing any files). Change your condition from

    Not subf Is Nothing
    

    to

    fldr.SubFolders.Count > 0
    
  2. GetExtenstionName() only returns a single extension, so GetExtensionName("file.tar.gz") will produce gz, not tar.gz, so even if your code entered the Else branch (which it doesn't) it still wouldn't delete any files. You need something like this to extract double extensions from a file name:

    ext = fso.GetExtensionName(fso.GetBaseName(fname)) & "." _
            & fso.GetExtensionName(fname)
    

    BTW, you don't need CStr() here, since GetExtensionName() already returns a string.

As for your question regarding return(): in what respect to you mean "equivalent"? If you want the procedure/function to return to the caller you can do that via Exit Sub and Exit Function respectively. If you want to return a value to the caller, you can do that by assigning the value to the function name:

Function Foo()
  Foo = "bar"
End Function

Note that the latter works only for functions, not procedures. Also, the assignment can take place anywhere in the function body, it doesn't have to be at the end.

On a more general note, I wouldn't use StrComp() for string comparisons unless you actually want to make use of the 3-state comparison it does (less/equal/greater). Better (as in "better readable") would be a simple comparison like this:

If LCase(ext) = "tar.gz" Then
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top