![](/img/trans.png)
[英]RegEx find and replace filename in SQL file via Adobe Brackets
[英]Batch find and replace filename in file with a loop
我在 windows 上(但我最終可以運行虛擬 linux),當我在其中調用我的圖像或音樂文件時,我正在嘗試替換多個python Ren'Py 文件中的一些行
我的意思是,實際上,在我的 Ren'Py 文件中,我有: imagebutton "aaaa.png"
我想用imagebutton "images/aaaa.png"
替換它我想用另一個文件夾中的所有文件來做因為我現在將所有 7500 張圖像移到 /images/ 文件夾中
實際上,我的文件夾是這樣的:
script1.rpy
script2.rpy
/images/aaaa.png
/images/bbbb.png
我找到:
find -name '*.png' -printf '%f\n' |
while read file; do
# do something with "$file"
done
while read a; do
echo ${a//abc/XYZ}
done < /tmp/file.txt > /tmp/file.txt.t
mv /tmp/file.txt{.t,}
但我真的不知道如何結合它:(
如果你想用imagebutton“images/aaaa.png”替換imagebutton“aaaa.png”,如下:
while read a; do
echo ${a//imagebutton \"/imagebutton \"images\/}
done < /tmp/file.txt
另一種方法是在 Bash 中使用關聯 arrays。 但是,我尚未確認它是否適用於許多文件。
declare -A files
for file in `find -name '*.png'`; do
name=`basename $file`;
files[$name]=${file#./};
done
while read a; do
from=`echo $a | grep -o -E "[^\"]+\.png"`
echo ${a//$from/${files[$from]}}
done < /tmp/file.txt
如果可以使用 Python,我認為最好用 Python 編寫。
是的,我決定在 Python
import os
import sys
from os import listdir
from os.path import isfile, join
scripts = [s for s in listdir(sys.argv[1]) if isfile(join(sys.argv[1], s)) and "rpy" == s.split(".")[1]]
folders = ["images", "sounds"]
warning = []
for script in scripts:
print(script)
file = open(script, "r", encoding="utf8")
content = file.readlines()
file.close()
for folder in folders:
folderContent = [i for i in listdir(sys.argv[1] + os.sep + folder) if isfile(join(sys.argv[1] + os.sep + folder, i))]
for fc in folderContent:
for i, e in enumerate(content):
if fc in e:
if len(e.split("/")) > 1:
warning.append(script + " | ligne " + str(i+1))
content[i] = content[i].replace(fc, folder + "/" + fc)
file = open(script, "w", encoding="utf8")
file.write("".join(content))
file.close()
file = open("warningRename.txt", "w", encoding="utf8")
file.write("\n".join(warning))
file.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.