Created
October 5, 2024 08:44
-
-
Save yowasou/d288a9ffaa2f3e22b5e983e8c429c707 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 渡ってきた複数ファイルのうち、拡張子がsafetensorsのファイルを探し、 | |
# それ以外の画像ファイルをすべて同じファイル名に変える。 | |
# CivitaiからダウンロードしてきたLoRAに対して使う。 | |
require 'fileutils' | |
def rename_files_to_same_name | |
imagefiles = [] | |
safefilename = "" | |
ARGV.each do |file| | |
puts "ARGV : " + file | |
ext = File.extname(file).downcase # 拡張子を取得 | |
if (ext == ".safetensors") | |
safefilename = File.basename(file, File.extname(file)) | |
elsif (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".webp" || ext == ".jfif") | |
imagefiles.push(file) | |
end | |
end | |
i = 0 | |
imagefiles.each do |file| | |
puts "TARGET FILE : " + file | |
if (i == 0) | |
new_file = File.dirname(file) + "/" + safefilename + File.extname(file) | |
else | |
new_file = File.dirname(file) + "/" + safefilename + "_" + i.to_s + File.extname(file) | |
end | |
puts "NEW FILE : " + new_file | |
if !File.exist?(new_file) | |
FileUtils.mv(file, new_file) # ファイルをリネーム | |
puts "Renamed: #{file} -> #{new_file}" | |
end | |
i = i + 1 | |
end | |
end | |
# 使用例 | |
rename_files_to_same_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment