batch move for different file types to different folder name

0

I have files containing in

C:\foldername1\Files\soandso1.mkv C:\foldername2\Files\soandso2.mp4 C:\foldername3\Files\soandso3.avi

I want to move the .mkv, .mp4, and .avi back to it's parent folder, which is foldername1, 2, 3.

foldername will be different for each soandso file is in. Basically, I want to get rid of the "Files" folder and have the media files placed in foldername1, 2, and 3.

Is there a batch command for this?

Roger

Posted 2013-02-02T01:29:02.773

Reputation: 3

So foldername1\Files only contains MKVs, foldername2\Files has only MP4s and foldername3\Files has only AVIs? – Karan – 2013-02-02T01:48:26.593

No, it could be random. "foldername" can go up to "foldername50" which contains a "Files" folder. Inside "Files" contains either a .mkv, .avi, or .mp4. – Roger – 2013-02-02T02:04:23.613

Also, it would be nice if I can include a command that deletes the "Files" folder after the move. – Roger – 2013-02-02T02:05:35.017

Since this looks to be a one-time deal, any reason why a simple search + sort by extension + cut/paste won't do? Also, which version of Windows? – Karan – 2013-02-02T02:11:30.860

because there is multiple "foldername", which means I have to go into each of them, go into "Files", cut and paste it to the parent "foldername"? Is there a search sort and cut paste technique I don't know about? I'm pretty new to these sort of stuff. – Roger – 2013-02-02T02:13:44.343

Wait, forget about my previous comment. I see now you don't want a single folder each for AVI, MKV etc., but want each file to go to its own parent folder. – Karan – 2013-02-02T02:19:05.997

that is correct. – Roger – 2013-02-02T02:38:31.637

Answers

1

Something I put together quickly; no time to test extensively right now:

@echo off
for /f "delims=" %%d in ('dir /b/ad-s-h') do (
    if exist "%%d\Files" (
        pushd "%%d\Files"
        if exist *.avi move *.avi ..
        if exist *.mkv move *.mkv ..
        if exist *.mp4 move *.mp4 ..
        popd
        rd "%%d\Files"
    )
)

If you run it in C:\, it will look at each top-level folder on the drive and if it contains a Files sub-folder, it will move any AVI/MKV/MP4 files found up one level (i.e. to the top-level folder), then delete the Files sub-folder.

If any other files exist in Files the folder will not be deleted. You can use rd /s /q if you want to forcibly delete. Also, move will prompt you each time to overwrite duplicates (if any), which can be done automatically by using move /y instead.

Karan

Posted 2013-02-02T01:29:02.773

Reputation: 51 857

I am using windows 7, how do i execute this command? – Roger – 2013-02-02T02:40:09.757

Copy+paste in Notepad, save as something like "D:\Test.bat" (with the quotes!), then open a command prompt window, navigate to C:, D:\ or whichever drive you want to run it on, and type D:\Test.bat. I advise you to copy a few of the folders to another drive and test the batch file on them first, before letting it loose on your entire collection (although it won't delete any files if saved as posted above). – Karan – 2013-02-02T02:43:26.463

Wow! Tested it and it works. Thanks Karan! You just saved me hours of work with this. – Roger – 2013-02-02T03:29:40.920

Hmm... executing on the actual drive with the media didn't complete do the work. Some files didn't move and some "Files" folder did not delete by itself. Maybe it was too large of a batch to execute all commands, but it got most of the job done. – Roger – 2013-02-02T03:42:45.630

Any errors displayed? Did move stop and ask you to overwrite some files with the same names? Also, did you check the Files folders that remained - what files were still in there? – Karan – 2013-02-02T16:33:57.953