Page 1 of 1

Delete folder syntax

Posted: Thu Jan 12, 2012 7:39 am
by grovecat
I can create folders in the Documents folder and read and write to files in them. However, I am having trouble deleting a folder and I can't seem to get the syntax right.

Could someone please just give me a line of code that deletes a folder in iOS?

Thanks
Don

Re: Delete folder syntax

Posted: Thu Jan 12, 2012 10:33 am
by Klaus
Hi Don,

...
delete folder XYZ
...
should do, BUT the folder needs to be EMPTY before you can delete it!
So you will have to delete all files inside of it first!


Best

Klaus

Re: Delete folder syntax

Posted: Thu Jan 12, 2012 11:36 am
by jmburnod
Hi Don,

There is a lesson to get the list of files in folder and subfolders
http://www.runrev.com/developers/lesson ... d-folders/


Best regards

Jean-Marc

Re: Delete folder syntax

Posted: Thu Jan 12, 2012 4:18 pm
by FourthWorld
Klaus wrote:Hi Don,

...
delete folder XYZ
...
should do, BUT the folder needs to be EMPTY before you can delete it!
So you will have to delete all files inside of it first!
Also, the directory cannot be in use by your app or any other at the time you delete it. This has bitten me once, but simply setting the default folder to something else takes care of it.

If you have any issues with deleting a folder, or pretty much any file I/O issue, it can be helpful to include the sysError function in your reporting, e.g.:

Code: Select all

delete folder tMyPath
   if the result is not empty then
     answer the result & "("& sysError() &")"
  end if 
SysError returns the error code sent by the host OS to identify the specific reason an action failed. So while LiveCode itself may only return "can't delete folder", adding sysError will let you know WHY you can't delete it (in use by another app, doesn't exist, isn't empty, etc.). You'll have to look up the error codes at the OS vendor's site, but once you do it can become easy to lookup what the error code means to provide more specific feedback to the user.

Re: Delete folder syntax

Posted: Thu Jan 12, 2012 11:36 pm
by grovecat
Thanks guys

I was aware that in iOS only an empty folder could be deleted, and I was sure that was the case. However, your posts prompted me to look a bit deeper and I found there was a file in there after all. So, problem solved.

Cheers
Don

Re: Delete folder syntax

Posted: Thu Apr 05, 2012 3:37 am
by FireWorx
So is the best practice to return the files and then delete them one by one in a repeat loop and then delete the empty folder? Such as.... Seems like a lot of work just to delete a folder with some files in it. Is there an easier way?
By the way this script does work I tested it.

on mouseUp
if the environment is not mobile then
put "/Users/fireworxsoftware/Desktop/The Files/library" into TPath1
put tPath1 & "/MyFolder" into tPath2
else
put specialFolderPath("Library") into tPath1
put tPath1 & "/MyFolder" into tPath2
end if
set the defaultfolder to tPath2
put the files into tMyList
repeat with x = 1 to the number of lines of tMylist
put line x of tMyList into tFile
delete file tPath2 & "/" & tFile
end repeat
delete folder tPath1 & "/MyFolder"
end mouseUp