Can't believe it is so hard to copy a file with a new name

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

Post Reply
Bruce Brown
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 13
Joined: Mon Aug 26, 2013 6:49 pm

Can't believe it is so hard to copy a file with a new name

Post by Bruce Brown » Tue Oct 08, 2013 2:56 am

I started writing a program that I thought would be a breeze but is turning into a major undertaking.
The user will select a file and the user will enter a number of times it will copied and renamed with a number.
So user selects "myfile.pdf" enters into a field the number of copies they want.
By default the directory will be the source directory of selected file but the user is allowed to pick a new directory.
for example the user selected file and entered 3 into the number of copies field
The file should be copied and renamed to myfile-1.pdf, myfile-2.pdf, myfile-3.pdf.
I tried revCopy but does not allow me to rename.
I tried rename which almost works except it moves original file and renames and so won't repeat loop to make 3 copies.
It seems like I am going to have to use revcopy to copy the file to a temp directory. Rename it by the count index and move to new directory.
What a pain.

Any other ideas?
I am on windows and would like to make a simple file duper utility.
**************
Bruce

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Can't believe it is so hard to copy a file with a new na

Post by Simon » Tue Oct 08, 2013 3:56 am

Hi Bruce,
In the dictionary there is code for helping you under "copyFile":
put URL "binfile:/Disk/myfile" into URL "binfile:/Disk/Folder/myfile"
That will give you all the control you need to accomplish what you'd like.

If you need more help just ask.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Can't believe it is so hard to copy a file with a new na

Post by SparkOut » Tue Oct 08, 2013 8:29 pm

the docs wrote: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.
While the docs also say when using revCopyFile that the destination is a folder, there is nothing to stop you setting the destination to include the renamed filename. You can

Code: Select all

   answer file "where is the file to copy and rename?"
   put it into tPath
   put tPath into tNewPath
   set the itemDelimiter to slash
   put "myNewName.ext" into the last item of tNewPath
   revCopyFile tPath,tNewPath
and it will work to copy and rename the file in one go

Bruce Brown
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 13
Joined: Mon Aug 26, 2013 6:49 pm

Re: Can't believe it is so hard to copy a file with a new na

Post by Bruce Brown » Tue Oct 08, 2013 9:30 pm

Hi Sparkout.. Thanks.. I thought I tried this already but I will give it a go and report back. If it works.. my apologies for getting frustrated. I need to use revCopy because I am using it for very large TIFF images.. really big.. as in billboard size but within the limits of most programs that handle TIff images. BTW, I wish Livecode had a graphics library that handled all the popular image formats like TIFF, PSD, including cmyk versions.
It is important in the work I deal with.

BTW, my real goal is to do a loop (counter)
So that newfile name is original file name like myname.txt with myname-1.txt and myname-2 and so on. using the count number of the loop. I already figured out how to separate a filename to get extension.. and also to put extension into its own variable. I think I know how to do it.. so I will post again if I need help.

Thanks
**************
Bruce

Bruce Brown
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 13
Joined: Mon Aug 26, 2013 6:49 pm

Re: Can't believe it is so hard to copy a file with a new na

Post by Bruce Brown » Tue Oct 08, 2013 10:35 pm

Oh my god it works! I now know what happened before. My variables were not being transfered from button code to next button code. With all the trials of getting the code to work i dropped the revCopyFile command thinking that was the problem. Anyway, Thank you for you help. here is my finished program to duplicate files and it works. at least on my pc. I will compile for Mac too to see if it works.

I have three buttons and three fields all on one main stack.
First button code:

Code: Select all

on mouseUp
    ## Ask the user to choose a file
    answer file "Please choose a file"
    ## If the dialog is not cancelled put the path to the selected file into a variable
    if the result is not "cancel" then
       put it into selectedFile
       set itemDelimiter to slash
       put number of items of selectedFile into pathLength
       put selectedFile into field "file2Dupe"
       put  item 1 to pathLength-1 of it into field "outputDirectory"
    end if
end mouseUp
I originally thought/wanted selectedFile and outputDirectory to be used by the second and third button.
outputDirectory is by default the same as the original files location.
But I offered a second button to browse to a new output directory
Code shown here:

Code: Select all

on mouseUp
       ## Ask the user to choose a folder
    answer folder "Please choose an output folder"
    ## If the dialog is not cancelled put the path to the selected folder into a variable
    if the result is not "cancel" then
       put it into field "outputDirectory"
    end if
end mouseUp
And then my final button called "Do It"
Code here:

Code: Select all

local tCount 
on mouseUp
   put field "file2Dupe" into selectedFile
   set itemdelimiter to slash
   put last item of selectedFile into justFile
   put field "outputDirectory" into outputDir
   put field "numCopies" into tCount
   set itemdelimiter to "."
   put last item of justFile into fileExt
   put item -2 of justFile into fileNoExt
   set itemdelimiter to slash
   repeat with x = 1 to tCount
      revCopyFile selectedFile,outputDir & "/" & fileNoExt & "-" & x & "." & fileExt
   end repeat
end mouseUp
I was trying to avoid doing the put field "fieldname" into variables again but not quite sure how to declare a variable local or global.. to be used in all buttons.
so my code for do it button waste time to make variables.. I did make a local variable called tCount but not sure if that was needed.

This works but if I wanted variables from selecting file and selecting folder to be used in my do It button. where would I declare them?
**************
Bruce

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Can't believe it is so hard to copy a file with a new na

Post by Simon » Tue Oct 08, 2013 11:44 pm

Hi Bruce,
Here is what I came up with this morning:

Code: Select all

on mouseUp
   answer file "Which file to copy"
   put it into tFile
   answer folder "Which folder to copy to?"
   put it into tFolder
   ask "How many Copies?"
   put it into tLoop
   
   --UGH messy
   set itemDel to "."
   put the last item of tFile into tSuffix --get the suffix out, could be 3,4,5? chararcters after "."
   delete the last item of tFile 
   set itemDel to "/"
   put the last item of tFile into tFileName --need just the original file name w/o suffix
   --END Messy
   
   repeat with x = 1 to tLoop
      put url ("binfile:" & tFile & "." & tSuffix) into url ("binfile:" & tFolder &"/"& tFileName & "-" & x & "." & tSuffix )
   end repeat
end mouseUp
Not sure about your var question. I think because you are using distinct buttons you'd have to declare global vars and call them in each button.
"global gFile,gFolder --any others"
If you copy that into each button then they will all have access to the info.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Bruce Brown
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 13
Joined: Mon Aug 26, 2013 6:49 pm

Re: Can't believe it is so hard to copy a file with a new na

Post by Bruce Brown » Thu Oct 10, 2013 12:55 am

Thanks Simon.. The revCopy method is better because it does not load into memory. It is just like a shell command "copy" The files i am copying are big. But thanks for the alternative method.. It is good to know.
**************
Bruce

Post Reply