# Newbie - File Rename

**URL:** <https://www.ruby-forum.com/t/newbie-file-rename/72070>\
**Category:** Ruby\
**Created:** [October 13, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/newbie-file-rename/72070 "2006-10-13T23:10:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [October 13, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/newbie-file-rename/72070/1 "2006-10-13T23:10:19Z")

</div>

I am obviously missing something.

I am simple trying to rename some files with long names to a shorter  
format.

Here are a couple of sample file names: longfilename - 0100.txt,  
longfilename - 01-1.txt

Here is my code - rename.rb:

# Quick Script to rename files

puts ‘start’  
require ‘find’  
require ‘ftools’  
require ‘enumerator’

dir1original = ‘c:/temp/’

puts ‘Here’

Find.find(dir1original) do |path|  
puts ‘The current item is ’ + path  
if File.file? path  
puts path + ’ is a file’  
oldname = path  
newname = path  
newname.sub!(‘longfilename - 0’, ‘0’)  
puts newname + ’ Is the newfile’  
puts “\n\nCheck About to RENAME file”  
File.rename(oldname, newname)  
puts 'Renamed ’ + oldname + ’ to ’ + newname + “\n\n\n”  
end  
end

puts “\n\n”

When I run it I get the following error (after I get teh Check About to  
RENAME file message:  
rename.rb:21:in ‘rename’: No such file or directory - c:/temp/0101.txt  
or c:/temp/0101.txt Errno::ENOENT  
from rename.rb:21  
from c:/ruby/lib/ruby/1.8/find.rb:39:in ‘find’  
from c:/ruby/lib/ruby/1.8/find.rb:38:in ‘catch’  
from c:/ruby/lib/ruby/1.8/find.rb:38:in ‘find’  
from rename.rb:12

My script is named rename.rb and is located on c:  
This is on a Windows XP box.

Not sure what I have missed, but I figure it has to be something simple.

Help would be greatly appreciated.

Snoopy

---

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [October 14, 2006, 3:56am UTC](https://www.ruby-forum.com/t/newbie-file-rename/72070/2 "2006-10-14T03:56:54Z")

</div>

Hi,

At Sat, 14 Oct 2006 06:10:23 +0900,  
Snoopy D. wrote in [ruby-talk:219613]:

> if File.file? path  
> puts path + ’ is a file’  
> oldname = path  
> newname = path.sub(‘longfilename - 0’, ‘0’)  
> puts newname + ’ Is the newfile’  
> puts “\n\nCheck About to RENAME file”  
> File.rename(oldname, newname)  
> puts 'Renamed ’ + oldname + ’ to ’ + newname + “\n\n\n”  
> end

Assigning path to newpath doesn’t create a new string, but  
makes the latter to share the same string with the former and  
oldname.

---

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [October 16, 2006, 11:39am UTC](https://www.ruby-forum.com/t/newbie-file-rename/72070/3 "2006-10-16T11:39:08Z")

</div>

Snoopy D. wrote:

> > > ```
> > > puts "\n\nCheck About to RENAME file"
> > > File.rename(oldname, newname)
> > > 
> > > ```
> 
> …
> 
> Just got back and tried this.
> 
> Same ERROR.
> 
> Any other ideas? Did I miss something on the install?

What about trying `require "fileutils"', at the top, and then using`FileUtils.move(oldname, newname)’ instead?  
Especially on windows I find File.rename to be a bit flaky sometimes,  
and am not sure if it will do the right thing with absolute paths.

---

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [October 16, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/newbie-file-rename/72070/4 "2006-10-16T18:12:42Z")

</div>

Hi,

At Mon, 16 Oct 2006 13:31:18 +0900,  
Snoopy D. wrote in [ruby-talk:219939]:

> > ```
> > newname = path.sub('longfilename - 0', '0')
> > 
> > ```

> Same ERROR.

Are you sure there is no exclamation mark?

---

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [October 16, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/newbie-file-rename/72070/5 "2006-10-16T19:47:24Z")

</div>

unknown wrote:

> Hi,
> 
> At Mon, 16 Oct 2006 13:31:18 +0900,  
> Snoopy D. wrote in [ruby-talk:219939]:
> 
> > > ```
> > > newname = path.sub('longfilename - 0', '0')
> > > 
> > > ```
> 
> > Same ERROR.
> 
> Are you sure there is no exclamation mark?

I had originally missed that.

With some additional “puts” statements I found that mistake. Still  
errored out.

Went back and tried the File.rename and now it works.

I think it might have needed the ‘require “fileutils”’ statement.

Frustrating, but now working.

THANKS TO ALL WHO HELPED OUT.

Snoopy

---

<div class="post-metadata">

**Author:** ![snoopy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snoopy](https://www.ruby-forum.com/u/snoopy)\
**Post date:** [October 16, 2006, 6:31am UTC](https://www.ruby-forum.com/t/newbie-file-rename/72070/6 "2006-10-16T06:31:15Z")

</div>

Nobuyoshi N. wrote:

> Hi,
> 
> At Sat, 14 Oct 2006 06:10:23 +0900,  
> Snoopy D. wrote in [ruby-talk:219613]:
> 
> > if File.file? path  
> > puts path + ’ is a file’  
> > oldname = path  
> > newname = path.sub(‘longfilename - 0’, ‘0’)  
> > puts newname + ’ Is the newfile’  
> > puts “\n\nCheck About to RENAME file”  
> > File.rename(oldname, newname)  
> > puts 'Renamed ’ + oldname + ’ to ’ + newname + “\n\n\n”  
> > end
> 
> Assigning path to newpath doesn’t create a new string, but  
> makes the latter to share the same string with the former and  
> oldname.

Just got back and tried this.

Same ERROR.

Any other ideas? Did I miss something on the install?
