Solved bug

This commit is contained in:
Kevin Veen-Birkenbach 2023-08-22 23:58:37 +02:00
parent 75cdf367b2
commit 5cbf98a926

View File

@ -5,7 +5,7 @@ def replace_content(path, old_string, new_string, preview, verbose):
with open(path, 'r', encoding='utf-8') as f: with open(path, 'r', encoding='utf-8') as f:
content = f.read() content = f.read()
if old_string in content: # Prüfen, ob der alte String im Inhalt vorkommt if old_string in content:
new_content = content.replace(old_string, new_string) new_content = content.replace(old_string, new_string)
if verbose: if verbose:
@ -16,6 +16,9 @@ def replace_content(path, old_string, new_string, preview, verbose):
f.write(new_content) f.write(new_content)
def process_directory(base_path, old_string, new_string, recursive, folder, files, content, preview, verbose, hidden): def process_directory(base_path, old_string, new_string, recursive, folder, files, content, preview, verbose, hidden):
# Eine Liste, um die Pfade der umzubenennenden Ordner zu speichern
directories_to_rename = []
for root, dirs, filenames in os.walk(base_path): for root, dirs, filenames in os.walk(base_path):
# Wenn "hidden" nicht gesetzt ist, versteckte Dateien/Ordner aus der Liste entfernen # Wenn "hidden" nicht gesetzt ist, versteckte Dateien/Ordner aus der Liste entfernen
if not hidden: if not hidden:
@ -35,20 +38,25 @@ def process_directory(base_path, old_string, new_string, recursive, folder, file
print(f"Renaming file from: {old_path} to: {new_path}") print(f"Renaming file from: {old_path} to: {new_path}")
if not preview: if not preview:
os.rename(old_path, new_path) os.rename(old_path, new_path)
# Pfade von zu ändernden Ordnern speichern
if folder: if folder:
for d in dirs: for d in dirs:
if old_string in d: if old_string in d:
old_path = os.path.join(root, d) old_path = os.path.join(root, d)
new_path = os.path.join(root, d.replace(old_string, new_string)) new_path = os.path.join(root, d.replace(old_string, new_string))
if verbose: directories_to_rename.append((old_path, new_path))
print(f"Renaming directory from: {old_path} to: {new_path}")
if not preview:
os.rename(old_path, new_path)
if not recursive: if not recursive:
break break
# Ordnernamen ändern nach dem os.walk() Durchlauf
for old_path, new_path in directories_to_rename:
if verbose:
print(f"Renaming directory from: {old_path} to: {new_path}")
if not preview:
os.rename(old_path, new_path)
def main(): def main():
parser = argparse.ArgumentParser(description="Replace strings in directories and files.") parser = argparse.ArgumentParser(description="Replace strings in directories and files.")
parser.add_argument('path', help="Path in which replacements should be made.") parser.add_argument('path', help="Path in which replacements should be made.")