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 9209443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:50:38+00:00 2026-06-18T00:50:38+00:00

From the current working directory I am trying to rename a file which is

  • 0

From the current working directory I am trying to rename a file which is in another directory. My test IRB code is below:

C:\Documents and Settings\peter>irb
irb(main):001:0> Dir.pwd
=> "C:/Documents and Settings/peter"
irb(main):002:0> File.rename('C:\Documents and Settings\peter\My Documents\userdata\test.txt','a.txt')
=> 0
irb(main):003:0>File.exist?('C:\Documents and Settings\peter\My Documents\userdata\test.txt')
=> false
irb(main):004:0>File.exist?('C:\Documents and Settings\peter\My Documents\userdata\a.txt')
=> false
irb(main):005:0>

Instead of renaming the target file is getting deleted from the file system – why so? If it is not the right approach to rename a file, please advice me a safe approach. But I can’t change the current working directory for renaming, and to rename if I need to change the current directory,after renaming I want to get back the previous current working directory.

  • 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-06-18T00:50:40+00:00Added an answer on June 18, 2026 at 12:50 am

    First, you should not use backslashes here. The reason for this is that backslash is an escape sequence initiator, so you might get very unexpected results next time (if you accidentally use backslashes in a double-quoted string, for example). In this case there were no problems, but it’s a dumb luck. Always use forward slashes in file paths. ruby on windows should handle them just fine.

    Second, you’re looking in the wrong place. Renamed file should appear at

    C:/Documents and Settings/peter/a.txt
    

    Because this is your current working dir. If you want it to appear in “C:/Documents and Settings/peter/My Documents/userdata” either change the working dir before renaming, or specify a full target file path.

    File.rename('C:/Documents and Settings/peter/My Documents/userdata/test.txt',
                'C:/Documents and Settings/peter/My Documents/userdata/a.txt')
    

    You can also use a block with Dir::chdir.

    Dir.chdir('C:/Documents and Settings/peter/My Documents/userdata') do
      File.rename('test.txt', 'a.txt')
    end
    

    After block returns, working dir is restored to whatever it was before chdir call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to copy a file (pdftk.exe) from the Current Working Directory the VB.NET
How can I change the current working directory from within a Java program? Everything
I found the C snippet to get the current working directory from here .
I am trying to replace some content in a file with the current working
I am using File::Find in the code below to find the files from /home/user/data
Trying to run gvim 7.3 from the working directory (project location) seems impossible-- it
Here is my current, working, query: var lsFooterRow = from i in _context.Inventory where
I'm trying to load my UITableView with distance from current location. I'm taking small
For various reasons (code review mostly) I need to switch from current development branch
While traversing from my current path , and searching for a file , I'm

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.