PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Iterators and arrays
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Iterators and arrays

Réponse
 
LinkBack Outils de la discussion
Vieux 02/04/2008, 10h53   #1
Clement Ow
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Iterators and arrays

I want to use each index of the array and loop it in such a way that the
path names can be passed into Dir so that i can carry out multiple
copies from different sources. unfortunately, it showed error while
trying to run the script. Any suggestions?


$DEST="U:/test1"

source=["C:/movtest/testing", "C:/movtest", "U:/movtest",
"U:/movtest/new", "U:/movtest/new1"]
j=source.length - 1
i= -1

while i<j

Dir.chdir(source[i])
print Dir.getwd
FileUtils.cp_r Dir.glob('2008*'), $DEST
i+=1

end
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 02/04/2008, 12h17   #2
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Iterators and arrays

On Wed, Apr 2, 2008 at 11:53 AM, Clement Ow
<clement.ow@asia.bnpparibas.com> wrote:
> I want to use each index of the array and loop it in such a way that the
> path names can be passed into Dir so that i can carry out multiple
> copies from different sources. unfortunately, it showed error while
> trying to run the script. Any suggestions?
>
>
> $DEST="U:/test1"
>
> source=["C:/movtest/testing", "C:/movtest", "U:/movtest",
> "U:/movtest/new", "U:/movtest/new1"]
> j=source.length - 1
> i= -1
>
> while i<j
>
> Dir.chdir(source[i])
> print Dir.getwd
> FileUtils.cp_r Dir.glob('2008*'), $DEST
> i+=1
>
> end
> --
> Posted via http://www.ruby-forum.com/.
>
>

You seem to get confused with the indexes, but the idiomatic Ruby code
would rather look like

dest = "u:/..."
sources = %w{ c:/movetest/testing c:/ ... } # unless you have spaces
in the pathes
sources.each do | source |
Dir.chdir source
puts source
FileUtiles.cp_r Dir.glob(...), dest # if this is what you really want
end

HTH
Robert
--
http://ruby-smalltalk.blogspot.com/

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

  Réponse avec citation
Vieux 03/04/2008, 07h39   #3
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Iterators and arrays

On 02.04.2008 13:17, Robert Dober wrote:
> On Wed, Apr 2, 2008 at 11:53 AM, Clement Ow
> <clement.ow@asia.bnpparibas.com> wrote:
>> I want to use each index of the array and loop it in such a way that the
>> path names can be passed into Dir so that i can carry out multiple
>> copies from different sources. unfortunately, it showed error while
>> trying to run the script. Any suggestions?
>>
>>
>> $DEST="U:/test1"
>>
>> source=["C:/movtest/testing", "C:/movtest", "U:/movtest",
>> "U:/movtest/new", "U:/movtest/new1"]
>> j=source.length - 1
>> i= -1
>>
>> while i<j
>>
>> Dir.chdir(source[i])
>> print Dir.getwd
>> FileUtils.cp_r Dir.glob('2008*'), $DEST
>> i+=1
>>
>> end
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>

> You seem to get confused with the indexes, but the idiomatic Ruby code
> would rather look like
>
> dest = "u:/..."
> sources = %w{ c:/movetest/testing c:/ ... } # unless you have spaces
> in the pathes
> sources.each do | source |
> Dir.chdir source
> puts source
> FileUtiles.cp_r Dir.glob(...), dest # if this is what you really want
> end


Inside the block I prefer

Dir.chdir source do
puts source
FileUtiles.cp_r Dir.glob(...), dest
end

which will restore the original current directory. That way, you can
more easily work with relative paths and it might save you from
surprises when in a completely unrelated location of a larger script
current dir is no longer the one that it was on program start. :-)

Kind regards

robert
  Réponse avec citation
Vieux 03/04/2008, 08h13   #4
Clement Ow
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Iterators and arrays

Robert Klemme wrote:
> On 02.04.2008 13:17, Robert Dober wrote:
>>> source=["C:/movtest/testing", "C:/movtest", "U:/movtest",
>>>

>> in the pathes
>> sources.each do | source |
>> Dir.chdir source
>> puts source
>> FileUtiles.cp_r Dir.glob(...), dest # if this is what you really want
>> end

>
> Inside the block I prefer
>
> Dir.chdir source do
> puts source
> FileUtiles.cp_r Dir.glob(...), dest
> end
>
> which will restore the original current directory. That way, you can
> more easily work with relative paths and it might save you from
> surprises when in a completely unrelated location of a larger script
> current dir is no longer the one that it was on program start. :-)
>
> Kind regards
>
> robert


The code is actually feasible but for a typo mistake!(Cause there wasnt
a dir named U:\test1) Here's the edited code:

dest=%w[U:/test_1 U:/dest1 U:/dest2 U:/dest3 U:/dest4]
j1=dest.length - 1
i1= 0
source=%w[C:/movtest/testing C:/movtest/testing/new U:/movtest/source
U:/movtest/new U:/movtest/new1]
j=source.length - 1
i= 0
while i<=j && i1<=j1

Dir.chdir(source[i])
print Dir.getwd
print dest[i1]
FileUtils.cp_r Dir.glob('2008*'), dest[i1]
i+=1
i1+=1
end

But is there anyway whereby, I can write all the 'print' to a log file
for reference, just in case there might be an error in copying? And if
possible have some conditions to test if the files did really copy over?
Thanks.

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h32.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14537 seconds with 12 queries