批量重命名文件;哈希排序问题

发布于 2025-01-08 15:10:03 字数 1488 浏览 4 评论 0原文

我想对从 YouTube 下载的一堆课程文件重命名并添加编号。然而,它们的顺序是错误的。我有一个列表,其中包含所有正确的名字,按观看顺序排列。

所以我的想法是获取该列表,用键对它们进行哈希,稍后将用作文件的编号,并且该值将是实际的文件名。之后,我将对键的哈希值进行排序,并迭代我的目录并重命名目录中包含的所有文件。

我现在有以下代码:

 1 #!/usr/bin/ruby
 2 
 3 counter = 0
 4 
 5 folder_path = "SOME_PATH"
 6 names = Hash.new
 7 
 8 file = File.new("names.txt", "r")
 9 while (line = file.gets)
10     #puts "#{counter}: #{line}"
11     names[sprintf '%03d', counter] = line
12     counter += 1
13 end
14 file.close
15 
16 puts "======== Names before sorting: \n ========"
17 puts names
18 
19 names.keys.sort
20 
21 puts "======== Names after sorting: \n ========"
22 puts names
23 
24 
25 
26 new_filename = ""
27 counter = 0
28 Dir.glob(folder_path + "/*").each do |f|
29         #puts "#{counter}: #{File.basename(f, File.extname(f))}"
30         numbering = (sprintf '%03d', counter)
31         new_filename = numbering + " - " + names[numbering]
32 
33         File.rename(f, folder_path + "/" + new_filename + File.extname(f))
34         counter += 1
35 end
36 
37 puts "Renaming complete."

现在的问题是我的哈希值没有真正排序。我认为这是因为我的键值实际上是字符串而不是数字,但是我怎样才能实现重命名的前导零呢?

重命名过程本身也不太有效。它似乎适用于前几个视频,但随后我收到以下错误消息:

main.rb:33:in `rename': No such file or directory - PATH - .mp4 or PATH -  - Dot and Cross Product Comparison/Intuition (Errno::ENOENT)

编辑:

哇,我的错。我没有检查names.txt。它实际上有一个带有正斜杠的名称。因此,导致出现上述错误信息。

我用 'names.index(names.values.sort[counter])' 进行了排序

I want to rename and add numbers to a bunch of lesson files, which I downloaded from YouTube. They are, however, in the wrong order. I have a list with all the correct names in the order they should be watched.

So my idea is to take that list, make hashes out of them with keys, which will later be used as numbering of the files, and the value will be the actual file name. After that I will sort my hashes for the keys and iterate through my directory and rename all the files included in the directory.

I have the following code right now:

 1 #!/usr/bin/ruby
 2 
 3 counter = 0
 4 
 5 folder_path = "SOME_PATH"
 6 names = Hash.new
 7 
 8 file = File.new("names.txt", "r")
 9 while (line = file.gets)
10     #puts "#{counter}: #{line}"
11     names[sprintf '%03d', counter] = line
12     counter += 1
13 end
14 file.close
15 
16 puts "======== Names before sorting: \n ========"
17 puts names
18 
19 names.keys.sort
20 
21 puts "======== Names after sorting: \n ========"
22 puts names
23 
24 
25 
26 new_filename = ""
27 counter = 0
28 Dir.glob(folder_path + "/*").each do |f|
29         #puts "#{counter}: #{File.basename(f, File.extname(f))}"
30         numbering = (sprintf '%03d', counter)
31         new_filename = numbering + " - " + names[numbering]
32 
33         File.rename(f, folder_path + "/" + new_filename + File.extname(f))
34         counter += 1
35 end
36 
37 puts "Renaming complete."

The problems are now that my hashes don't really sort. I think it is because my key values are actually string and not numbers, but how else would I achieve leadings zeros for my renaming?

The renaming process itself doesn't quite work either. It seems to work for the first couple videos, but then I get the following error message:

main.rb:33:in `rename': No such file or directory - PATH - .mp4 or PATH -  - Dot and Cross Product Comparison/Intuition (Errno::ENOENT)

EDIT:

Wow, my bad. I didn't check the names.txt. It actually had a name with a forward slash in it. Thus, causing the above error message.

I did the sorting with 'names.index(names.values.sort[counter])'

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

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

发布评论

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

评论(1

在巴黎塔顶看东京樱花 2025-01-15 15:10:03

当您输入 names.keys 时,您会得到一个键数组,而不是哈希值,然后对它进行排序不会影响您的哈希值。如果你想以排序的方式打印哈希值,你可以这样做:

names.keys.sort.map do |key|
  puts "#{key} #{names[key]}"
end

When you've typed names.keys you got an array of keys, not the hash, then sorting it didn't affect your hash. If you want to print hash in sorted way you can do like this:

names.keys.sort.map do |key|
  puts "#{key} #{names[key]}"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文