Forum Discussion

almmatt's avatar
almmatt
Contributor
9 years ago

Checking if a file exists in the Recycle Bin

Hello everyone,

 

Last week I posted a topic to get the current users SID to build a path to their Recycle Bin on disk (C:\$Recycle.bin\<SID>). I got the answer I was looking for, but as it turns out, aqFile.Exists() doesn't work for the recycle bin, as files are actually stored with a temporary name, and not the name they had when outside the recycle bin.

 

12/03/2015  12:27 PM           136,591 $RYXCIJU.zip
07/24/2015  08:51 AM           112,844 $RYXUSBX.tmp
12/09/2015  03:06 PM             6,416 $RYXXHBZ.7z
11/19/2015  03:38 PM           137,504 $RYYPJ6Q.ztl
07/10/2015  03:45 PM    <DIR>          $RYYRMP6.tmp
08/10/2015  11:21 AM             4,412 $RYYZOHE.zip

 

So, my new problem is this: Part of our regression testing requires that we verify deleting an item within our application moves it to the Recycle Bin. However, I don't know how to actually verify that the file exists in that directory. I read in a different topic that it's not a good idea to actually map items in Explorer, as this can cause failures on different versions of Windows.

 

Has anyone had any success verifying that a file is now in the recycle bin within TestComplete (KeywordTests or Script)?

 

Thanks for your time,

 

Matt

6 Replies

  • djadhav's avatar
    djadhav
    Regular Contributor

    Here is VBScript method.

     

    DIM initial_FileCount
    DIM new_FileCount
    DIM exists_FileInRecycleBin
    
    initial_FileCount = lib_CountFiles("Path to recycle bin")
    
    'Perform Delete operation
    
    new_FileCount = lib_CountFiles("Path to recycle bin")
    
    exists_FileInRecycleBin = (new_FileCount - initial_FileCount = 1)
    
    FUNCTION lib_CountFiles(ByVal fldr_path)
        DIM fso : SET fso = CreateObject("Scripting.FileSystemObject")
        Set objFiles = fso.GetFolder(fldr_path).Files
        lib_CountFiles = objFiles.Count
    END FUNCTION

    Since it is highly unlikely that another process put a new file in the recycle bin, this increase in filecount can be safely assumed to be because of the delete operation you performed.

    • almmatt's avatar
      almmatt
      Contributor

      Thanks for the code snippet. I tried using the count method when I first approached this problem, but it appears that when you delete a file, there are other items that are also placed into the recycle bin at the same time. So, for example, the count before deleting a file could be 15, you delete 1 item from disk, and the new could would be 17. Deleting one more file could cause the count to go to 20. It is an unreliable approach.

      • djadhav's avatar
        djadhav
        Regular Contributor

        I can see why it can seem like an unreliable method. Although as we can see that since the number of files produced in the recycle bin is unpredictable, an increase in file count is a highly accurate indication that file was deleted from somewhere.

        For more accurate verification, we can use the file size. What do you think of this?

         

        DIM fso : SET fso = CreateObject("Scripting.FileSystemObject")
        
        DIM exists_FileInRecycleBin
        DIM objFile, fileSize
        
        objFile = fso.GetFile({FilePath})
        fileSize = objFile.size
        
        'Perform Delete operation
        
        exists_FileInRecycleBin = lib_FindFileBySize({Path to recycle bin}, fileSize)
        
        FUNCTION lib_FindFileBySize(ByVal fldr_path, ByVal byt_fileSize)
        lib_FindFileBySize = FALSE
            DIM objFiles , obj_file
            Set objFiles = fso.GetFolder(fldr_path).Files
            For Each obj_file in objFiles
              IF obj_file.size = CLng(byt_fileSize) THEN
        		lib_FindFileBySize = TRUE
        		EXIT FOR
              END IF
            Next
        END FUNCTION
  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Matt,

     

    Please take a look at this reply. I suppose it will give you some clue on how to resolve your question.

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    > [...] verify deleting an item within our application moves it to the Recycle Bin.

    Slightly off topic, but what if deletion to the Recycle Bin is disabled on the target system? (Personally I always delete files permanently and always disable deletion to the Recycle Bin on my machines.) Maybe it is just enough to verify that the file is no longer present in its initial folder?

    • almmatt's avatar
      almmatt
      Contributor

      Alex,

       

      Thanks for your response. Our regression test case specifically states to verify that the file has been moved to the recycle bin. Since I have had difficulty doing so, our test currently just verified the file is no longer on disk in the original location. :robotwink: