Output file from variable

Hello Everyone

I am hoping someone can help me. I have just started learning ruby and i
am in the process of creating a script that will rename files based on
some of the file contents. So far i have got what i want but need the
file to be renamed with a variable but cannot for the life of me find
out how to simply rename the original input file. Below is an example of
what i mean.

x = File.readlines(ARGV[0])
random code…
filename = “filetobenamed”

So now i want to rename the original input file with something like
file.name("#{filename}")

One idea i thought of was to just create a new file with the contents of
x but even that was proving more difficult than i expected.

Any ideas would be greatly appreciated?
Iv searched around for hours and tried various methods but cannot find a
solution.

Thanks :slight_smile:

On Sat, Dec 31, 2011 at 12:23 AM, Alex S. [email protected]
wrote:

Hello Everyone

I am hoping someone can help me. I have just started learning ruby

Welcome :slight_smile:

Thanks :slight_smile:

I think I found it.

The “Pickaxed” Ruby 1.9 book mentions File.rename as a class method
(page 504).

A little test:

[email protected]:~$ vim original # just made a 1 line file
[email protected]:~$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]
[email protected]:~$ irb

004:0> File.open(“original”) do |f|
005:1* puts f.readline
006:1> end
my file
=> nil
007:0> File.rename(“original”, “new_name”)
=> 0
008:0> File.open(“new_name”) do |f|
009:1* puts f.readline
010:1> end
my file
=> nil
011:0> quit
[email protected]:~$ ls -l | grep new_ # so it is really renamed
-rw-r–r-- 1 peterv peterv 8 2011-12-31 01:56 new_name

Actually, a Google search for “ruby file rename”,
brings me to

http://ruby-doc.org/core-1.9.3/File.html#method-c-rename

Good luck with Ruby :slight_smile:

Peter

Beside what Peter has described, one more hint: you probably need to
close your file before calling the rename method.

You can close your file by calling x.close

On Dec 30, 2011, at 8:31 PM, Alex S. wrote:

Thank you very much for the reply but for your solution to work i would
need the file name of the original file being read. I am inputing my
file via ARGV[0] so i cannot specify the original filename. Seeing as
the variable x contains the entire file is there a way i can output x
into a new file and name the file with a variable?
I simply want a way to rename the input file :frowning:

ARGV[0] is the name of the original file so:

File.rename(ARGV[0], new_filename)

should work.

Gary W.

Thanks you Gary it works now. HOWEVER one last thing I cannot get
working. When i run the file from the windows command prompt:
“c:/windows/ruby file.rb file”
it works perfect but i have a batch file that puts the file into the
script and it gives the error Permission Denied Errno::EACCES. I suspect
this is due to the file not being closed when it tries to rename it. The
.close methods produce the error undefinded method ‘close’ for Array.
This has been taking me hours to try and solve! Anyone know any batch
file tricks?
Thanks for all the help so far guys. After all my work its turning out
windows is giving me the most trouble…

Hello Peter

Thank you very much for the reply but for your solution to work i would
need the file name of the original file being read. I am inputing my
file via ARGV[0] so i cannot specify the original filename. Seeing as
the variable x contains the entire file is there a way i can output x
into a new file and name the file with a variable?
I simply want a way to rename the input file :frowning:

Other than this hickup i am enjoying Ruby quite a lot and have managed
to make it do everything i want so far.

Thank you :slight_smile:

On Sat, Dec 31, 2011 at 2:28 AM, Yong Li [email protected] wrote:

Beside what Peter has described, one more hint: you probably need to
close your file before calling the rename method.

You can close your file by calling x.close

You are correct, but I think it is better to advise the OP
to open the File in a block, as I demonstrated:

004:0> File.open(“original”) do |f|
005:1* puts f.readline
006:1> end

The main reason is that this will still close the file even if
an exception would occur (see IO.open for documentation).

Peter

On Sat, Dec 31, 2011 at 7:42 AM, Alex S. [email protected]
wrote:

Thanks you Gary it works now. HOWEVER one last thing I cannot get
working. When i run the file from the windows command prompt:
“c:/windows/ruby file.rb file”
it works perfect but i have a batch file that puts the file into the
script and it gives the error Permission Denied Errno::EACCES. I suspect
this is due to the file not being closed when it tries to rename it. The
.close methods produce the error undefinded method ‘close’ for Array.

If you still think this may be the case (of the file not being closed
properly before rename), what happens when you run my example
code exactly:

File.open(“original”) do |f|
puts f.readline
end # this automatically closes the file (because a block is used)

File.rename(“original”, “new_name”)

File.open(“new_name”) do |f|
puts f.readline
end

Sorry, I could not test this on Windows myself, but I assume it should
work
?

Peter

Hello Peter and Wayne.

Thanks for the advice on the block, that tip is no doubt going to save
me in future scripts aswell. Well now the batch file successfully
processes the file but instead of renaming it, it just disappears and I
do no know where it goes? Running from the command line still works as
it should its just the batch process that is causing the problems still.
It doesent make any sense… How is it working from command line and not
when i input it via a batch file? is there way to output the renamed
file to the current directory?

Thanks again for all the help, im not trying to be a serial question
asker but im so close.

Woot, its all working now. It was sending them to my windows home
folder. Thank you all so much for the help you have been great. Much
appreciated :slight_smile:

On Dec 31, 2011, at 6:14 AM, Peter V. wrote:

The main reason is that this will still close the file even if
an exception would occur (see IO.open for documentation).

That’s great to know! Thanks for the tip.

Wayne

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