Skip to content

Instantly share code, notes, and snippets.

@paulera
Created December 29, 2023 02:25
Show Gist options
  • Save paulera/49a660ba426a46ab82aaabb30a3c122a to your computer and use it in GitHub Desktop.
Save paulera/49a660ba426a46ab82aaabb30a3c122a to your computer and use it in GitHub Desktop.
Automatically recreates a directory tree from a filelist, using divider characters to define folder paths.
#!/usr/bin/env python3
# Written by OpenAI (GPT 4): https://chat.openai.com/share/b7a9d856-7509-460d-85b3-3e5f07161e55
import argparse
import os
import shutil
def process_files(output_folder, divider, file_names):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for file_name in file_names:
if divider in file_name:
parts = file_name.split(divider)
actual_file_name = parts[-1]
folder_structure = os.path.join(output_folder, *parts[:-1])
if not os.path.exists(folder_structure):
os.makedirs(folder_structure)
shutil.copy(file_name, os.path.join(folder_structure, actual_file_name))
else:
shutil.copy(file_name, os.path.join(output_folder, file_name))
print("Folder structure created successfully.")
def main():
parser = argparse.ArgumentParser(description="Recreate a directory tree based on file names.")
parser.add_argument("-o", "--output", default="output", help="Output folder where the directory tree will be created.")
parser.add_argument("-d", "--divider", default="-", help="Character used as a directory divider in file names.")
parser.add_argument("files", nargs="+", help="List of files to process.")
args = parser.parse_args()
process_files(args.output, args.divider, args.files)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment