Forum: Ruby Creating directory and its files

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ef876fc6e1baa12cef2c381ea6feabbe?d=identicon&s=25 Martin Sharon (martinh)
on 2009-04-27 05:38
Hi there:
I want to create a directory and create files under it.
I can use

FileUtils.mkdir_p ARGV[0]

to create a directory, however if the directory already exist, program
will
generate error messages:

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:243:in
`mkdir': File exists - ETFlist (Errno::EEXIST)
        from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:243:in
`fu_mkdir'
        from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:217:in
`mkdir_p'
        from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:215:in
`reverse_each'
        from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:215:in
`mkdir_p'
        from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:201:in
`each'
        from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/fileutils.rb:201:in
`mkdir_p'


Please tell me how to solve this problem.

Thank you so much.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-27 06:20
Martin Sharon wrote:
> Hi there:
> I want to create a directory and create files under it.
> I can use
>
> FileUtils.mkdir_p ARGV[0]
>
> to create a directory, however if the directory already exist, program
> will
> generate error messages:
>

How about posting what you want to do if the directory already exists?
If you want to overwrite it(=delete it), then first check if the
directory exists:

if File.exist?("dirname")

If it does, then:

if File.exist?("dirname")
 FileUtils.remove_dir("dirname")
end

But that is playing with fire.  You could end up deleting your whole
hard drive if you get the right file name in there.   A safer option
would be to rename the old dir, and then create the new dir.
Ef876fc6e1baa12cef2c381ea6feabbe?d=identicon&s=25 Martin Sharon (martinh)
on 2009-04-27 06:34
You mention me it's safe to create a directory with date version
like directory_yy_mm_dd

i can get date by

time=Date.today()

but how to connect the directory name with the time?

I use

FileUtils.mkdir_p ARGV[0]+time

but there is an error message:

test2.rb:61:in `+': can't convert Date into String (TypeError)


7stud -- wrote:
> Martin Sharon wrote:
>> Hi there:
>> I want to create a directory and create files under it.
>> I can use
>>
>> FileUtils.mkdir_p ARGV[0]
>>
>> to create a directory, however if the directory already exist, program
>> will
>> generate error messages:
>>
>
> How about posting what you want to do if the directory already exists?
> If you want to overwrite it(=delete it), then first check if the
> directory exists:
>
> if File.exist?("dirname")
>
> If it does, then:
>
> if File.exist?("dirname")
>  FileUtils.remove_dir("dirname")
> end
>
> But that is playing with fire.  You could end up deleting your whole
> hard drive if you get the right file name in there.   A safer option
> would be to rename the old dir, and then create the new dir.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-27 06:59
Martin Sharon wrote:
> You mention me it's safe to create a directory with date version
> like directory_yy_mm_dd
>
> i can get date by
>
> time=Date.today()
>
> but how to connect the directory name with the time?
>
> I use
>
> FileUtils.mkdir_p ARGV[0]+time
>

require 'date'

time = Date.today()
fname = "somestring"

combined1 = fname + time.to_s
combined2 = "#{fname}-#{time}"

puts combined1, combined2

--output:--
somestring2009-04-26
somestring-2009-04-26
This topic is locked and can not be replied to.