如何在Ruby中重命名文件?

时间:2022-10-03 10:48:33

Here's my .rb file:

这是我的。rb文件:

puts "Renaming files..."

folder_path = "/home/papuccino1/Desktop/Test"
Dir.glob(folder_path + "/*").sort.each do |f|
    filename = File.basename(f, File.extname(f))
    File.rename(f, filename.capitalize + File.extname(f))
end

puts "Renaming complete."

The files are moved from their initial directory to where the .rb file is located. I'd like to rename the files on the spot, without moving them.

将文件从初始目录移动到.rb文件所在的位置。我想在不移动的情况下,将这些文件重新命名。

Any suggestions on what to do?

有什么建议吗?

5 个解决方案

#1


78  

What about simply:

那么简单:

File.rename(f, folder_path + "/" + filename.capitalize + File.extname(f))

#2


22  

Doesn't the folder_path have to be part of the filename?

folder_path不是必须是文件名的一部分吗?

puts "Renaming files..."

folder_path = "/home/papuccino1/Desktop/Test/"
Dir.glob(folder_path + "*").sort.each do |f|
  filename = File.basename(f, File.extname(f))
  File.rename(f, folder_path + filename.capitalize + File.extname(f))
end

puts "Renaming complete."

edit: it appears Mat is giving the same answer as I, only in a slightly different way.

编辑:似乎Mat给出的答案与我相同,只是方式略有不同。

#3


14  

If you're running in the same location as the file you want to change

如果您运行的位置与您想要更改的文件相同

File.rename("test.txt", "hope.txt")

Though honestly, I sometimes I don't see the point in using ruby at all...no need probably so long as your filenames are simply interpreted in the shell:

老实说,我有时候根本不明白使用ruby的意义……只要你的文件名只是在shell中解释就可以了:

`mv test.txt hope.txt`

#4


2  

If you are on a linux file system you could try mv #{filename} newname

如果您在linux文件系统上,您可以尝试mv #{文件名}newname

You can also use File.rename(old,new)

您还可以使用File.rename(旧的,新的)

#5


0  

Don't use this pattern unless you are ready to put proper quoting around filenames:

不要使用这个模式,除非你已经准备好在文件名周围加上适当的引号:

`mv test.txt hope.txt`

Indeed, suppose instead of "hope.txt" you have a file called "foo the bar.txt", the result will not be what you expect.

事实上,假设不是“希望”。你有一个文件叫"foo the bar "txt",结果不会是你期望的那样。

#1


78  

What about simply:

那么简单:

File.rename(f, folder_path + "/" + filename.capitalize + File.extname(f))

#2


22  

Doesn't the folder_path have to be part of the filename?

folder_path不是必须是文件名的一部分吗?

puts "Renaming files..."

folder_path = "/home/papuccino1/Desktop/Test/"
Dir.glob(folder_path + "*").sort.each do |f|
  filename = File.basename(f, File.extname(f))
  File.rename(f, folder_path + filename.capitalize + File.extname(f))
end

puts "Renaming complete."

edit: it appears Mat is giving the same answer as I, only in a slightly different way.

编辑:似乎Mat给出的答案与我相同,只是方式略有不同。

#3


14  

If you're running in the same location as the file you want to change

如果您运行的位置与您想要更改的文件相同

File.rename("test.txt", "hope.txt")

Though honestly, I sometimes I don't see the point in using ruby at all...no need probably so long as your filenames are simply interpreted in the shell:

老实说,我有时候根本不明白使用ruby的意义……只要你的文件名只是在shell中解释就可以了:

`mv test.txt hope.txt`

#4


2  

If you are on a linux file system you could try mv #{filename} newname

如果您在linux文件系统上,您可以尝试mv #{文件名}newname

You can also use File.rename(old,new)

您还可以使用File.rename(旧的,新的)

#5


0  

Don't use this pattern unless you are ready to put proper quoting around filenames:

不要使用这个模式,除非你已经准备好在文件名周围加上适当的引号:

`mv test.txt hope.txt`

Indeed, suppose instead of "hope.txt" you have a file called "foo the bar.txt", the result will not be what you expect.

事实上,假设不是“希望”。你有一个文件叫"foo the bar "txt",结果不会是你期望的那样。