0

こことここのリンクテキストに気付いたいくつかの異なるスクリプトを結び付けよ うとしています。ユーザーがディレクトリ文字とファイル拡張子を指定して、文字を削除したり、ファイルの名前を変更したりできる基本的なスクリプトを取得しようとしています。

全体を繋ぐのに苦労しています。これが私がこれまでのところです。

    require 'fileutils'

define renamer(strip, stripdetails) 
# So this is a strip function.

    def strip(str,char)
   new_str = ""
   str.each_byte do |byte|
      new_str << byte.chr unless byte.chr == char
   end
   new_str
end
# and then retrieve details from user.

#Get directory of files to be changed.
def stripdetails(strip myname)
 puts "Enter Directory containing files"
 STDOUT.flush
 oldname = gets.chomp
 puts "what characters do you want to remove"
 str = gets.chomp
 puts "what file extension do files end in?"
 fileXt = gets.chomp
 end

#And I found this from stackoverflow(I don't have enuff credits to post another hyperlink)
old_file = "oldname"
new_file = strip(oldname,str)
FileUtils.mv(old_file, new_file)
4

2 に答える 2

3

これがコードのリファクタリングです。あなたの質問やコードからは完全には明らかではありませんが、ディレクトリ内の各ファイル名から特定の文字を削除したいと考えています。

ブログ投稿からコピーした strip() メソッドは、組み込みtr()メソッドの再実装が不十分であるため、まったく不要であることに注意してください。

#Given a directory, renames each file by removing
#specified characters from each filename

require 'fileutils'

puts "Enter Directory containing files"
STDOUT.flush
dir = gets.chomp
puts "what characters do you want to remove from each filename?"
remove = gets.chomp
puts "what file extension do the files end in?"
fileXt = gets.chomp

files = File.join(dir, "*.#{fileXt}")
Dir[files].each do |file|
  new_file = file.tr(remove,"")
  FileUtils.mv(file, new_file)
end
于 2011-01-10T13:02:06.247 に答える
0

このプログラムがstripdetailsメソッドを呼び出すことはありません。コードが同じスコープで実行されるように、「Get directory of files to be changed」ブロックのdef stripdetails..and行を削除してみてください。end

于 2011-01-10T05:30:03.677 に答える