Ok, I fixed my code. You need to do two things. First, you should have a "directory_of_Files" which contains all of the files and subfolders with files. Next you should have a new parent folder, "new_parent_directory", which is empty. new_parent_directory will get organized by the file endings from the files within "directory_of_Files". Essentially, this script looks for all of the files within a directory, and directories within that directory, then, it creates a list of the file endings and then it makes directories in a new directory based on those file endings, and then it takes all the files within the parent directory and moves them into the newly established directories.
If you have python installed.....
In terminal, type
python
then,
import os
then,
#this is ths directory that contains all your files
#YOU MUST CHANGE THIS!!!!!!
directory_of_Files = "/Users/name/Desktop/test1"
#AND YOU MUST CHANGE THIS!!!!!!
new_parent_directory = "/Users/name/Desktop/newhometest"
#From here down, it's all magic.
all_subfolders = [x[0] for x in os.walk(directory_of_Files)]
#Get the full file name and only the files
filenames=[]
for subfolder in all_subfolders:
os.chdir(subfolder)
for file in filter(os.path.isfile, os.listdir(os.getcwd())):
if not file.startswith("."):
filenames.append(os.getcwd()+"/"+file)
#get the file endings
all_files_endings = []
for i in filenames:
all_files_endings.append(i.split(".",1)[1])
#remove the duplications
all_files_endings = list(set(all_files_endings))
#create some folders in the new_directory with the file endings
for fileExtensions in all_files_endings:
os.mkdir(new_parent_directory + "/" + fileExtensions)
#move the files from their old destination to their new destination
newnames=[]
for subfolder in all_subfolders:
os.chdir(subfolder)
for file in filter(os.path.isfile, os.listdir(os.getcwd())):
if not file.startswith("."):
newnames.append(new_parent_directory+"/"+file.split(".",1)[1]+"/"+file)
print file
if len(filenames) == len(newnames):
for i in range(len(filenames)):
shutil.move(filenames[i], newnames[i])
I tested this on Mac OSX 10.11 with python 2.7. You could also just copy all of the code into a text file, save it as "something.py", and then run it from terminal with the code,
python something.py
You could use
find
as you've tagged this Q, but a better idea is to usetar
, which has switches to support this. The classic invocation is (in the top of the source tree)tar cf - | (cd target;tar xf -)
which copies the whole tree. You'd want to look at theman
page for yourtar
to see how to select just the files you want. – MAP – 2016-07-21T07:52:37.853Oops, that's a typo on the
tar
command, should betar cf - . | (cd target;tar xf -)
(have to tell it something to tar up. – MAP – 2016-07-21T08:43:42.2871What OS are you using? – Jonatan Öström – 2016-07-21T10:20:36.270
Please note that [SU] is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
– DavidPostill – 2016-07-22T09:32:39.343Sorry you are right in this case I was using it as a script writing service, I apologize. If I do not know how to write scripts, how can I obtain one or learn to write one. Could I ask a question such as "Can someone help me write a script...?" – Nevermind Susan – 2019-04-27T20:47:55.900