Rename a file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Rename a file

Valentin Ryckewaert
Hi everyone,

I'm trying to rename files with this code but it doesn't work, pharo is saying me "PrimitiveFailed"
I tried lots of things and I'm not finding how I can rename this file, someone has an idea please?

| myFile |
myFile := FileSystem root / 'aPath/cat.jpg'.
myFile renameTo:'cats.jpg'

Valentin
Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Sven Van Caekenberghe-2
Hi Valentin,

> On 18 Apr 2016, at 10:31, Valentin Ryckewaert <[hidden email]> wrote:
>
> Hi everyone,
>
> I'm trying to rename files with this code but it doesn't work, pharo is saying me "PrimitiveFailed"
> I tried lots of things and I'm not finding how I can rename this file, someone has an idea please?
>
> | myFile |
> myFile := FileSystem root / 'aPath/cat.jpg'.
> myFile renameTo:'cats.jpg'
>
> Valentin

Seems to work OK for me (Pharo 5 on Mac OS)

'/tmp/foo.txt' asFileReference writeStreamDo: [ :out | out << 'Pharo' ].

'/tmp/foo.txt' asFileReference exists.

'/tmp/foo.txt' asFileReference renameTo: 'bar.txt'.

'/tmp/foo.txt' asFileReference exists.

'/tmp/bar.txt' asFileReference exists.

'/tmp/bar.txt' asFileReference contents.

Maybe a permission problem ?

Sven
Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Valentin Ryckewaert
Using your method (A string asFileReference) it works thanks !

But if you use FileSystem root / .... I get the "same" object but it's still not working. Did I make a mistake somewhere?

2016-04-18 10:38 GMT+02:00 Sven Van Caekenberghe <[hidden email]>:
Hi Valentin,

> On 18 Apr 2016, at 10:31, Valentin Ryckewaert <[hidden email]> wrote:
>
> Hi everyone,
>
> I'm trying to rename files with this code but it doesn't work, pharo is saying me "PrimitiveFailed"
> I tried lots of things and I'm not finding how I can rename this file, someone has an idea please?
>
> | myFile |
> myFile := FileSystem root / 'aPath/cat.jpg'.
> myFile renameTo:'cats.jpg'
>
> Valentin

Seems to work OK for me (Pharo 5 on Mac OS)

'/tmp/foo.txt' asFileReference writeStreamDo: [ :out | out << 'Pharo' ].

'/tmp/foo.txt' asFileReference exists.

'/tmp/foo.txt' asFileReference renameTo: 'bar.txt'.

'/tmp/foo.txt' asFileReference exists.

'/tmp/bar.txt' asFileReference exists.

'/tmp/bar.txt' asFileReference contents.

Maybe a permission problem ?

Sven

Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Michal Balda
In reply to this post by Valentin Ryckewaert
Hi,
I think the problem is using 'aPath/cat.jpg' as a single string (= as a
single component of the path).

This:
     path1 := FileSystem root / 'a/b/c.txt'.
is different to:
     path2 := FileSystem root / 'a' / 'b' / 'c.txt'.

Compare the results when I ask for the parent directory:
     path1 parent "-> File @ /"
     path2 parent "-> File @ /a/b"

When renaming, the #renameTo: method takes the #parent directory as the
base for the target filename.

Therefore:

(FileSystem root / 'a/b/c.txt')
     renameTo: 'my.txt'.

will try to create file:
     FileSystem root / 'my.txt'

Whereas:

(FileSystem root / 'a' / 'b' / 'c.txt')
     renameTo: 'my.txt'.

will try to create file:
     FileSystem root / 'a' / 'b' / 'my.txt'

And in your case, FileSystem root is probably not writable.


Michal



On 18.4.2016 10:31, Valentin Ryckewaert wrote:

> Hi everyone,
>
> I'm trying to rename files with this code but it doesn't work, pharo
> is saying me "PrimitiveFailed"
> I tried lots of things and I'm not finding how I can rename this file,
> someone has an idea please?
>
> | myFile |
> myFile := FileSystem root / 'aPath/cat.jpg'.
> myFile renameTo:'cats.jpg'
>
> Valentin


Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

CyrilFerlicot
In reply to this post by Valentin Ryckewaert


On 18/04/2016 10:44, Valentin Ryckewaert wrote:
> Using your method (A string asFileReference) it works thanks !
>
> But if you use FileSystem root / .... I get the "same" object but it's
> still not working. Did I make a mistake somewhere?
>

Hi,

Did you try `FileSystem root / 'a' / 'Path' / 'cat.jpg'` instead of
`FileSystem root / 'aPath/cat.jpg'`?

On what OS are you? I remember I had some trouble with the root on Windows.

--
Cyril Ferlicot

http://www.synectique.eu

165 Avenue Bretagne
Lille 59000 France


signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Valentin Ryckewaert
I'm on Linux

Is it wanted that root / 'a' / 'foo.txt' is different than root / '.foo.txt' ? I find that strange

2016-04-18 10:52 GMT+02:00 Cyril Ferlicot Delbecque <[hidden email]>:


On 18/04/2016 10:44, Valentin Ryckewaert wrote:
> Using your method (A string asFileReference) it works thanks !
>
> But if you use FileSystem root / .... I get the "same" object but it's
> still not working. Did I make a mistake somewhere?
>

Hi,

Did you try `FileSystem root / 'a' / 'Path' / 'cat.jpg'` instead of
`FileSystem root / 'aPath/cat.jpg'`?

On what OS are you? I remember I had some trouble with the root on Windows.

--
Cyril Ferlicot

http://www.synectique.eu

165 Avenue Bretagne
Lille 59000 France


Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

CyrilFerlicot


On 18/04/2016 10:56, Valentin Ryckewaert wrote:
> I'm on Linux
>
> Is it wanted that root / 'a' / 'foo.txt' is different than root /
> '.foo.txt' ? I find that strange
>

If you want a relative path from the image location you can use
`FileSystem workingDirectory / 'foo.txt'`

--
Cyril Ferlicot

http://www.synectique.eu

165 Avenue Bretagne
Lille 59000 France


signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Sven Van Caekenberghe-2
In reply to this post by Valentin Ryckewaert
Think of it as follows: Pharo is cross platform, forward slash is used on *nix and Mac OS, but not Windows where it is backslash. FileReference is an object oriented API that abstracts over that. Asking for an interpretation of the sub parts is a step too far (even if it would be handy).

> On 18 Apr 2016, at 10:56, Valentin Ryckewaert <[hidden email]> wrote:
>
> I'm on Linux
>
> Is it wanted that root / 'a' / 'foo.txt' is different than root / '.foo.txt' ? I find that strange
>
> 2016-04-18 10:52 GMT+02:00 Cyril Ferlicot Delbecque <[hidden email]>:
>
>
> On 18/04/2016 10:44, Valentin Ryckewaert wrote:
> > Using your method (A string asFileReference) it works thanks !
> >
> > But if you use FileSystem root / .... I get the "same" object but it's
> > still not working. Did I make a mistake somewhere?
> >
>
> Hi,
>
> Did you try `FileSystem root / 'a' / 'Path' / 'cat.jpg'` instead of
> `FileSystem root / 'aPath/cat.jpg'`?
>
> On what OS are you? I remember I had some trouble with the root on Windows.
>
> --
> Cyril Ferlicot
>
> http://www.synectique.eu
>
> 165 Avenue Bretagne
> Lille 59000 France
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Valentin Ryckewaert
Ok thanks everyone for your answers I got it ! :)

2016-04-18 11:08 GMT+02:00 Sven Van Caekenberghe <[hidden email]>:
Think of it as follows: Pharo is cross platform, forward slash is used on *nix and Mac OS, but not Windows where it is backslash. FileReference is an object oriented API that abstracts over that. Asking for an interpretation of the sub parts is a step too far (even if it would be handy).

> On 18 Apr 2016, at 10:56, Valentin Ryckewaert <[hidden email]> wrote:
>
> I'm on Linux
>
> Is it wanted that root / 'a' / 'foo.txt' is different than root / '.foo.txt' ? I find that strange
>
> 2016-04-18 10:52 GMT+02:00 Cyril Ferlicot Delbecque <[hidden email]>:
>
>
> On 18/04/2016 10:44, Valentin Ryckewaert wrote:
> > Using your method (A string asFileReference) it works thanks !
> >
> > But if you use FileSystem root / .... I get the "same" object but it's
> > still not working. Did I make a mistake somewhere?
> >
>
> Hi,
>
> Did you try `FileSystem root / 'a' / 'Path' / 'cat.jpg'` instead of
> `FileSystem root / 'aPath/cat.jpg'`?
>
> On what OS are you? I remember I had some trouble with the root on Windows.
>
> --
> Cyril Ferlicot
>
> http://www.synectique.eu
>
> 165 Avenue Bretagne
> Lille 59000 France
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Nicolai Hess-3-2
In reply to this post by Sven Van Caekenberghe-2


2016-04-18 11:08 GMT+02:00 Sven Van Caekenberghe <[hidden email]>:
Think of it as follows: Pharo is cross platform, forward slash is used on *nix and Mac OS, but not Windows where it is backslash. FileReference is an object oriented API that abstracts over that. Asking for an interpretation of the sub parts is a step too far (even if it would be handy).

We have an open bugtracker issue about this.
There is no conclusion right now how to handle this.
I understand that this path interpretation is a step too far , on the other hand we already do this in #resolveString:

see output of (on a windows platform(!))

(FileSystem root / 'aPath/file.txt') basename.
(FileSystem root / 'aPath\file.txt') basename.

vs.

(FileSystem root resolveString:'aPath/file.txt') basename.
(FileSystem root resolveString:'aPath\file.txt') basename.

#/ could behave like #resolveString

And for example:

(FileLocator vmDirectory / 'testdir/file.txt') writeStream << 'Test'

will succeed, if the directory <vmdirectory>/testdir/ exists (it does not try to create a file called "testdir/file.txt", but really just "file.txt").



(see issue 13217)



 

> On 18 Apr 2016, at 10:56, Valentin Ryckewaert <[hidden email]> wrote:
>
> I'm on Linux
>
> Is it wanted that root / 'a' / 'foo.txt' is different than root / '.foo.txt' ? I find that strange
>
> 2016-04-18 10:52 GMT+02:00 Cyril Ferlicot Delbecque <[hidden email]>:
>
>
> On 18/04/2016 10:44, Valentin Ryckewaert wrote:
> > Using your method (A string asFileReference) it works thanks !
> >
> > But if you use FileSystem root / .... I get the "same" object but it's
> > still not working. Did I make a mistake somewhere?
> >
>
> Hi,
>
> Did you try `FileSystem root / 'a' / 'Path' / 'cat.jpg'` instead of
> `FileSystem root / 'aPath/cat.jpg'`?
>
> On what OS are you? I remember I had some trouble with the root on Windows.
>
> --
> Cyril Ferlicot
>
> http://www.synectique.eu
>
> 165 Avenue Bretagne
> Lille 59000 France
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Valentin Ryckewaert
An interresting fact is that exists answer true to 
(FileSystem root / 'aPath/cat.jpg') exists

2016-04-18 11:25 GMT+02:00 Nicolai Hess <[hidden email]>:


2016-04-18 11:08 GMT+02:00 Sven Van Caekenberghe <[hidden email]>:
Think of it as follows: Pharo is cross platform, forward slash is used on *nix and Mac OS, but not Windows where it is backslash. FileReference is an object oriented API that abstracts over that. Asking for an interpretation of the sub parts is a step too far (even if it would be handy).

We have an open bugtracker issue about this.
There is no conclusion right now how to handle this.
I understand that this path interpretation is a step too far , on the other hand we already do this in #resolveString:

see output of (on a windows platform(!))

(FileSystem root / 'aPath/file.txt') basename.
(FileSystem root / 'aPath\file.txt') basename.

vs.

(FileSystem root resolveString:'aPath/file.txt') basename.
(FileSystem root resolveString:'aPath\file.txt') basename.

#/ could behave like #resolveString

And for example:

(FileLocator vmDirectory / 'testdir/file.txt') writeStream << 'Test'

will succeed, if the directory <vmdirectory>/testdir/ exists (it does not try to create a file called "testdir/file.txt", but really just "file.txt").



(see issue 13217)



 

> On 18 Apr 2016, at 10:56, Valentin Ryckewaert <[hidden email]> wrote:
>
> I'm on Linux
>
> Is it wanted that root / 'a' / 'foo.txt' is different than root / '.foo.txt' ? I find that strange
>
> 2016-04-18 10:52 GMT+02:00 Cyril Ferlicot Delbecque <[hidden email]>:
>
>
> On 18/04/2016 10:44, Valentin Ryckewaert wrote:
> > Using your method (A string asFileReference) it works thanks !
> >
> > But if you use FileSystem root / .... I get the "same" object but it's
> > still not working. Did I make a mistake somewhere?
> >
>
> Hi,
>
> Did you try `FileSystem root / 'a' / 'Path' / 'cat.jpg'` instead of
> `FileSystem root / 'aPath/cat.jpg'`?
>
> On what OS are you? I remember I had some trouble with the root on Windows.
>
> --
> Cyril Ferlicot
>
> http://www.synectique.eu
>
> 165 Avenue Bretagne
> Lille 59000 France
>
>




Reply | Threaded
Open this post in threaded view
|

Re: Rename a file

Tudor Girba-2
In reply to this post by CyrilFerlicot
You can also use String>>asFileReference:

relative path:
        './foo.txt' asFileReference

absolute path:
        '/foo.txt' asFileReference

Cheers,
Doru

> On Apr 18, 2016, at 11:01 AM, Cyril Ferlicot Delbecque <[hidden email]> wrote:
>
>
>
> On 18/04/2016 10:56, Valentin Ryckewaert wrote:
>> I'm on Linux
>>
>> Is it wanted that root / 'a' / 'foo.txt' is different than root /
>> '.foo.txt' ? I find that strange
>>
>
> If you want a relative path from the image location you can use
> `FileSystem workingDirectory / 'foo.txt'`
>
> --
> Cyril Ferlicot
>
> http://www.synectique.eu
>
> 165 Avenue Bretagne
> Lille 59000 France
>

--
www.tudorgirba.com
www.feenk.com

"Presenting is storytelling."