How to move files with a specific file extension from within a parent folder as well as its' subfolders to a new folder

0

1

I want to move all files with extensions such as: .JPEG .MP3 .MP4 .MOV .AVI .PDF .PSD .WAV .TXT

...to new parent folders named after the extension of the files contained therein. Currently I have too many files within so many subfolders (under one parent folder) to do this manually. I tried searching the parent folder with * to then sort by file type but it is too much and my system either freezes or is unusable/slow. please assume I know nothing in regard to scripts/batch files but I can follow directions, copy and paste ;) please advise.

Nevermind Susan

Posted 2016-07-21T07:38:04.907

Reputation: 29

You could use find as you've tagged this Q, but a better idea is to use tar, 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 the man page for your tar to see how to select just the files you want. – MAP – 2016-07-21T07:52:37.853

Oops, that's a typo on the tar command, should be tar cf - . | (cd target;tar xf -) (have to tell it something to tar up. – MAP – 2016-07-21T08:43:42.287

1What 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.343

Sorry 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

Answers

0

(After edit and comments) This script should do it, including subfolders:

@echo off
for \r %%f in (*) do (
    if not exist %%~xf mkdir %%~xf
    move /y %%f %%~xf\%%~nf.%%~xf
)
pause

If you've never used a Batch script before, here's how it works:

  1. Open Notepad (not MS Word or WordPad)
  2. Copy/paste the script in it
  3. File -> Save As
  4. In the "Type" dropdown menu, select "All types"
  5. Save the file as something.bat (you can change the something but the .bat needs to be there)
  6. Put the file in the same directory as the files it needs to work on
  7. Double click the file

Make sure you test it before actually using it. The pause at the end in not needed, it will just prompt you to press a key before closing the window.

Nathan.Eilisha Shiraini

Posted 2016-07-21T07:38:04.907

Reputation: 2 503

This almost works as it creates the folders with the file extension names but only moves files with corresponding extensions that are in the same folder as the .bat files to the newly created folders. It doesn't move files that are within subdirectories or files within folders. I would have to copy and paste the .bat file into every sub folder (over 500) to sort all files by extension leaving me with multiple folders of JPEG .MP3 etc within each subfolder. Any way to have it execute from the one folder where all of the 500+ folders that contain the files is in? – Nevermind Susan – 2016-07-21T10:16:17.107

Edited the Batch script, should include subfolders. Sorry, I didn't see that point in your question '^^ – Nathan.Eilisha Shiraini – 2016-07-21T10:26:53.320

Still doesn't include the subfolders' content. Maybe it is something I am doing wrong. Before I went to sleep last night I searched within the root folder (asterisk period asterisk) to extract all files (280,000 files) and once that finished use the batch files you created to sort them into files but not successful; the batch file execution shows a repeated 0 files moved access is denied. I am logged on as admin. and have used open as admin. as well as when opening batch file. Any ideas? Thank you for your help thus far. – Nevermind Susan – 2016-07-21T22:52:45.250

0

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

dcook

Posted 2016-07-21T07:38:04.907

Reputation: 1

"I did not test this," Please don't post untested code ... – DavidPostill – 2016-07-21T09:30:23.673

I didn't know Python could be used on a windows OS. I have downloaded it but have no idea how to implement what you responded. – Nevermind Susan – 2016-07-21T10:18:03.200

Thanks David, I didn't know the rules. Yes, Susan, python is just another scripting langauge. nothing special. Perhaps it's a bit nicer than other languages because it's more human readable. The script I provided will MOVE, not rename all the files. If you just want to rename, so you have a original backup in it's old place, then change the very last line to os.rename instead of shutil.move – dcook – 2016-07-21T17:03:14.510

Cook, I am not running OSX or Linux along side windows 7 on this computer. I do have LinuxMint/WindowsXP on another desktop but this laptop only has Windows7 on it. Python is available for download for windows7, I have downloaded and opened the application and the cmd box opens (I guess the equivalent of terminal) and so I typed python and pressed enter and received: "NameError: name 'python' is not defined' – Nevermind Susan – 2016-07-21T23:02:34.173

is there a way to attach screen shots to my comments? first time using superuser – Nevermind Susan – 2016-07-21T23:09:24.210