Using revCopyFile to duplicate a file renames the original

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
HankT
Posts: 17
Joined: Mon Oct 27, 2008 7:24 pm

Using revCopyFile to duplicate a file renames the original

Post by HankT » Sat Mar 14, 2009 6:59 pm

If I revCopyFile to a different directory it works as expected, however if I try to revCopyFile into the same directory with a new name, the original file gets a " 2" appended to it.

As a simple example

Code: Select all

ON mouseUp pMouseBtnNo
    answer file "Select the file to Duplicate"
    IF it is not empty THEN
        put it into SourceFile
        put it into DestinationFile
        set the itemdel to "/"
        put "Copy Of" && last item of DestinationFile into last item of destinationFile
        set the itemdel to comma
        revCopyFile SourceFile,destinationFile
    END IF
END mouseUp
this results in the sourceFile being renamed as:

sourceFile && "2"

The only thing that immediately comes to mind is copying the sourceFile to some temporary directory, then copying it, renamed, back to the original directory and finally deleting the temporary file, but that sure sounds ugly.

Does anyone have a suggestion for something cleaner?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Mar 14, 2009 10:51 pm

Hi Hank,

Perhaps you want to use the rename command?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

HankT
Posts: 17
Joined: Mon Oct 27, 2008 7:24 pm

Post by HankT » Sat Mar 14, 2009 11:14 pm

Mark wrote:Perhaps you want to use the rename command?
It still gets messy because I'd need to figure out what sourceFile's new name is. If SourceFile's name is MyFile, then it will be renamed as "MyFile 2", however if it's named "MyFile 147" then it'll be renamed "MyFile 148", so I'd have to test if the last word is a number, and increment it to be able to find the renamed file.

It's doable, but still messy. In fact, I think the copying to a temp directory approach might even be cleaner using two copy commands and a delete. It just seems like such a sledgehammer method.

Does anything else come to mind?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Mar 15, 2009 12:09 am

Dear Hank,

Why exactly do you want to rename a file? Why can't you just use the rename command? Have you read the docs regarding the rename command?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

HankT
Posts: 17
Joined: Mon Oct 27, 2008 7:24 pm

Post by HankT » Sun Mar 15, 2009 12:20 am

Mark wrote:Dear Hank,

Why exactly do you want to rename a file?
I don't, I want to Duplicate a file. The side effect of doing so within the same directory is that the original fine is getting renamed (see the sample code in the first msg to try it)

I think what is happening is that the revCopyFile command is first copying the file using the original name, thereby forcing a rename of the original file, then finishes by renaming the copied file to the name that was requested in the revCopyFile command (but this is all just a guess)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Mar 15, 2009 12:44 am

Hi Hank,

What about using the shell function? "Copy" on Windows systems and "cp" on unix systems will give you full control of the copying process.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sun Mar 15, 2009 1:02 am

Hi Hank,

you must be on a mac since revcopyfile uses applescript to do the copying and the renaming is as you suspected part of the duplication process and the way applescript dupiicates files in the same directory. I remember that the documentation once stated that you should not copy into the same directory but it does not mention that anymore.
They provide an alternative:

put URL "binfile:/Disk/myfile" into URL "binfile:/Disk/Folder/myfile"

look up revcopyfile whether it is ok for you. It seems to have limitations on large files.
I dont have used the command myself so no experience with it.
regards
Bernd

HankT
Posts: 17
Joined: Mon Oct 27, 2008 7:24 pm

Post by HankT » Sun Mar 15, 2009 1:17 am

Mark wrote:Hi Hank,

What about using the shell function?
Thanks Mark. I'm not much of a UNIX guy, but this is what I came up with as my sample, and it works well enough for my purposes that I'll be able to use it in my actually stack.

Code: Select all

ON mouseUp pMouseBtnNo
    answer file "Select file to Duplicate"
    IF it is not empty THEN
        put quote & it & quote into SourceFile
        put quote & it & quote into DestinationFile
        set the itemdel to "/"
        put "Copy Of" && last item of DestinationFile into last item of destinationFile
        set the itemdel to comma
        
        put shell ("cp" && SourceFile && destinationFile) into theResult
        IF theResult is not empty THEN
            answer error "Error:" && theResult
        END IF
    END IF
END mouseUp
Last edited by HankT on Sun Mar 15, 2009 1:24 am, edited 1 time in total.

HankT
Posts: 17
Joined: Mon Oct 27, 2008 7:24 pm

Post by HankT » Sun Mar 15, 2009 1:23 am

bn wrote:Hi Hank,

you must be on a mac since revcopyfile uses applescript to do the copying
Yes, I'm working on a Mac, hence posted in the Mac OS Category :)

Thanks for confirming my guess about the reasoning. I was pretty sure, but it's nice to get a confirmation.

I thought about using the PUT style, but I like to try to find a single solution that I can use in any situation, and since the docs DO mention the fact that large files would have to be fully loaded into memory, I decided against that method. Naturally ALL suggestions are welcomed when running into problems like this one, so thanks.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sun Mar 15, 2009 2:18 am

Hank,

:) about the mac guessing, but I read the RSS feed and jump into a topic without looking at the forum name.
glad you found a solution with Mark's help. I will note it for later use...

bernd

Post Reply