For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > December 2005 > Renaming a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Renaming a file
siddharthabhishek@gmail.com

2005-11-30, 3:58 am

Hi,

i have been trying to rename a file by using following piece of perl
code

use File::Copy;
move("e:\proj\file1.txt","e:\proj\file2.txt");

but my code is throwing an error " Permission Denied"

whereas i am able to use copy function , as shown below
use File::Copy;
copy("e:\proj\file1.txt","e:\proj\file2.txt");

i could easily work with this but my problem is that i dont want both
the files to exist together. moreover what is bothering me more is why
move function is not working??? I am not getting that perfect result
from my code. :(

Please note that i am using Windows 2000 professional as OS and
activeperl 5.8.7
IMP note: i am able to rename a file, which is lying in the same
directory as my perl code, by using move funtion. ( i want to
implement this code to rename any file from anywhere in the file
system)

please guide me

Thanks
Siddharth

Josef Moellers

2005-11-30, 3:58 am

siddharthabhishek@gmail.com wrote:
> Hi,
>=20
> i have been trying to rename a file by using following piece of perl
> code
>=20
> use File::Copy;
> move("e:\proj\file1.txt","e:\proj\file2.txt");
>=20
> but my code is throwing an error " Permission Denied"
>=20
> whereas i am able to use copy function , as shown below
> use File::Copy;
> copy("e:\proj\file1.txt","e:\proj\file2.txt");
>=20
> i could easily work with this but my problem is that i dont want both
> the files to exist together. moreover what is bothering me more is why=


> move function is not working??? I am not getting that perfect result
> from my code. :(
>=20
> Please note that i am using Windows 2000 professional as OS and
> activeperl 5.8.7
> IMP note: i am able to rename a file, which is lying in the same
> directory as my perl code, by using move funtion. ( i want to
> implement this code to rename any file from anywhere in the file
> system)


I know little about file and directory permissions on Windows, but maybe =

you do not have permission to delete the source file?

--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

siddharthabhishek@gmail.com

2005-12-02, 9:09 pm

Hi Josef

Thanks for your reply.....but if u are talking about my privilege then
i must tell u that i have admin privilege on the machine so there is no
question of me not having delete permission and also i am able to
delete a file by using my perl code...

unlink (e:\proj\file1.txt);

there must be some other thing which i dont know about MOVE
function.....is there any alternative function which i can use for
renaming a file.????

Regards
Siddharth

Josef Moellers

2005-12-02, 9:09 pm

siddharthabhishek@gmail.com wrote:
> Hi Josef
>=20
> Thanks for your reply.....but if u are talking about my privilege then
> i must tell u that i have admin privilege on the machine so there is no=


> question of me not having delete permission and also i am able to
> delete a file by using my perl code...
>=20
> unlink (e:\proj\file1.txt);
>=20
> there must be some other thing which i dont know about MOVE
> function.....is there any alternative function which i can use for
> renaming a file.????

^^^^^^^^

Hmm, I am not the maintainer of the SAQ list, but how about "rename"?

And, while we're at it, better switch to forward slashes, backslashes=20
have a back^H^Hd habit of disappearing with side-effects in double=20
quoted strings. Perl will handle the forward slashes for you.

--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

Josef Moellers

2005-12-02, 9:09 pm

siddharthabhishek@gmail.com wrote:
> Hi Josef
>=20
> Thanks for your reply.....but if u are talking about my privilege then
> i must tell u that i have admin privilege on the machine so there is no=


> question of me not having delete permission and also i am able to
> delete a file by using my perl code...
>=20
> unlink (e:\proj\file1.txt);


I forgot: As I understand it, Windows has some peculiar ideas about=20
admins and rights. IIRC, admins are by no means the Windows equivalent=20
of root.
But if you can unlink it, you shouild be able to move (or rename) it.
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

Petr Vileta

2005-12-02, 9:09 pm

<siddharthabhishek@gmail.com> píse v diskusním príspevku
news:1133342736.951775.242500@g43g2000cwa.googlegroups.com...
> Hi,
>
> i have been trying to rename a file by using following piece of perl
> code
>
> use File::Copy;
> move("e:\proj\file1.txt","e:\proj\file2.txt");
>
> but my code is throwing an error " Permission Denied"
>

There is 3 ways minimally ;-)

1) use slash, not backslash
move("e:/proj/file1.txt","e:/proj/file2.txt");

2) use ' not "
move('e:\proj\file1.txt', 'e:\proj\file2.txt');

3) use rename, not File::Copy module
rename "e:/proj/file1.txt", "e:/proj/file2.txt";
or
rename 'e:\proj\file1.txt', 'e:\proj\file2.txt';

--

Petr Vileta, Czech republic
(My server reject all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com