Forum Discussion

aa1's avatar
aa1
Contributor
6 years ago

Deleting folders does not work

Hi, I am trying to delete a folder from my local directory. The following piece of code seems to run for the first two or one folders and then it crashes. I cannot seem to  understand what is wrong. Can any one point out what is wrong?

 

   var foldersToDelete,aFolder;    
    foldersToDelete = aqFileSystem.FindFolders(filePath,"*_files"); 
    Delay(20000);    
    if (foldersToDelete.Count > 0)
    {
      while (foldersToDelete.HasNext())
      {
        aFolder = foldersToDelete.Next();
        aqFileSystem.DeleteFolder(aFolder.Path,true);
        Log.Message("Deleted Folder: " + aFolder.Path);
      }
    }
    else
    {
      Log.Message("No folders found to delete");    
    }

It fails at this line if (foldersToDelete.Count > 0). The error I get is this: JScript runtime error. Object required.

 

Thank you in advance.

aa

  • I think the problem is that if FindFolders doesn't find anything, it doesn't return a collection at all but a "null" value.  The code you shold use is, instead of  

     

     if (foldersToDelete.Count > 0)

    replace with 

     

    if (foldersToDelete != null)
  • AlexKaras's avatar
    AlexKaras
    6 years ago

    Hi,

     

    I think Robert is correct. .Count is not a property of the result returned by .FindFolders() method. Refer to the code sample provided in the documentation for this method.

     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I think the problem is that if FindFolders doesn't find anything, it doesn't return a collection at all but a "null" value.  The code you shold use is, instead of  

     

     if (foldersToDelete.Count > 0)

    replace with 

     

    if (foldersToDelete != null)
    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

       

      I think Robert is correct. .Count is not a property of the result returned by .FindFolders() method. Refer to the code sample provided in the documentation for this method.

       

    • aa1's avatar
      aa1
      Contributor

      Thank you so much. It worked. I had tried  if (!strictEqual(foundFolders, null)) and it did not work somehow but hadn't tried using

      if (foldersToDelete != null)

       

       

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        strictEqual() should work for JavaScript, while check for null should be used for JScript.

        Does this correspond to your situation?