# Output file name based on original file name

**URL:** <https://www.ruby-forum.com/t/output-file-name-based-on-original-file-name/177369>\
**Category:** Ruby\
**Created:** [October 15, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/output-file-name-based-on-original-file-name/177369 "2009-10-15T19:44:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 15, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/output-file-name-based-on-original-file-name/177369/1 "2009-10-15T19:44:23Z")

</div>

I have a simple txt parsing script that I’d like to have the output be  
named after the original text file.

original = original.txt  
output = original\_output.txt

I’m just doing a simple file.open, and couldn’t find anything.

File.open(‘output.txt’, ‘w’) do |f2|  
File.readlines(“original.txt”).each do |line|

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 15, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/output-file-name-based-on-original-file-name/177369/2 "2009-10-15T19:54:20Z")

</div>

Collin M. wrote:

> Thanks in advance for any help.

This is the simplest…  
output\_file = “#{input\_file}\_output.txt”

On the other hand, you can look at the FileUtils to see how to get the  
base name or the name without extension, etc… so that you can make it  
more productive. I’m just suggesting the simplest.

Cheers,  
Mohit.  
10/16/2009 | 1:53 AM.

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 15, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/output-file-name-based-on-original-file-name/177369/3 "2009-10-15T23:54:21Z")

</div>

Hi,

Am Freitag, 16. Okt 2009, 02:44:38 +0900 schrieb Collin M.:

> I’m just doing a simple file.open, and couldn’t find anything.
> 
> File.open(‘output.txt’, ‘w’) do |f2|  
> File.readlines(“original.txt”).each do |line|

Do you get the below error message?

irb(main):001:0\> File.exists? “nonexistent”  
=\> false  
irb(main):002:0\> File.read “nonexistent”  
Errno::ENOENT: No such file or directory - nonexistent  
from (irb):2:in `read’  
from (irb):2  
irb(main):003:0\>

Bertram
