Delete folder syntax

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
grovecat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 99
Joined: Thu Aug 04, 2011 10:32 am

Delete folder syntax

Post by grovecat » Thu Jan 12, 2012 7:39 am

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

Klaus
Posts: 14235
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Delete folder syntax

Post by Klaus » Thu Jan 12, 2012 10:33 am

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Delete folder syntax

Post by jmburnod » Thu Jan 12, 2012 11:36 am

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
https://alternatic.ch

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10063
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Delete folder syntax

Post by FourthWorld » Thu Jan 12, 2012 4:18 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

grovecat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 99
Joined: Thu Aug 04, 2011 10:32 am

Re: Delete folder syntax

Post by grovecat » Thu Jan 12, 2012 11:36 pm

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

FireWorx
Posts: 362
Joined: Wed Sep 07, 2011 9:39 pm

Re: Delete folder syntax

Post by FireWorx » Thu Apr 05, 2012 3:37 am

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

Post Reply