gpt4 book ai didi

ruby - 在文件名中的某个关键字后面附加文本

转载 作者:数据小太阳 更新时间:2023-10-29 08:42:34 30 4
gpt4 key购买 nike

在目录 C:\Users\Scripts 中,我有以下文件:

2A_Apple_VC_20180101.txt
2A_Apple_VC_20180201.txt
2A_Apple_VC_20180301.txt
etc.

如何在 2A_ 之后为该目录中以 2A_ 开头的所有文件名插入短语 Vendor,从而永久更改文件名到:

2A_Vendor_Apple_VC_20180101.txt
2A_Vendor_Apple_VC_20180201.txt
2A_Vendor_Apple_VC_20180301.txt

最佳答案

你可以试试File#rename

Dir["2A_*.txt"].each do |f|
File.rename(f, f.sub('2A_', '2A_Vendor_'))
end

或路径

path = 'your_path_here'
Dir.glob("#{path}/2A_*.txt").each do |f|
File.rename(f, f.sub('2A_', '2A_Vendor_'))
end

对于 Windows,您可能需要使用反斜杠 this

关于ruby - 在文件名中的某个关键字后面附加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54407870/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com