重命名Ruby的方法;逃逸冒号 [英] Rename method of Ruby; escaping colon

查看:69
本文介绍了重命名Ruby的方法;逃逸冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中重命名文件时如何逃脱冒号?

How do you escape a colon, when renaming files in Ruby?

我有以下代码(名称是已填充数据的哈希):

I have following code (names is a hash with data already filled in):

new_filename = ""
counter = 0 
Dir.glob(folder_path + "/*").each do |f|
  numbering = names.index(names.values.sort[counter])
  new_filename = numbering + " - " + names.values.sort[counter]
  puts "New file name: " + new_filename
  File.rename(f, folder_path + "/" + new_filename + File.extname(f))
  counter += 1
end

puts "Renaming complete."

new_filename的输出是正确的,例如"Foo - Bar: Foo.txt".重命名文件时,文件具有以下格式:"Foo - Bar/ Foo.txt".

The output of new_filename is correct, e.g. "Foo - Bar: Foo.txt". When it renames the file, the file has following format: "Foo - Bar/ Foo.txt".

我尝试用反斜杠转义冒号,但似乎不起作用,因为我的输出如下所示:"Foo - Bar/\ Foo.txt".

I tried escaping with the colon with a backslash, but doesn't seem to work, because my output then looks like this: "Foo - Bar/\ Foo.txt".

是否可以在字符串中使用冒号来重命名文件?

Is is possible to have a colon in a string for renaming files?

推荐答案

FYI-在NTFS中,冒号标识同一文件的单独流..."Foo Bar:Foo.txt"标识文件"Foo Bar",流"Foo.txt".参考备用数据流"(当前 http://support.microsoft.com/kb/105763 ). AFIK这项功能并未真正得到广泛使用,尽管我已经看到它用于标记带有第三方数据的文件(我将其用于存储文件的sha1,以便在流*:sha1下进行重复识别).

FYI - in NTFS a colon identifies a separate stream of the same file... "Foo Bar: Foo.txt" identifies file "Foo Bar", stream " Foo.txt". Reference "Alternate Data Streams" (currently http://support.microsoft.com/kb/105763). AFIK this feature is not really widely used, though I have seen it used to tag files with thrid-party data (I use it to store a file's sha1 for dupe identification under the stream *:sha1).

这篇关于重命名Ruby的方法;逃逸冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆