How to change a file extension ?
[1/9] from: pat665::ifrance::com at: 13-Jan-2002 17:52
How to change a file extension ?
Example:
Changing from %program.r to %program.bak
Pitfall:
It cannot be done with a simple find and replace. Find and replace will fail
in some case like the following:
/d/rebol/prog.r/program.r -> /d/rebol/prog.bak/program.r
/d/rebol/prog.r/program.r -> /d/rebol/prog.bak/program.bak
Divide and conquer:
1. How to find the file extension ?
2. How to replace on extension with another ?
Solution 1:
short anwser : with find/last
The file extension is found like this.
myfile: %/d/rebol/prog.r/program.r
print find/last myfile "." ; .r
Solution 2:
infile: to-string %/d/rebol/prog.r/program.r
ext: find/last infile "."
outfile: copy/part infile (length? infile) - (length? ext)
append outfile ".bak"
print outfile
It seems to me a bit complicated. Does anyone have a better (simpler)
solution ?
[2/9] from: rotenca:telvia:it at: 13-Jan-2002 19:36
Hi Patrick,
> infile: to-string %/d/rebol/prog.r/program.r
> ext: find/last infile "."
<<quoted lines omitted: 3>>
> It seems to me a bit complicated. Does anyone have a better (simpler)
> solution ?
one mode:
file: %/c/d/e/f.ext-with
clear change find/last file "." ".r"
probe file
another
file: %/c/d/e/f.ext-with
change/part find/last file "." ".r" tail file
probe file
---
Ciao
Romano
[3/9] from: al:bri:xtra at: 14-Jan-2002 8:47
Patrick Philipotwrote:
> How to change a file extension ?
> Example:
> Changing from %program.r to %program.bak
CD: :change-dir
LD: :list-dir
MD: :make-dir
WD: :what-dir
Directory?: func [File [file!]][#"/" = last File]
Filename: func [File [file!]][copy/part File find/last File "."]
Extension: func [File [file!] Extension [file!]][
join Filename File Extension
]
>> extension %program.r %.bak
== %program.bak
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[4/9] from: rebol665:ifrance at: 13-Jan-2002 23:28
Hi paolo and andrew,
Thanks to both of you. However I do not understand how it works. Especially
what find/last is doing.
For example:
file: %/c/rebol/prog.r/program.r
find/last file "."
== %.r
So it returns the end of the file (or the end of the string in my previous
post). However you are both using it as it was returning a position. What is
wrong with me ?
Patrick
[5/9] from: nitsch-lists:netcologne at: 13-Jan-2002 23:50
RE: [REBOL] How to change a file extension ?
Hi Patrick
foreach f read %./
[ either parse f [copy g to %.r thru %.r]
[ g: join g %.bak Print ["renaming" f "->" g] ]
[ print ["keeping" f] ]
]
; + quick double %.r workaround:
foreach f read %./
[ either parse f compose [copy g to (x: %.r) thru (x) ]
[ g: join g %.bak Print ["renaming" f "->" g] ]
[ print ["keeping" f] ]
]
-volker
[pat665--ifrance--com] wrote:
[6/9] from: rotenca:telvia:it at: 14-Jan-2002 0:50
Hi Patrick,
>So it returns the end of the file (or the end of the string in my previous
>post). However you are both using it as it was returning a position. What is
>wrong with me ?
The answer is the argument of the Part refinement. From doc:
For series, the /PART refinement will
copy [change remove] a given number of values (specified as either a
count or as an ending series position).
If the argument si an integer, it is a length value, if a series, it is the
pointer to the end.
In the latter, the index is implicit: it is derived from the position in the
series.
---
Ciao
Romano
[7/9] from: rotenca:telvia:it at: 14-Jan-2002 2:44
Hi Patrick,
> file: %/c/rebol/prog.r/program.r
> find/last file "."
> == %.r
>
> So it returns the end of the file (or the end of the string in my previous
> post). However you are both using it as it was returning a position. What is
> wrong with me ?
I had misinterpreted your question. My answer is:
find/last does not return the end of the series, but start from the end of the
series to search. It return the series at the position in which the search
stop. In the example at the position of "." in the 'file series.
---
Ciao
Romano
[8/9] from: joel:neely:fedex at: 14-Jan-2002 9:50
Hi, Patrick,
If you like shuffling cards, you'll love this solution:
Patrick Philipot wrote:
> How to change a file extension ?
> Example:
<<quoted lines omitted: 4>>
> /d/rebol/prog.r/program.r -> /d/rebol/prog.bak/program.r
> /d/rebol/prog.r/program.r -> /d/rebol/prog.bak/program.bak
change-tail: func [s [string!] t [string!] r [string!]] [
if (head reverse copy t) copy/part head reverse copy s length? t [
s: head reverse head
change/part
head reverse s
head reverse copy r
length? t
]
s
]
>> change-tail "/d/rebol/prog.r/program.r" ".r" ".bak"
== "/d/rebol/prog.r/program.bak"
Just for fun!!!
-jn-
[9/9] from: rebol665:ifrance at: 14-Jan-2002 21:51
Hi, joel
You're not helping ... But it was funny !
And it had indeed crossed my mind to try the reverse stuff.
I then judged it was time to call for help.
I am glad I did it because I did not have understood how the /part
refinement actually worked in copy, remove and change.
I think I have now, thanks to Paolo and Andrew and you.
Not a waste of time at all.
Patrick
ps : my final version
infile: %/d/rebol/prog.r/program.r
outfile: append copy/part infile find/last infile ".r" ".bak"
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted