how to copy and rename a file without changing the extension

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
sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am

how to copy and rename a file without changing the extension

Post by sphere » Sat Sep 27, 2014 3:40 pm

Hi, being busy with my next problem.
i have read some tutorials and also working with files. but i can't figure it out. you can also see in my script piece that i tried some possiblities.
Want i want is to open a file, copy it to an other destination and rename it depending on a field input and without changing the extension of the file.
I saw a few examples but nowhere was thought about the extension (at least as far as i could see).
Then if this is working i want to alter it so that only pdf or jpg can be copied.

First i ask for the file, and set it in a local or global (both are not giving me want i want but ok):

Code: Select all

on mouseUp
   # Start by getting the file to upload 
  global gFileForUpload
    answer file "Select a file to upload"
    put it into gFileForUpload
    
    #put tFileForUpload into specialFolderPath("Documents") 
   #revCopyFile url ("binfile:" & slash & tFileForUpload) , ("binfile:" & specialFolderPath("Documents") & slash)
end mouseUp
I just changed this in the above code:

Code: Select all

 answer file "Select a file to upload" with type "PDF document|pdf|PDF" or type "JPG document|jpg|JPG"
So the piece of which extension to load is solved :)

Then trying to copy it and rename it, but although it does not give an error, it does not copy and rename it:

Code: Select all

on mouseUp
   
   # copy and Rename file 
   #put gFileForUpload into url ( "file:"& specialFolderPath("Documents") # & slash & field "new name")  
   put field "new name" into tDestFile
   open file gFileForUpload
   open file tDestFile
   write gFileForUpload to file tDestFile
end mouseUp

I hope that someone has a clue and can tell me what i do wrong..
Thanks for any help on this.

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

Re: how to copy and rename a file without changing the exten

Post by Klaus » Sat Sep 27, 2014 4:35 pm

Hi again sphere :D

Do this:

Code: Select all

on mouseUp
  ## Dont forget to declare GLOBAL variables!
   global gFileForUpload
   
   # copy and Rename file 

   ## The following line will only save the path as a STRING into the file! But the URL syntax is the way to go! :-)
   # put gFileForUpload into url ( "file:"& specialFolderPath("Documents") # & slash & field "new name")  
   put field "new name" into tDestFile

   ## The following will of course only work if tDestFile and gFileForUpload contain valid pathnames!
  
   ## Now use the URL syntax with BINFILE to copy the file to new location! 
   ## Since we are dealing with PDF etc, and NOT simple text files!
   put url("binfile:" & gFileForUpload) into url("binfile:" & tDestFile)
end mouseUp
Best

Klaus

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

Re: how to copy and rename a file without changing the exten

Post by jmburnod » Sat Sep 27, 2014 4:43 pm

Hi Sphere,

This script copy the selected file to documents folder with other name

Code: Select all

on mouseUp
   answer file "open file"
   if it = empty then exit mouseup
   put it into tPathS
   put specialFolderPath("Documents") into tPathDoc
   
   --get the extension
   set the itemdel to "."
   put item -1 of tPathS into tExtension
   set the itemdel to ","
   
   --build dest path
   put field "new name" into tNameD --new name = short name of file without extension
   put tPathDoc & "/" & tNameD & "." & tExtension into tPathD
   put url("binfile:" & tPathS) into url("binfile:" & tPathD) 
end mouseUp
Best regards
Jean-Marc
https://alternatic.ch

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am

Re: how to copy and rename a file without changing the exten

Post by sphere » Sat Sep 27, 2014 5:11 pm

Hello Jean-Marc,

thank you for your solution.
I'm going to try it right away.
Is it so that i can use the local also on other scripts (or a piece of code from your script) ?
or do i then have to use a global?

i only have a stack with 3 buttons and 2 input fields

thanks for your help ! much appreciated !

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

Re: how to copy and rename a file without changing the exten

Post by jmburnod » Sat Sep 27, 2014 5:20 pm

Welcome,
but dont forget Klaus's point
## Since we are dealing with PDF etc, and NOT simple text files!
You have to use "file" instead "binfile" for .txt files
Good luck for next step
Jean-Marc
https://alternatic.ch

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am

Re: how to copy and rename a file without changing the exten

Post by sphere » Sat Sep 27, 2014 6:01 pm

Oops, didn't see it first :oops: as the link in the email jumped strait to Jean-Marc post.

But hey Good to see you again Klaus.
And thanks for the explanation it really helps me to understand how it works, because that's important.
So i can test things first.
I'm having a lot of fun seeing things are getting to works and sometimes with help and sometimes with just trying a few ways around.

Danke schon !

Post Reply