URL "file:newDirectory/file" should create directory if need

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderator: Klaus

Post Reply
nextyoyoma
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 37
Joined: Sat Oct 06, 2012 6:29 am

URL "file:newDirectory/file" should create directory if need

Post by nextyoyoma » Thu Nov 07, 2013 11:12 pm

I'm sure if there is a reason for the current behavior:

Code: Select all

set the defaultFolder to specialFolderPath("Desktop")
put "hello world" into URL "file:myFolder/myFile
This only works if ~/Desktop/myFolder/ already exists. If not, you have to create it. So you have to do:

Code: Select all

set the defaultFolder to specialFolderPath("Desktop")
if there is not a folder (the defaultFolder & "/myFolder") then create folder "myFolder"
put "hello world" into URL "file:myFolder/myFile
Seems it would be convenient for directories to be created on the fly if they don't exist.

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by Nonsanity » Fri Nov 08, 2013 5:06 am

That sort of change would break any code that expected/depended on the existing behavior. Normally that sort of different default behavior would warrant a new function, but this isn't a function call, at least not a discrete one.

I'd propose changing "create folder" so that giving it a larger chunk of a pathname (more/than/one/folder) would create them all—if that's not what it already does. (I don't think so, but I haven't checked.) That wouldn't break existing code that uses just a single folder name as an argument, but it would make creating nested folders as simple as a single line of code.
~ Nonsanity
~ Chris Innanen

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: URL "file:newDirectory/file" should create directory if

Post by snm » Fri Nov 08, 2013 6:11 am

It would not break any behavior of existing code. You could still check if directory exist before creating it or create without checking.

Marek

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by Nonsanity » Fri Nov 08, 2013 8:04 am

If someone has a function that should only write to a file its folder exists, and do nothing if the folder doesn't exist, then writing:

put it into url "file:missingFolder/filename.txt"

...would do nothing if the folder missingFolder did not exist. But if the code was changed to create folders, then that line would always write to that file, instead of failing when the folder is wrong, as expected.

I know I once wrote code like that where I knew only one of two folders existed on the client's computer, and that file X should be written to whichever one was there. I did not want it to create the other folder.

put mydata into url "file:male/mydata.conf"
put mydata into url "file:female/mydata.conf"

Would only write to only one file if only one of those folders existed.
~ Nonsanity
~ Chris Innanen

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: URL "file:newDirectory/file" should create directory if

Post by snm » Fri Nov 08, 2013 8:38 am

Creating missing directories by engine should be much quicker than what we have now. If such future exist, there is possible one line command. You can always check if directory exist if you need that before save the file.
So it's better to go run around only in case if you have special, sporadic needs like your example. It's more reasonable for me.

Marek

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by Nonsanity » Fri Nov 08, 2013 5:32 pm

I agree with you there, snm. It would have been better if the url and "file:" keywords would create any missing folders in the path they are given. But since they don't, changing the existing behavior would, as I described, break existing code. That should always be avoided since someone that didn't notice such a change in the release notes or forgets that, somewhere in their projects, there's some code that depends on the existing behavior, they would have no idea why their program suddenly is doing things it never did before.

The idea is good, but the timing is wrong to implement it now, particularly as it is just an ease-of-use change. Better to make another easy way to do the same thing, like expanding "create folder" to make more than one at a time. So that...

Code: Select all

create folder "C:/this/is/a/test"
...would do the same as...

Code: Select all

set itemdel to "/"
repeat for each item thefolder in "C:/this/is/a/test"
   put thefolder & "/" after thepath
   create folder thepath
end repeat
Then you could replace your proposed...

Code: Select all

put "C:/this/is/my/data.txt" into myfile
put mydata into url ("file:" & myfile)
...with just two extra lines...

Code: Select all

put "C:/this/is/my/data.txt" into myfile
set itemdel to "/"
create folder (item 1 to -2 of myfile)
put mydata into url ("file:" & myfile)
...instead of five extra lines...

Code: Select all

put "C:/this/is/my/data.txt" into myfile
set itemdel to "/"
repeat for each item thefolder in (item 1 to -2 of myfile)
   put thefolder & "/" after thepath
   create folder thepath
end repeat
put mydata into url ("file:" & myfile)
And that change won't break existing code since calling "create folder" with more than one folder does nothing at all right now. It doesn't even create the first folder. There's nothing to break.
~ Nonsanity
~ Chris Innanen

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by jacque » Fri Nov 08, 2013 10:13 pm

I propose:

Code: Select all

create folder "Users/me/folder1/folder2/" recursive
Typically when expanding the syntax, we get a new modifier like that, which leaves the original behavior intact.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: URL "file:newDirectory/file" should create directory if

Post by SparkOut » Sat Nov 09, 2013 4:37 pm

What Jacque said.

+1

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: URL "file:newDirectory/file" should create directory if

Post by snm » Sat Nov 09, 2013 5:15 pm

+1. And maybe also

Code: Select all

put myData into URL tUrl recursive
Marek

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by jacque » Sat Nov 09, 2013 8:35 pm

I liked this enough to put in a feature request for it: http://quality.runrev.com/show_bug.cgi?id=11416
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by monte » Sun Nov 10, 2013 10:43 am

May I suggest modifying the syntax to use 'recursively' seeing as I recently added it as syntactic sugar for the union and intersect commands so they recurse multi-dimensional arrays...
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by jacque » Sun Nov 10, 2013 8:22 pm

@Monte, I agree they should be consistent, but to be honest, I'd like to see your additions changed. Except for the new array syntax there are no other adverbs in the language (oops: found one exception, below.)

flip img vertical
sort lines of var numeric
move btn 1 50,50 relative

I thought the union/intersect change was the only adverb in the language until I found this (also recently-added) one:

start using font file tPath globally

Which I also think is wrong. Could you make your array syntax accept both "recursive" and "recursively" to make it easier for us old-timers who have never before had to use adverbs?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by monte » Mon Nov 11, 2013 1:44 am

Hmm... can you raise this on the engine forum? I think both of these adverbs were recommendations from Mark. It's a quick change but it would want to happen before 6.5 is out.
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: URL "file:newDirectory/file" should create directory if

Post by jacque » Mon Nov 11, 2013 8:06 pm

Done. You guys can hash it out.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply