Forum: Ruby (Errno::EINVAL) occurs during the File::rename() execution

Posted by Love U Ruby (my-ruby)
on 2013-01-30 11:28
Hi I wrote the below code to rename the file names. The logic is during
renaming file new name should be preceded with the Directory name into
which the file resides.

The code is:

current_dir = Dir.pwd
Dir.chdir('C:\Documents and Settings\rakshiar\My
Documents\userdata\Downloaded Files')
current_dir = Dir.pwd

Dir.entries(current_dir)[2..-1].each do |dir|

  puts dir
  puts "-------"
  Dir.entries(dir)[2..-1].each do |d|

  old_file_path = File.realdirpath(d ,dir)
  puts old_file_path
  file_old_name=File.split(File.realdirpath(d ,dir))[1]
  file_loc_dir=File.split(File.realdirpath(d ,dir))[0]
  @dir_string=dir.inspect
  file_new_name=@dir_string.concat(file_old_name)
  file_new_name=file_new_name.tr_s('"','')
  new_file_path = File.realdirpath(file_new_name ,file_loc_dir)
  puts new_file_path
  File.rename(old_file_path, new_file_path)

  end
  puts "-------"
end

Output:
=======

-------
H76191
-------
C:/Documents and Settings/rakshiar/My Documents/userdata/Downloaded
Files/H76191
/Intergrity letter[1][1].pdf
C:/Documents and Settings/rakshiar/My Documents/userdata/Downloaded
Files/H76191
/H76191Intergrity letter[1][1].pdf
C:/Documents and Settings/rakshiar/My Documents/userdata/Downloaded
Files/H76191
/Intergrity letter[1][1].pdf.part
C:/Documents and Settings/rakshiar/My Documents/userdata/Downloaded
Files/H76191
/H76191Intergrity letter[1][1].pdf.part

****************************************************
Error occurs when the execution came into that part
****************************************************

-------
H76954
-------
C:/Documents and Settings/rakshiar/My Documents/userdata/Downloaded
Files/H76954
/Specjalistyczny Gabinet Kardiologiczny Dr hab. med. Ewa
Straburzy?ska-Migaj.pdf

C:/Documents and Settings/rakshiar/My Documents/userdata/Downloaded
Files/H76954
/H76954Specjalistyczny Gabinet Kardiologiczny Dr hab. med. Ewa
Straburzy?ska-Mig
aj.pdf
C:/Documents and Settings/rakshiar/My
Documents/userdata/Ruby/Scripts/FileNamere
naming.rb:20:in `rename': Invalid argument - (C:/Documents and
Settings/rakshiar
/My Documents/userdata/Downloaded Files/H76954/Specjalistyczny Gabinet
Kardiolog
iczny Dr hab. med. Ewa Straburzy?ska-Migaj.pdf, C:/Documents and
Settings/rakshi
ar/My Documents/userdata/Downloaded Files/H76954/H76954Specjalistyczny
Gabinet K
ardiologiczny Dr hab. med. Ewa Straburzy?ska-Migaj.pdf) (Errno::EINVAL)
        from C:/Documents and Settings/rakshiar/My
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:20:in `block (2 levels) in <main>'
        from C:/Documents and Settings/rakshiar/My
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:9:in `each'
        from C:/Documents and Settings/rakshiar/My
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:9:in `block in <main>'
        from C:/Documents and Settings/rakshiar/My
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:5:in `each'
        from C:/Documents and Settings/rakshiar/My
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:5:in `<main>'

C:\Documents and Settings\rakshiar\My Documents\userdata\Ruby\Scripts>
Posted by Wayne Brisette (Guest)
on 2013-01-30 11:37
(Received via mailing list)
I believe the issue is how the read is being done.

/Specjalistyczny Gabinet Kardiologiczny Dr hab. med. Ewa
Straburzy?ska-Migaj.pdf

The ? tells me that Ruby isn't parsing this correctly (or maybe parsing 
is the
wrong term here encoding I think is a better choice). This looks similar 
to an
encoding issue I recently faced within a text file.

-Wayne
Posted by Love U Ruby (my-ruby)
on 2013-01-30 11:40
Wayne Brisette wrote in post #1094311:
> I believe the issue is how the read is being done.
>
> /Specjalistyczny Gabinet Kardiologiczny Dr hab. med. Ewa
> Straburzy?ska-Migaj.pdf
>
> The ? tells me that Ruby isn't parsing this correctly (or maybe parsing
> is the
> wrong term here encoding I think is a better choice). This looks similar
> to an
> encoding issue I recently faced within a text file.
>
> -Wayne

Okay! Thanks,


But any idea how to deal with that?
Posted by Joel Pearson (virtuoso)
on 2013-01-30 12:32
There's an answer here with an easy hack & slash method:

http://stackoverflow.com/questions/2270635/invalid...
Posted by Love U Ruby (my-ruby)
on 2013-01-30 13:29
Joel Pearson wrote in post #1094324:
> There's an answer here with an easy hack & slash method:
>
> 
http://stackoverflow.com/questions/2270635/invalid...


I suspect if there is any upper-limit on the length of the argument we 
are passing via the File::rename() method. Meanwhile I will give it a 
try.


Thanks,
Posted by Joel Pearson (virtuoso)
on 2013-01-30 13:41
If there's a length limit, try renaming using only the basename and then 
move the file seperately.
Posted by Love U Ruby (my-ruby)
on 2013-01-30 13:43
Joel Pearson wrote in post #1094338:
> If there's a length limit, try renaming using only the basename and then
> move the file seperately.


I tried the below as you linked me:

file_new_name.gsub!(/[\x00\/\\:\*\?\"<>\|]/, '_')


But still the error:

-------
H76954
-------
C:/Documents and Settings/rakshiar/My Documents/userdata/Downloaded 
Files/H76954
/Specjalistyczny Gabinet Kardiologiczny Dr hab. med. Ewa 
Straburzy?ska-Migaj.pdf

C:/Documents and Settings/rakshiar/My Documents/userdata/Downloaded 
Files/H76954
/H76954Specjalistyczny Gabinet Kardiologiczny Dr hab. med. Ewa 
Straburzy_ska-Mig
aj.pdf
C:/Documents and Settings/rakshiar/My 
Documents/userdata/Ruby/Scripts/FileNamere
naming.rb:21:in `rename': No such file or directory - (C:/Documents and 
Settings
/rakshiar/My Documents/userdata/Downloaded Files/H76954/Specjalistyczny 
Gabinet
Kardiologiczny Dr hab. med. Ewa Straburzy?ska-Migaj.pdf, C:/Documents 
and Settin
gs/rakshiar/My Documents/userdata/Downloaded 
Files/H76954/H76954Specjalistyczny
Gabinet Kardiologiczny Dr hab. med. Ewa Straburzy_ska-Migaj.pdf) 
(Errno::ENOENT)

        from C:/Documents and Settings/rakshiar/My 
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:21:in `block (2 levels) in <main>'
        from C:/Documents and Settings/rakshiar/My 
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:9:in `each'
        from C:/Documents and Settings/rakshiar/My 
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:9:in `block in <main>'
        from C:/Documents and Settings/rakshiar/My 
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:5:in `each'
        from C:/Documents and Settings/rakshiar/My 
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:5:in `<main>'

C:\Documents and Settings\rakshiar\My Documents\userdata\Ruby\Scripts>
Posted by Love U Ruby (my-ruby)
on 2013-01-30 14:30
Arup Rakshit wrote in post #1094339:
> Joel Pearson wrote in post #1094338:
>> If there's a length limit, try renaming using only the basename and then
>> move the file seperately.
>
>
> I tried the below as you linked me:
>
> file_new_name.gsub!(/[\x00\/\\:\*\?\"<>\|]/, '_')
>
>

After doing a bit research I found that there is a file called
"Specjalistyczny Gabinet Kardiologiczny Dr hab. med. Ewa
Straburzyńska-Migaj.pdf" in the directory - "H76954". Now when the
script reading,one character - `ń` in the file name converting it
to the `?`. Thus getting the error. Anyway to avoid the such
character encoding problem?
Posted by Wayne Brisette (Guest)
on 2013-01-30 14:39
(Received via mailing list)
Look at the Ruby Gem UNF. I think that should solve your problem.

Wayne


----- Original Message ----
From: Arup Rakshit <lists@ruby-forum.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Wed, January 30, 2013 7:31:13 AM
Subject: Re: (Errno::EINVAL) occurs during the File::rename() execution



After doing a bit research I found that there is a file called
"Specjalistyczny Gabinet Kardiologiczny Dr hab. med. Ewa
Straburzyńska-Migaj.pdf" in the directory - "H76954". Now when the
script reading,one character - `ń` in the file name converting it
to the `?`. Thus getting the error. Anyway to avoid the such
character encoding problem?
Posted by Love U Ruby (my-ruby)
on 2013-01-30 14:50
Wayne Brisette wrote in post #1094354:
> Look at the Ruby Gem UNF. I think that should solve your problem.
>
> Wayne
>
>


Can you do the required change in my code?

I am confused with this,as I had never used it.
Posted by Wayne Brisette (Guest)
on 2013-01-30 14:53
(Received via mailing list)
I found it here:
https://github.com/fog/fog/issues/1294

I've never used it but it looks like all you have to do is: .to_unf

Wayne


----- Original Message ----
From: Arup Rakshit <lists@ruby-forum.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Wed, January 30, 2013 7:50:48 AM
Subject: Re: (Errno::EINVAL) occurs during the File::rename() execution

Wayne Brisette wrote in post #1094354:
> Look at the Ruby Gem UNF. I think that should solve your problem.
>
> Wayne
>
>


Can you do the required change in my code?

I am confused with this,as I had never used it.
Posted by Love U Ruby (my-ruby)
on 2013-01-31 07:26
Wayne Brisette wrote in post #1094356:
> I found it here:
> https://github.com/fog/fog/issues/1294
>
> I've never used it but it looks like all you have to do is: .to_unf
>


Can anyone help me here?

Thanks
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.