在 Ruby 中使用变量重命名文件

发布于 2024-12-29 13:40:49 字数 1763 浏览 0 评论 0原文

在 Ruby 中如何使用变量重命名文件?

File.rename("text1.txt", "text2.txt")

上面的例子在使用 irb 时很好,但是我编写了一个脚本,其中 var1 和 var2 我都不知道。

例如:

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 我会看到我想要的文件名。

我得到的错误是:

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 在那里......我正在查看它。


我确信我已经找到了问题所在: 它似乎正在将一个变量添加到另一个变量中。这是一个产生相同输出的简化示例:

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)

OUTPUT :

/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

How do you use variables to rename files in Ruby?

File.rename("text1.txt", "text2.txt")

The above example is fine when using irb, but I writing a script where both var1 and var2 are unknown to me.

for example:

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)

does not work...
but if i use puts var1 I see the file name I want.

The error I get is:

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

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)

OUTPUT :

/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2025-01-05 13:40:49

您应该将完整的文件路径传递给 File.rename ,而不仅仅是基本名称

我不确定 File.basename() 中的示例中发生了什么,但是想象一下:

  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,如下所示:

  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)

You should be passing the full file path to File.rename, not just the 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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文