在 Ruby 中使用变量重命名文件 [英] renaming files using variables in Ruby
问题描述
如何使用变量在 Ruby 中重命名文件?
How do you use variables to rename files in Ruby?
File.rename("text1.txt", "text2.txt")
上面的例子在使用 irb 时很好,但是我编写了一个脚本,其中 var1 和 var2 都不为我所知.
The above example is fine when using irb, but I writing a script where both var1 and var2 are unknown to me.
例如:
script_dir = File.expand_path File.dirname(__FILE__)
Dir.chdir(script_dir)
Dir.glob('Cancer1-1.pencast').each do |pencast|
pencast_title = File.basename(File.basename(pencast), '.*')
i = 1
audio_title = File.basename(`unzip -l #{pencast} | grep .aac | awk '{print $4;}' | awk 'NR=='#{i}''`)
audio_path = `unzip -l #{pencast} | grep .aac | awk '{print $4;}' | awk 'NR=='#{i}''`
audio_extension = File.extname(File.basename(audio_path))
new_name = "#{pencast_title}-#{i}#{audio_extension}"
File.rename(audio_title, new_name)
不起作用...但是如果我使用 puts var1
我会看到我想要的文件名.
does not work...
but if i use puts var1
I see the file name I want.
我得到的错误是:
prog_test.rb:12:in `rename': No such file or directory - audio-0.aac (Errno::ENOENT)
or Cancer1-1-1.aac
from prog_test.rb:12
from prog_test.rb:5:in `each'
from prog_test.rb:5
但是文件 audio-0.aac
在那里...我正在查看它.
but the file audio-0.aac
is there... I'm looking at it.
我确定我已经找到了问题所在:它似乎是将一个变量添加到另一个变量中.这是一个产生相同输出的简化示例:
I am certain I have located the problem: it seems to be adding a variable to another variable. This is a simplified example that produces the same output:
audio_title = "audio-0.aac"
fullPath = File::SEPARATOR + "Users" + File::SEPARATOR + "name" + File::SEPARATOR + "Desktop" + File::SEPARATOR + audio_title
newname = File::SEPARATOR + "Users" + File::SEPARATOR + "name" + File::SEPARATOR + "Desktop" + File::SEPARATOR + "audio1.aac"
puts fullPath
puts newname
File.rename(fullPath, newname)
输出:
/Users/name/Desktop/audio-0.aac
/Users/name/Desktop/audio1.aac
prog_test.rb:22:in `rename': No such file or directory - /Users/name/Desktop/audio-0.aac or /Users/name/Desktop/audio1.aac (Errno::ENOENT)
from prog_test.rb:22
推荐答案
您应该将完整的文件路径传递给 File.rename
,而不仅仅是基本名称
You should be passing the full file path to File.rename
, not just the basename
我不确定您在 File.basename()
中的示例中发生了什么,但请想象以下内容:
I am not sure what is going on in your example inside File.basename()
, but imagine the following:
fullPath = "C:" + File::SEPARATOR + "Folder" + File::SEPARATOR + "File.txt" # C:\Folder\File.txt
basename = File.basename(fullPath) # File
newFileName = "File.bak"
File.rename(basename, newFileName)
# How can Ruby possibly know which directory to find the above file in, or where to put it? - It will just look in the current working directory
因此,您需要将完整路径传递给 File.rename,如下所示:
So instead, you need to pass the full path to File.rename, like so:
fullPath = "C:" + File::SEPARATOR + "Folder" + File::SEPARATOR + "File.txt" # C:\Folder\File.txt
directory = File.dirname(fullPath) # C:\Folder
newFileName = "File.bak"
File.rename(fullPath, directory + File::SEPARATOR + newFileName)
这篇关于在 Ruby 中使用变量重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!