Dir copy with rename

R

Rebhan, Gilbert

Hi,

i want to copy a dir recursive with all files included,=20
but rename all dirnames that match a pattern, i.e.

/srcdir
/bla/foobar/blabla
/bla/test/subtest/foobar
/foobar/test
/foobar

should be=20

/targetdir
/bla/foobaz/blabla
/bla/test/subtest/foobaz
/foobaz/test
/foobaz

i tried =3D

re=3D/foobar/
sub=3D"foobaz"

Dir['Y:/test/**/**'].each { |old|
next if old =3D=3D '.'||old=3D=3D '..'||old =3D~ /\./
unless /re/.match(old)
new =3D old.sub(re,sub)
File.rename(old,new)
end
}



the /foobar folder on first level and on second level get renamed, but
it dosn't
work, when i have /foobar on the third level, means

/foobar/foobar/foobar gets=20
/foobaz/foobaz/foobar and Exit Code 1

/test/sub3/foobar/foobar gets
/test/sub3/foobaz/foobar and Exit Code 1

Error message i.e. =3D
No such file or directory - Y:/test/sub3/foobar/foobar or
Y:/test/sub3/foobaz/foobar



Any ideas what's wrong ?

Regards, Gilbert
 
M

Mariusz Pękala

--9amGYk9869ThD9tj
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=20
Hi,
=20
i want to copy a dir recursive with all files included,=20
but rename all dirnames that match a pattern, i.e.
=20
/srcdir
/bla/foobar/blabla
/bla/test/subtest/foobar
/foobar/test
/foobar
=20
should be=20
=20
/targetdir
/bla/foobaz/blabla
/bla/test/subtest/foobaz
/foobaz/test
/foobaz
=20
i tried =3D
=20
re=3D/foobar/
sub=3D"foobaz"
=20
Dir['Y:/test/**/**'].each { |old|
next if old =3D=3D '.'||old=3D=3D '..'||old =3D~ /\./
unless /re/.match(old)
new =3D old.sub(re,sub)
File.rename(old,new)
end
}
=20
the /foobar folder on first level and on second level get renamed, but
it dosn't
work, when i have /foobar on the third level, means
=20
/foobar/foobar/foobar gets=20
/foobaz/foobaz/foobar and Exit Code 1
=20
/test/sub3/foobar/foobar gets
/test/sub3/foobaz/foobar and Exit Code 1
=20
Error message i.e. =3D
No such file or directory - Y:/test/sub3/foobar/foobar or
Y:/test/sub3/foobaz/foobar
=20
Any ideas what's wrong ?

Are you sure the code you posted here is the code you really run?

I guess you want 'if' instead of 'unless', because you want to rename
the file if the re matches, not if it does not.

Second - you should use re.match(old) not /re/.match(old) since 're'
variable is already a Regexp.

Having these errors I doubt you would have any file renamed.

--=20
Ceterum censeo Internet Explorer esse delendam.

--9amGYk9869ThD9tj
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7-ecc0.1.6 (GNU/Linux)

iD8DBQFGcQnMsnU0scoWZKARAkxJAJ9b20ILBTlCv6LtK5GpxAVoLY8zFwCgm94Z
6RmwQYN8Y4jM+C3wVBO4LTc=
=Lapl
-----END PGP SIGNATURE-----

--9amGYk9869ThD9tj--
 
R

Rebhan, Gilbert

=20
Hi,

-----Original Message-----
From: Mariusz Pekala [mailto:[email protected]]=20
Sent: Thursday, June 14, 2007 11:26 AM
To: ruby-talk ML
Subject: Re: Dir copy with rename
Dir['Y:/test/**/**'].each { |old|
next if old =3D=3D '.'||old=3D=3D '..'||old =3D~ /\./
unless /re/.match(old)
new =3D old.sub(re,sub)
File.rename(old,new)
end
}

now i have=20

src=3D"Y:/test"

re=3D/foobar/
sub=3D"foobaz"

Dir[src+'/**/**/**'].each { |old|
next if old =3D=3D '.'||old=3D=3D '..'||old =3D~ /\./
puts old
if re.match(old)=20
new =3D old.sub(re,sub)
File.rename(old,new)
end
}


when i use Dir[src] the /foobar folder on first
level, means Y:/test/foobar gets Y:/test/foobaz and Exit 0

but when i use Dir[src+'/**'], because i want to recurse
in all subfolders

the folder on first level gets renamed and then i get an error =3D
ruby dircopy.rb
Y:/test/sub3
Y:/test/sub1
Y:/test/foobar
Y:/test/sub3/foobar
Y:/test/sub3/foobar/foobar
dircopy.rb:14:in `rename':
No such file or directory - Y:/test/sub3/foobar/foobar or
Y:/test/sub3/foobaz/foobar (Errno::ENOENT)


Regards, Gilbert
 
R

Robert Klemme

Hi,

-----Original Message-----
From: Mariusz Pekala [mailto:[email protected]]
Sent: Thursday, June 14, 2007 11:26 AM
To: ruby-talk ML
Subject: Re: Dir copy with rename
Dir['Y:/test/**/**'].each { |old|
next if old == '.'||old== '..'||old =~ /\./
unless /re/.match(old)
new = old.sub(re,sub)
File.rename(old,new)
end
}

now i have

src="Y:/test"

re=/foobar/
sub="foobaz"

Dir[src+'/**/**/**'].each { |old|
next if old == '.'||old== '..'||old =~ /\./
puts old
if re.match(old)
new = old.sub(re,sub)
File.rename(old,new)
end
}


when i use Dir[src] the /foobar folder on first
level, means Y:/test/foobar gets Y:/test/foobaz and Exit 0

but when i use Dir[src+'/**'], because i want to recurse
in all subfolders

the folder on first level gets renamed and then i get an error =
ruby dircopy.rb
Y:/test/sub3
Y:/test/sub1
Y:/test/foobar
Y:/test/sub3/foobar
Y:/test/sub3/foobar/foobar
dircopy.rb:14:in `rename':
No such file or directory - Y:/test/sub3/foobar/foobar or
Y:/test/sub3/foobaz/foobar (Errno::ENOENT)

Your basic flaw seems to be that although the subject talks about "copy
with rename" your code actually only renames. That will lead to errors
because you change the original directory tree and thus a file further
down the hierarchy of a renamed folder does not exist any more with the
original file name - hence you cannot rename it. Note that Dir[] first
generates the full list of files, so any changes you do to the
filesystem during iteration will not be reflected in the list.

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,267
Latest member
WaylonCogb

Latest Threads

Top