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

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
[email protected]_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:inblock (2 levels) in ’
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:inblock in ’
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

C:\Documents and Settings\rakshiar\My Documents\userdata\Ruby\Scripts>

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

Wayne B. 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?

There’s an answer here with an easy hack & slash method:

http://stackoverflow.com/questions/2270635/invalid-chars-filter-for-file-folder-name-ruby

If there’s a length limit, try renaming using only the basename and then
move the file seperately.

Joel P. wrote in post #1094324:

There’s an answer here with an easy hack & slash method:

http://stackoverflow.com/questions/2270635/invalid-chars-filter-for-file-folder-name-ruby

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,

Joel P. 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:ineach’
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:ineach’
from C:/Documents and Settings/rakshiar/My
Documents/userdata/Ruby/Scrip
ts/FileNamerenaming.rb:5:in `’

C:\Documents and Settings\rakshiar\My Documents\userdata\Ruby\Scripts>

Look at the Ruby Gem UNF. I think that should solve your problem.

Wayne

----- Original Message ----
From: Arup R. [email protected]
To: ruby-talk ML [email protected]
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?

Wayne B. 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.

Arup R. wrote in post #1094339:

Joel P. 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?

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 R. removed_e[email protected]
To: ruby-talk ML [email protected]
Sent: Wed, January 30, 2013 7:50:48 AM
Subject: Re: (Errno::EINVAL) occurs during the File::rename() execution

Wayne B. 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.

Wayne B. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs