0724デフォルトの名無しさん
2020/04/21(火) 22:46:58.98ID:HADG6GDbRuby で作った。
ただし、DryRun なので実際には実行されません。
001.txt, 002.txt に変わります
require 'fileutils'
# 絶対パスのディレクトリ名の後ろに、* を付けること!
# . で始まる、隠し directory, file を除く
dir_path = "C:/Users/Owner/test/*"
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 から始まる
# format で、0埋め3桁表示にする。extname は拡張子
# ファイル名は、001.txt, 002.txt など
dest_path = target_dir + "/" + ( "%03d" % idx ) + File.extname( full_path )
FileUtils::DryRun.move( full_path, dest_path )
end