2017-05-29 8 views
-1

整理する必要があるファイルは12,000以上あります。すべてのフォルダが含まれていますが、ファイルは現在フラット化されたファイル構造になっています。ファイル構造を整えるためのBash Macターミナル

マイフォルダやファイルは、すべての彼らはにする必要がありますパスで指定されている。例えば、一つのディレクトリに私は\texturesという名前のフォルダを持っていると\textures\actors\bearという名前の別のフォルダがどんなに\textures\actorsフォルダがありません。私はこれらのフォルダを取って、それぞれのフォルダとファイル名が示すべき正しい場所に配置するマクロを開発するのに苦労しています。私はにこれらを自動的に並べ替えることができたいと思います。actorsと内部それはbearになります。しかし、12,000以上のファイルがありますので、私はこのすべてを決定し、可能ならばそれを行う自動化されたプロセスを探しています。

すべてのファイルまたはフォルダ名を調べ、ファイルまたはフォルダがディレクトリ内にあるフォルダを検出し、そこに自動的に移動し、指定されたパス内に存在しないフォルダを作成するスクリプトはありますか必要なときは?このようなディレクトリ構造を考えると

おかげ

+0

以下の解決方法が有効な場合は、それを知らせることができます。おそらく答えを投票することによって。 –

+0

Devin、このソリューションは機能しましたか、目標を達成するための支援が必要ですか? –

+0

なぜディレクトリ名にバックスラッシュがありますか? –

答えて

0

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

Pythonスクリプト:

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) 
$ ls /tmp/stacktest 
    \textures 
    \textures\actors\bear 
     fur.png 
    \textures\actors\bear\fur2.png 

は、以下のPythonスクリプトは、このにそれを向けるだろうこのスクリプトは、dir_pathのすべてのファイルとフォルダを読み取り、dest_pathの下に新しいディレクトリ構造を作成します。 dest_pathdir_pathに入れないでください。

関連する問題