Created On:  03 October 2010

The 4Test language has a built-in function called SYS_RemoveDir – this can be used to delete a directory that is empty, however this will only able to remove a directory that is empty, there are no built-in functions that will remove a directory that is empty.

To remove a directory that is not empty a user can employ SilkTest’s ability to execute command line commands or batch files – via the SYS_Execute function.

In the below example SilkTest checks if there are contents in a specified directory, if there are no contents then the built-in SYS_RemoveDir function is used, if the directory is not empty then the SYS_Execute is used to call the RMDIR command – The RMDIR command will remove directories in whichever path you choose, while the /s /q attributes will ensure the directory is deleted whether empty or not and without prompting the user.
 
NOTE: You must be careful when using the above command to input the correct path, all directories within it will be deleted without prompting.

DelDir (String sDir)
       if (ListCount(Sys_GetDirContents(sDir))==0)  // if directory is empty
             SYS_RemoveDir(sDir)
       else  // if directory not empty
            Sys_Execute("RMDIR {sDir} /s /q")

Another way of doing this is to use a batch file:

- Create the batch file with the following contents
           RMDIR c:\directoryname /s /q
           Exit
- Save the file

-Use the SYS_Execute to call this batch file
           SYS_Execute("c:\delete.bat")