[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Moving all files in a folder to another hard drive

SpringFlowers AutumnMoon

8/24/2008 12:34:00 PM

I have some code below to move all files in a folder to another hard
drive (which has 2TB of space). It runs well except that whenever a
filename has some international characters, then the line

if File.file?(basedir + file)

will fail. The file is printed as "Chart for ???????.xls"

Does someone know how to solve this problem with Ruby being so powerful?
The program is running on Windows. (Vista or XP should both be ok).

code:
------------------------------------

require 'ftools'

basedir = "c:/data/"
target = "w:/data/"

Dir.chdir(basedir)
files = Dir.glob("*");

i = 1
files.each { |file|
p i, file
if File.file?(basedir + file)
puts "Moving..."
File.move(basedir + file, target + file)
puts "Now sleeping..."
sleep(60)
end
i += 1
}
--
Posted via http://www.ruby-....

13 Answers

Dave Bass

8/24/2008 8:53:00 PM

0

Presumably the file names are being corrupted at some point. It could be
in Ruby, or it could be in Windows. My approach would be to put some
print statements in to find out where exactly the problem lies.
--
Posted via http://www.ruby-....

SpringFlowers AutumnMoon

8/24/2008 11:33:00 PM

0

Dave Bass wrote:
> Presumably the file names are being corrupted at some point. It could be
> in Ruby, or it could be in Windows. My approach would be to put some
> print statements in to find out where exactly the problem lies.

actually, if i use

files.each { |file|
p i, file
file.each_byte {|c| print c, ' ' }
[...]

then the filename print out as a lot of 63, which is the ASCII of "?",
so it looks like the filenames already come back bad...

using $KCODE = "u"; or ruby -Ku move.rb doesn't seem to help. They
seem to be only indicating the file containing the code uses UTF-8
encoding.



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

Mark T

8/25/2008 3:16:00 AM

0

Is the reason you are moving the files something to do with
instability of the present volume?
The "????????" is a clue.
It may be that the fat table has been corrupted at the entry being read.
Skip this entry.
Get what you can.
If the remainder is valuable, pay someone to recover it.

Good luck. (Not really joking, seriously, well, not real serious if ya
know.... blabla...)

SpringFlowers AutumnMoon

8/25/2008 7:47:00 AM

0

Mark T wrote:
> Is the reason you are moving the files something to do with
> instability of the present volume?
> The "????????" is a clue.
> It may be that the fat table has been corrupted at the entry being read.
> Skip this entry.

oh thanks for your reminder. actually, whenever any filename has
international characters in it, then the filename will have ???? as
well... it happens to any folder and happens to my other computer too
which has RAID Mirroring. I think somehow, the international character
didn't get thru into a Ruby string.
--
Posted via http://www.ruby-....

Axel Etzold

8/25/2008 9:16:00 AM

0


-------- Original-Nachricht --------
> Datum: Mon, 25 Aug 2008 08:32:48 +0900
> Von: SpringFlowers AutumnMoon <summercoolness@gmail.com>
> An: ruby-talk@ruby-lang.org
> Betreff: Re: Moving all files in a folder to another hard drive

> Dave Bass wrote:
> > Presumably the file names are being corrupted at some point. It could be
> > in Ruby, or it could be in Windows. My approach would be to put some
> > print statements in to find out where exactly the problem lies.
>
> actually, if i use
>
> files.each { |file|
> p i, file
> file.each_byte {|c| print c, ' ' }
> [...]
>
> then the filename print out as a lot of 63, which is the ASCII of "?",
> so it looks like the filenames already come back bad...
>
> using $KCODE = "u"; or ruby -Ku move.rb doesn't seem to help. They
> seem to be only indicating the file containing the code uses UTF-8
> encoding.
>
>
>
> --
> Posted via http://www.ruby-....

Have you tried to use Iconv to convert between encodings ?
In editors, texts full of questions marks suddenly become readable, if the right encoding is
chosen....

http://www.ruby-doc.org/stdlib/libdoc/iconv/rdoc/classes/...
http://en.wikipedia.org/wiki/Character_encoding#Popular_character...

Best regards,

Axel

--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/s...

SpringFlowers AutumnMoon

8/25/2008 11:19:00 AM

0

no matter which of the two methods is used:

files = Dir.new(basedir).entries

Dir.chdir(basedir)
files = Dir.glob("*");

then if i do

files.each { |file|
p i, file
file.each_byte {|c| print c, ' ' }

then whenever the filename has international characters, then the ASCII
code 63 is printed out a lot, meaning it is "?". I wonder is it true
for Japanese version of Ruby too? Does it actually get back UTF-8 code
or JIS code?

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

SpringFlowers AutumnMoon

8/25/2008 1:24:00 PM

0

so I thought Ruby 1.9 differ from Ruby 1.8 as Ruby 1.9's String doesn't
have to be ASCII... but if I use the following using Ruby 1.9, I still
get the 63 ASCII denoting "?" in the filenames.


basedir = "c:/data/"

i = 0
Dir.new(basedir).entries.each { |file|
p i, file
file.each_byte {|c| print c, ' ' }

if (i > 10)
break
end
i += 1
}
--
Posted via http://www.ruby-....

Axel Etzold

8/25/2008 2:09:00 PM

0


-------- Original-Nachricht --------
> Datum: Mon, 25 Aug 2008 20:19:27 +0900
> Von: SpringFlowers AutumnMoon <summercoolness@gmail.com>
> An: ruby-talk@ruby-lang.org
> Betreff: Re: Moving all files in a folder to another hard drive

> no matter which of the two methods is used:
>
> files = Dir.new(basedir).entries
>
> Dir.chdir(basedir)
> files = Dir.glob("*");
>
> then if i do
>
> files.each { |file|
> p i, file
> file.each_byte {|c| print c, ' ' }
>
> then whenever the filename has international characters, then the ASCII
> code 63 is printed out a lot, meaning it is "?". I wonder is it true
> for Japanese version of Ruby too? Does it actually get back UTF-8 code
> or JIS code?
>
> --
> Posted via http://www.ruby-....

As far as I know, East Asian encodings use more than one bit to store a character
(due to the huge amout of characters, they wouldn't all fit into 256 places).
This might explain why you get ? when you write each_byte there.

I am not on Windows right now, so I am sure whether your international files
all get copied to ?(repeat x times) or whether they are copied to names which
are not correctly displayed in the file browser.

What you could do is CGI.escape and (maybe later CGI.unescape) them:

require "cgi"

files.each { |file|
p CGI.escape(file)
# and then move files
}

That's not elegant, but it will produce names with only % and ASCII letters.


How does rio behave under Windows (http://rio.ruby...) ?


Best regards,

Axel
--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/...

Dave Bass

8/25/2008 4:08:00 PM

0

Presumably you're using the NTFS filesystem? There's some information on
it here:

http://en.wikipedia.org...

Since the filesystem has a closed specification (thank you Microsoft),
it may be that the Ruby developers have been unable to work out exactly
how it works with filenames that contain Unicode characters.



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

M. Edward (Ed) Borasky

8/26/2008 1:34:00 AM

0

On Tue, 2008-08-26 at 01:07 +0900, Dave Bass wrote:
> Presumably you're using the NTFS filesystem? There's some information on
> it here:
>
> http://en.wikipedia.org...
>
> Since the filesystem has a closed specification (thank you Microsoft),
> it may be that the Ruby developers have been unable to work out exactly
> how it works with filenames that contain Unicode characters.

Yes ... my advice is to "shell out" to Windows or call native C
libraries, rather than trying to "reverse engineer" NTFS.
>
>
>
--
M. Edward (Ed) Borasky
ruby-perspectives.blogspot.com

"A mathematician is a machine for turning coffee into theorems." --
Alfréd Rényi via Paul ErdÅ?s