0801デフォルトの名無しさん
2021/08/25(水) 13:21:49.43ID:T8xCLAXofileutils のDryRun を使ったので、実際には実行されない。
結果を予測して、表示するだけ
require 'fileutils'
# 絶対パスのディレクトリ名の後ろに、* を付けること!
# . で始まる、隠し directory, file を除く
dir_path = "C:/Users/Owner/Documents/*.jpg"
target_dir = File.dirname( dir_path ) # ディレクトリパスだけを取り出す
Dir.glob( dir_path )
.select { |full_path| File.file?( full_path ) } # ファイルのみ
.each.with_index( 1 ) do |full_path, idx| # index は、1 から始まる
dest_path = target_dir + "/new#{ idx }-" + File.basename( full_path )
FileUtils::DryRun.move( full_path, dest_path )
end
出力
mv C:/Users/Owner/Documents/abc.jpg
C:/Users/Owner/Documents/new1-abc.jpg
mv C:/Users/Owner/Documents/xyz.jpg
C:/Users/Owner/Documents/new2-xyz.jpg