How to delete all the sub-directories in a directory



Recently we received a question from a customer that was using a set of IP cameras and a central FTP server where the cameras stored their recorded videos. As the cameras generate a lot of data they wanted to schedule an FTP script that deletes all the sub-folders of a given folder.

 

The script is pretty straightforward, all it is needed is to use GETLIST to retrieve the list of sub-directories, then use FOREACH and RMDIR to delete each one:

 

# Connect to FTP server
OPENHOST("ftp.myhost.com","myuser","mypass")
 
# Change current remote directory
CHDIR("/www/Cameras")
 
# Get all the subdirectories
GETLIST($list,REMOTE_DIRECTORIES)
 
# For each directory run the RMDIR command
FOREACH $item IN $list
   RMDIR($item)
END FOREACH
 
# Finished, close the connection
CLOSEHOST