revCopyFile - What am I doing wrong?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

revCopyFile - What am I doing wrong?

Post by rumplestiltskin » Tue Feb 18, 2014 12:12 am

revCopyFile "oldname","newname"

Shouldn't that simply copy the file named "oldname" to a new file named "newname"?

I'm getting nothing. When I check the result, I get "execution error". Is this because I'm trying to write to the Applications folder on my Mac? (I'm not intentionally trying to write there but LC wants to do this. When I create my standalone, I assume the app will try to write to whatever folder in which the app resides. True?)

I don't seem to have any issue reading from "oldname" or writing to "oldname".

Thanks,
Barry

PS - Here's the code in my script:

Code: Select all

on mouseUp
   revCopyFile "Donations" , "DonationsPrevious"
   answer the result
   open file "Donations" for write
   write field DonationList to file "Donations"
   close file "Donations"
end mouseUp
I always get "execution error" as the result. (That line is there just for debugging.) Has something changed in OSX 10.9.1 Mavericks?

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

Re: revCopyFile - What am I doing wrong?

Post by Klaus » Tue Feb 18, 2014 1:24 pm

Hi Barry,
rumplestiltskin wrote:I'm not intentionally trying to write there but LC wants to do this.
come on, LC only does what you tell it! 8)

And if you do not supply absolute pathnames to the REVCOPYFILE command, it uses the DEFAULT FOLDER
which is obviously the application folder where you may not have writing permissins!

Use a folder where you definitively have writing permission like -> specialfolderpath("documents")

And no, nothing has changed in 10.9.1 :D


Best

Klaus

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by rumplestiltskin » Tue Feb 18, 2014 5:42 pm

Klaus,

Thanks for the input. However, when I used "open/read/write/close" to do the copying, it worked...in the Application folder. I know this is not good practice but my data file will never be more than 20KB so there won't be a memory issue.

I've read some posts up here that indicate some issues with the revCopyFile function. I'll try it with a good path to the user's Documents folder and report back.

Thanks,
Barry

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

Re: revCopyFile - What am I doing wrong?

Post by Klaus » Tue Feb 18, 2014 6:23 pm

Hi Barry,

"revCopyFile" is a bit slow, if the files you want to copy are smaller than ca. 5-10 MB, then you can also use
...
put url("binfile:path to source file...") into url("binfile:path to target file...")
...
This will be fast enough! :D


Best

Klaus

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by MaxV » Wed Feb 19, 2014 3:43 pm

In order to set the default folder to the current folder of your standalone, just use:

Code: Select all

On OpenStack
   set itemDel to "/"   
   set the defaultFolder to item 1 to -2 of (the effective fileName of this stack)
end OpenStack
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by rumplestiltskin » Wed Feb 19, 2014 4:23 pm

I probably sent a confusing message about where I was attempting to save the copied file and, therefore, the responses have been directed to that. The problem is that revCopyFile resulted in an error message while using "open/write/close" worked regardless of whether the destination was the Applications folder or my Home folder.

Is there some other code that must precede revCopyFile in order to get it to work?

Thanks,
Barry

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

Re: revCopyFile - What am I doing wrong?

Post by Klaus » Wed Feb 19, 2014 4:35 pm

Hi Barry,

from the docs about "revCopyFile", please note the meaning of the second parameter!
##########################################################################
...
revCopyFile fileToCopy,folderToCopyTo
...
Library: Common library
...
Summary:
Copies a file.

Examples:
revCopyFile "/Disk/myfile","/Disk/Folder/"
revCopyFile "data/settings.txt","olddata"
revCopyFile the sourceFile of this card, it

Use the revCopyFile command to make a copy of a file to another folder.
...
##########################################################################

Looks like that you are providing the filename of the copy to make!
LC/revCopyFile does not like that and throws an error!

In any case I would use "put binfile into another binfile":
...
put the effective filename of this stack into tSource
set itemdel to "/"
put "Donations" into item -1 of tSource
put tSource into tTarget
put "DonationsPrevious" into item -1 of tTarget
## Use FILE for Text fiels and BINFILE for everything else like images, pdfs, stack etc...
put url("file:" & tSource) into url("file:" & tTarget)
...


Best

Klaus

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by rumplestiltskin » Wed Feb 19, 2014 4:56 pm

Still somewhat confused.

revCopyFile "data/settings.txt","olddata" << (from your post)

Doesn't the above code indicate that a file named "settings.txt" will be copied to the same "data" folder under a new name "olddata"? In other words, it appears I should be able to provide the file name and a different folder name isn't required.

Barry

edit: Maybe that's just a badly worded example from the Livecode doc writer?

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

Re: revCopyFile - What am I doing wrong?

Post by Klaus » Wed Feb 19, 2014 5:05 pm

Hi Barr<,
rumplestiltskin wrote:revCopyFile "data/settings.txt","olddata" << (from your post)
Doesn't the above code indicate that a file named "settings.txt" will be copied to the same "data" folder under a new name "olddata"?
no, that example (and the other script snippets) are copied from the docs and tell me clearly that the second parameter is a FOLDER
where the file in the first parameter will be copied to!
rumplestiltskin wrote:edit: Maybe that's just a badly worded example from the Livecode doc writer?
No, just made a quick test and you need to supply a FOLDER or you get "Execution error" (from AppleScript, which is used in revCopyFile)! :D


Best

Klaus

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by rumplestiltskin » Wed Feb 19, 2014 8:06 pm

A-ha! I misunderstood what I was reading! I did see the other text referring to a folder but, as I didn't see a "/" in the example I provided, I erroneously thought I could just provide a name.

This brings up another question: Based upon this thread and the documentation, it would appear that I may use revFileCopy to copy the file to another folder (and it looks like it must be a different folder) and then, if I want to rename it, use the "rename" command on the new copy of the file in that destination folder.

I think your advice to use "put URL..." is simpler than what I've described in the previous paragraph as it accomplishes two tasks in one (copy/rename) and permits me to use the same destination folder as the source. :D

Thanks again for your assistance! I deployed the app (a donation tracker requested by my wife) and she's already requested enhancements. I love Livecode. :D

Barry

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

Re: revCopyFile - What am I doing wrong?

Post by SparkOut » Thu Feb 20, 2014 8:48 pm

You should be able to copy and rename in one go : http://forums.runrev.com/phpBB2/viewtop ... 312#p87312
Give that a try (I know it works on Windows 7)

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by rumplestiltskin » Sun Jan 03, 2016 1:21 am

I substituted...

Code: Select all

 put URL("binfile:"&theListManager) into URL("binfile:"&whatFileName)
in place of revCopyFile in my script and it seems to work just fine. So my script permits me to prepare a new name for the file (by adding the current date & time to the end of the filename). So "theListManager" is the original name and "whatFileName" is the modified name. One script to do both the copy and rename in one step...and it lets me keep all dated backups of the file in one folder (the same one the user is accustomed to opening in order to run my app).

One question, though. I see this in the dictionary entry for revCopyFile:
However, the revCopyFile command provides certain advantages. It copies file attributes (such as file type) and Mac OS resource forks along with the file. It also does not require reading the entire file into memory, so even extremely large files can be copied.

Memory: My file is 25KB and it could possibly grow to a MB or 2. Don't see any memory problem here.
FileType: Don't see a problem here as I'm not actually using any filetype except the naming convention of ".livecode" for the modifiable stack.
Resource Fork: I guess this would be an issue with something really old but it's 2016 and I think I saw my last resource fork in ResEdit on a beige machine. :lol:

If anyone thinks I should worry, please advise.

Thanks very much and a Happy New Year to all LC'ers and their families!
Barry

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

Re: revCopyFile - What am I doing wrong?

Post by Klaus » Sun Jan 03, 2016 12:42 pm

Hi Barry,

no need to worry, Resource forks are a "dying breed" :D

Nowadays OS X uses file suffixes to determine the filetype and
one or two OS versions later Resource forks on Mac OS will be history!


Best

Klaus

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: revCopyFile - What am I doing wrong?

Post by rumplestiltskin » Sat Apr 01, 2017 1:05 am

Hello, Klaus (et al),

I'm playing with an app that can manipulate JPEGs and am using LC's "drag&drop" feature to record the pathname so we know where the file is located and then may operate on it appropriately. As I only wish to manipulate JPEGs, I'm checking for the existence of the file extension ".jpeg" or ".jpg" and, if the extension is not present, I do not let the field record the pathname.

However, what about valid jpegs that do not have a file extension? I see that the Preview app fails to open files when I remove the ".jpg" or ".jpeg" extension so this issue is not "just a LC problem". Now, when I examine the jpeg file (without the extension) I see chars 5 to 8 are "jfif" which is the correct filetype for a jpeg file. So what I'm wondering (and please stop me if there's an easier way to do this) is whether I can/should do the following in my field's script:

1. Check for the proper extension and, if it's there, we're good to go so jump over the rest of what follows.
2. Assuming no extension present, open the file for reading and check chars 5 to 8 for the presence of "jfif". If so, close the file, rename it with the proper extension, and go back to step 1.

Does this seem reasonable?

Thanks,
Barry

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

Re: revCopyFile - What am I doing wrong?

Post by Klaus » Sat Apr 01, 2017 9:57 am

Hi Barry,

yes! :D
Have a nice weekend!


Best

Klaus

Post Reply