这里的一些帮助将不胜感激。目标是将所有.txt文件重命名为ruby中用户输入的目录中的.text。我必须使用fileutils作为要求。当我运行我的当前脚本时,我没有收到任何错误。然而,没有任何事情发生......我相信你们中的一个人可能可以帮我指出这个问题。Ruby:将给定目录中的所有文件从.txt重命名为文本
#!/usr/bin/ruby
#This program was written by me
puts “what directory would you like to change? “
require 'fileutils'
pathname = gets.chomp
def rename(pathname)
currentdir = Dir.new('.')
newfile = FileTest.exists?(pathname.to_s)
if pathname != "q"
if newfile == "true"
require 'fileutils'
newfile.each do |f|
FileUtils.mv "#{File.dirname(f)}/#{File.basename(f, '.*')}.txt", "#{File.dirname(f)}/#{File.basename(f,'.*')}.text"
end
elsif currentdir
require 'fileutils'
(currentdir).each do |f|
FileUtils.mv "#{File.dirname(f)}/#{File.basename(f, '.*')}.txt", "#{File.dirname(f)}/#{File.basename(f,'.*')}.text"
end
else
puts "Invalid Path"
end
end
end
编辑:我想我现在知道这个问题,但不知道在哪里把代码。我需要cd到用户输入的目录,这应该是我只能更改主目录中的.txt文件的原因。
此问题与建议的意见不同,因为要求是使用fileutils并让用户输入他们想要编辑的目录。此项查找文件名从任何地方,并且不使用文件实用程序
的可能的复制[如何在质量红宝石重命名文件(http://stackoverflow.com/questions/10758694/how-to- mass-rename-files-in-ruby) – Wes
这可能是因为你从来没有真正调用过重命名方法吗? – Rashmirathi
虽然不尽相同。要求是使用fileutils并让用户输入他们想要编辑的目录。此条目从任何地方查找文件名并且不使用fileutils –