Question

I'd like to move all files located in all second level subdirectories that have a specific file extension.

Here's what I have:

cd D:\Media\Unsorted
if not exist Movies mkdir Movies
move Movies-Downloaded\*\*.mp4 Movies\

Is there something wrong with my 'move' syntax? How could I achieve this?

Was it helpful?

Solution

Yes, there is something wrong. You can use wildcards only on the last element of the indicated path. And you are using wildcards in the last element (the .mp4 files) and in the previous element (the folder where the .mp4 files are located). This is not allowed.

You have to enumerate the folders and then, for each one, move the files in it to the target folder.

pushd D:\Media\Unsorted
if not exist Movies mkdir Movies
for /d %a in ("*") do if /i not "%~nxa"=="Movies" move /y "%~fa\*.mp4" "Movies"
popd

To include the previous commands in a batch file, percent signs must be doubled, that is, replace each % with %%

What it does is for each directory (for /d %a where %a will hold a reference to the directory being processed) in the current directory (pushd), if the name and extension of the directory (%~nxa) is not equal to the target directory, then, move all .mp4 files from the source directory using its full path (%~fa) to the target.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top