Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:55:47+00:00 2026-05-27T11:55:47+00:00

i have this program in ruby version 1.9.3p0 that renames files passed from the

  • 0

i have this program in ruby version 1.9.3p0 that renames files passed from the shell (send to) based on regular expression conditions. Works fine except when special characters are in the filename.

ruby RegExpRename.rb "32. Esther Verhoef - Déjà vu.epub"

gives error

/RegExpRename.rb:29:in `rename': No such file or directory - (32. Esther Verhoef - DÚjÓ vu.epub, test) (Errno::ENOENT)
from C:/Users/peter/AppData/Roaming/Microsoft/Windows/SendTo/RegExpRename.rb:29:in `block in <main>'
from C:/Users/peter/AppData/Roaming/Microsoft/Windows/SendTo/RegExpRename.rb:18:in `each'
from C:/Users/peter/AppData/Roaming/Microsoft/Windows/SendTo/RegExpRename.rb:18:in `<main>'

The encoding is the culpit, my windows cp is CP850, déjà is changed in DÚjÓ
I don’t know how to solve this, please help.
Here the program

# encoding: CP850
require 'find'
require 'fileutils'
require 'Win32API'

def get_long_win32_filename(short_name)
  max_path = 1024
  long_name = " " * max_path
  lfn_size = Win32API.new("kernel32", "GetLongPathName", ['P','P','L'],'L').call(short_name, long_name, max_path)
  return (1..max_path).include?(lfn_size) ? long_name[0..lfn_size-1] :  short_name
end

ARGV.each do|a|
  long = get_long_win32_filename(a)
  result = long.gsub(/(\w+)\s*,\s*([^\-]+)\s*\-\s*(.+\.[a-zA-Z]{3,4})/i, "\\2 \\1 - \\3")
  if result == long
    result = long.gsub(/(\d+\. +)(.*)/i, "\\2")
  end

  if result != long
    if File.exist?(result)
      puts "File #{result} already exists.  Not renaming."
    else
      File.rename(a, long)
      puts long + " ===> " + result
    end
  else
    puts long_name + " don't have to be renamed"
  end
end
sleep 15
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:55:48+00:00Added an answer on May 27, 2026 at 11:55 am

    Pfew.. Don’t know whether i should be happy that i finally found it or be sad that ruby makes it so difficult.
    Anyhow, here is the sollution.

    Problem is when a rubyscript gets a parameter in Windows7 command-prompt (or shell) it gets this (in my situation) encoded as CP1252, i have no idea why. So if i change my CP with CHCP 1252 the script runs fine, but that is no solution of course, all special characters are printed wrong.

    Solution: I tell ruby the encoding IS CP1252 and convert is to my locale CP (CP850 in my case)
    I wish the CP1252 could be detected and not hardcoded so that is a more generic solution.
    Any suggestions ?

    Here the program I used to test this out, I pass the argument “déjà.epub”, the file IS being renamed like this.

    a = ARGV[0].dup.encode(Encoding.locale_charmap)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Ruby program that zips a directory tree of XML files using
Scenario: I have to call an external program from my Ruby script, and this
Ok, suppose I have Ruby program to read version control log files and do
I've looked at this and it wasn't much help. I have a Ruby program
I have this program that should execute a piece of code base on the
So I have this program that I really like, and it doesn't support Applescript.
I have this little program I write in ruby. I found a nice piece
I have the following Ruby program: class Access def retrieve_public puts This is me
I have a Ruby program that takes as input an HTML document. The structure
I'm just learning to program and have decided to try Ruby. I'm sure this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.