-1

I have 12,000+ files that need to be organized. All the folders are included, however the files are in a flattened filestructure right now.

My folders and files are all named with the path that they should be in. For example, in one directory I have a folder named \textures and another folder named \textures\actors\bear however there is no \textures\actors folder. I am struggling at developing a macro that will take these folders and put them in the correct place that each folder and filename suggests it should be in. I would like to be able to automatically sort these into textures and inside that would be actors and inside that would be bear. However, there are over 12,000 files so I am looking for an automated process that will determine all of this and just do it if possible.

Is there a script that will look at every file or folder name and detect which folder the file or folder should be in in the directory and automatically move them there as well as create any folders that do not exist within the path given when needed?

Thanks

1 Answers1

0

Given a directory structure like this:

$ ls /tmp/stacktest
    \textures  
    \textures\actors\bear
        fur.png
    \textures\actors\bear\fur2.png

The python script below will turn it into this:

$ ls /tmp/stackdest
    textures/actors/bear
        fur.png
        fur2.png

Python script:

from os import walk
import os

# TODO - Change these to correct locations
dir_path = "/tmp/stacktest"
dest_path = "/tmp/stackdest"

for (dirpath, dirnames, filenames) in walk(dir_path):
    # Called for all files, recu`enter code here`rsively
    for f in filenames:
        # Get the full path to the original file in the file system
    file_path = os.path.join(dirpath, f)

        # Get the relative path, starting at the root dir
        relative_path = os.path.relpath(file_path, dir_path)

        # Replace \ with / to make a real file system path
        new_rel_path = relative_path.replace("\\", "/")

        # Remove a starting "/" if it exists, as it messes with os.path.join
        if new_rel_path[0] == "/":
            new_rel_path = new_rel_path[1:]
        # Prepend the dest path
        final_path = os.path.join(dest_path, new_rel_path)

        # Make the parent directory
        parent_dir = os.path.dirname(final_path)
        mkdir_cmd = "mkdir -p '" + parent_dir + "'"
        print("Executing: ", mkdir_cmd)
        os.system(mkdir_cmd)

        # Copy the file to the final path
        cp_cmd = "cp '" + file_path + "' '" + final_path + "'"
        print("Executing: ", cp_cmd)
        os.system(cp_cmd)

The script reads all the files and folders in dir_path and creates a new directory structure under dest_path. Make sure you don't put dest_path inside dir_path.

Dig-Doug
  • 91
  • 8