简体   繁体   中英

Create directory where specific files exist in ruby

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")

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)

只需将正确的目录名传递给mkdir即可:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM