Solved Moving files with specific extensions to Subfolder

November 4, 2012 at 10:25:53
Specs: Windows 7
I would like to move the files .avi and .mov of a folder to a subfolder that has to be created and called Videos.

for example. It looks like that now.

----Subfolder1 (different files and extensions)
----Subfolder2 (different files and extensions)
----Subfolder3 (different files and extensions)

to how it has to be after

----Subfolder1 (different files and extensions)
-------Videos (the files from the Subfolder that have the extensions .avi and .mov)
----Subfolder2 (different files and extensions)
-------Videos (the files from the Subfolder that have the extensions .avi and .mov)
----Subfolder3 (different files and extensions)
-------Videos (the files from the Subfolder that have the extensions .avi and .mov)

I am already trying that for hours and dont really get satisfying results.

And.. if its not too complicated (just an option if its easy to implement). Can the script check if in that Subfolders with the .avi and .mov files are any other files with the ending .jpg then it should just do what I mentioned above and move tehm to Videos. If there is no other file extension but only .avi and .mov it should not move them to the Videos folder but just leave them in that folder where they already are.

I know thats a bit complicated. So I would be very happy if the script at least could do the main moving stuff withouth that specifc distinction. THANKS A LOT

See More: Moving files with specific extensions to Subfolder

Report •

November 4, 2012 at 10:58:33
✔ Best Answer
here's a quick prototype that just handles part one, pending further refinements as per your second request...

@echo off
for /d %%a in (\mainfolder\*.*) do (
md %%a\videos
move %%a\*.mov %%a\videos
move %%a\*.avi %%a\videos
::I added these two parts to handle situations of ALL files moved, and NO files moved
set c=
:: ALL files were moved, so move them back
(for /f %%b in ('dir /b %%a') do set c=1) || move %%a\videos\*.* %%a
:: note the RD will fail if any files exist in 'videos' subd
rd %%a\videos 2>nul
::==== end main part
I know it seems silly to move them all, then move them back, but that was the easiest way to handle it and I'm lazy. The moving does not involve much overhead because only the files' names (directory entries) are modified. The files' content is not moved or copied. I'm unclear about the .jpg stuff. Maybe you can clarify that somewhat.

Report •

November 4, 2012 at 11:31:20
Worked great thanks a lot...
I want to copy that .bat file into the mainfolder and let the script then apply this move command to ALL folders he finds underneath. So that I dont need to change the script each time anymore but just start it inside a specific folder that I have copied it to.

And again.. thank you very much!

Report •

November 4, 2012 at 11:40:26
Well, this script should run through all the subfolders off of "mainfolder", without any need for modification. (refer to output: FOR /? page 1 under FOR /D).
If you have multiple "mainfolders" then the script could probably be modified to handle that, but I would need to know more about the directory structure in relation to the mainfolders.

Report •

Related Solutions

November 4, 2012 at 12:32:17
ahh about that .jpg thing
I wanted to prevent the files .avi and .mov to be moved to "Videos" if they were already alone in some folder, without any other files with different extensions.
And only be moved to "Videos" if there were some files with .jpg extensions with them inside a folder. But thats not really important. (and probably by far more complicated to resolve, maybe with VBS only)

And the folders just look like that.

C:\Traveling\Germany and then are devided thru subfolders by year,month and day.
C:\Traveling\Germany\2012\10\01\ and so on...
So I just copy the .bat to Germany for example and he then makes that move actions to all folders underneath.

But your first batch code already did the job. And I am trying to understand the whole thing right now. And the logic behind that commands and so on.

Report •

November 4, 2012 at 17:07:01
Oh! I understand i think. From the looks of things, you're done with this project, but fwiw, I think this would have done a multiple-level move from c:\traveling\germany:
for /r /d %%a in (c:\traveling\germany\*.*) do (... etc
for /f "tokens=*" %%a in ('dir /b /s /a:d c:\traveling\germany') do (... etc

Report •

November 5, 2012 at 03:31:47
And again thanks a lot :)
I am not a very experienced coder or somehting. But I was able to understand some other script languaes. Dont know why I am just too stupid on this one.. hehe
I am gonna find out how to delete specific files now, on my own ;)

Report •

Ask Question