ftp delete non-empty folder

Dave Cragg dcragg at lacscentre.co.uk
Tue Nov 18 11:53:35 EST 2003


At 10:57 am +1000 18/11/03, Sarah wrote:
>I have a utility that creates a folder with several sub-folders on
>an FTP server and transfers files to the correct folders. This all
>works fine but I also want to be able to delete the complete folder
>when I've finished testing. So far, the only solution has been to
>loop through each file & sub-folder as I can't work out how to
>delete a non-empty folder.
>
>I have tried "delete URL ftp://..." and I have tried sending direct
>FTP commands: DELE & RMD but all three options return an error
>saying the directory is not empty.
>
>Is there a way of doing this in a single step?
>Is it a server-specific setting (in which case I can't assume that
>it will work everywhere)?

Looping through the folders and files is the only way to do it right 
now. (The delete directory command works in a similar way.)

The handler below should do what you want. Check that "the result" is 
empty  to confirm that it deleted everything. BUT please test 
carefully as there could be some gotchas.. It checks for the presence 
of a ".." listing. I don't know if any FTP servers include this in 
directory listings, but just in case. (I once got caught this way 
when looping through "the directories" on the local file system.)

on deleteFTPDirectory pUrl
   try
     if last char of pUrl <> "/" then
       put "/" after pUrl
     end if
     put url pUrl into tList
     if the result <> empty then
       throw the result
     end if
     repeat for each line tLine in tList
       if char 1 of tLine is "-" then
         put pUrl & word 9 to -1 of tLine into tFileUrl
         delete url tFileUrl
         if the result <> empty then
           throw the result
         end if
       else if char 1 of tLine is "d" then
         if word 9  of tLine <> ".." then
           put word 9 to -1 of tLine into tDir

           put pUrl & word 9 to -1 of tLine into tDirUrl
           deleteFTPDirectory tDirUrl
           if the result is not empty then
             throw the result
           end if
         end if
       end if
     end repeat
     delete url pUrl
     if the result is not empty then
       throw the result
     end if
     return empty
   catch pErr
     return pErr
   end try
end deleteFTPDirectory

Cheers
Dave


More information about the use-livecode mailing list