Moving all files in a folder to another hard drive

This is a discussion on Moving all files in a folder to another hard drive within the RUBY forums in Programming Languages category; 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 ...

Go Back   Application Development Forum > Programming Languages > RUBY

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-24-2008, 08:33 AM
SpringFlowers AutumnMoon
Guest
 
Default Moving all files in a folder to another hard drive

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-forum.com/.

Reply With Quote
  #2  
Old 08-24-2008, 04:52 PM
Dave Bass
Guest
 
Default Re: Moving all files in a folder to another hard drive

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-forum.com/.

Reply With Quote
  #3  
Old 08-24-2008, 07:32 PM
SpringFlowers AutumnMoon
Guest
 
Default 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-forum.com/.

Reply With Quote
  #4  
Old 08-24-2008, 11:16 PM
Mark T
Guest
 
Default Re: Moving all files in a folder to another hard drive

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...)

Reply With Quote
  #5  
Old 08-25-2008, 03:47 AM
SpringFlowers AutumnMoon
Guest
 
Default Re: Moving all files in a folder to another hard drive

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-forum.com/.

Reply With Quote
  #6  
Old 08-25-2008, 05:15 AM
Axel Etzold
Guest
 
Default Re: Moving all files in a folder to another hard drive


-------- 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-forum.com/.


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/libdo...ses/Iconv.html
http://en.wikipedia.org/wiki/Charact...cter_encodings

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/smartsurfer

Reply With Quote
  #7  
Old 08-25-2008, 07:19 AM
SpringFlowers AutumnMoon
Guest
 
Default 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-forum.com/.

Reply With Quote
  #8  
Old 08-25-2008, 09:24 AM
SpringFlowers AutumnMoon
Guest
 
Default Re: Moving all files in a folder to another hard drive

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-forum.com/.

Reply With Quote
  #9  
Old 08-25-2008, 10:08 AM
Axel Etzold
Guest
 
Default Re: Moving all files in a folder to another hard drive


-------- 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-forum.com/.


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.rubyforge.org/) ?


Best regards,

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

Reply With Quote
  #10  
Old 08-25-2008, 12:07 PM
Dave Bass
Guest
 
Default Re: Moving all files in a folder to another hard drive

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

http://en.wikipedia.org/wiki/NTFS

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-forum.com/.

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:29 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.