0

Using the following two lines I create the file "tmp.txt" in each directory where file_name exist. Than I open the file and write into it an array which I created before:

tmp_file = File.expand_path(File.dirname(file_name)) + "/temp.txt"
File.open(tmp_file, 'w') {|f| f.write array.join("\n")}

How can I create now also a empty directory into the same directories where tmp.txt is created?

I tried something like this:

File.expand_path(File.dirname(file_name)) + FileUtils.mkdir("/NewDirectory")
3
  • 1
    The first line doesn't create a new file, it only returns a path. Commented Dec 4, 2015 at 9:46
  • Cannot be reproduced. I do not understand why you thought that a path ending with "temp.txt" would be related to creating a file named "tmp.txt". Commented Dec 4, 2015 at 10:07
  • @Stefan: I'm creating the tmp.txt and than write into it some strings (which comes from an array). Have edited the question now hopefully for better understanding. Commented Dec 4, 2015 at 10:30

2 Answers 2

1

Create directory path and pass it to FileUtils.mkdir.

BTW, Instead of concatenate using +, use File::join instead:

dirpath = File.expand_path(File.join(File.dirname(file_name), 'NewDirectory'))
FileUtils.mkdir(dirpath)
Sign up to request clarification or add additional context in comments.

Comments

0

Just pass a correct dirname to mkdir:

FileUtils.mkdir(File.expand_path(File.dirname(file_name)) + "/NewDirectory")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.