-1

I have many files on a folder and I want to move them to a folder that matches part of its name.

Example:

[PartOfItsName] Season01Episode02 (04.04.16).mp4
[PartOfAnotherName] Season02Episode02 (05.02.16).mp4
[AndAnotherOne] Season03Episode04 (02.01.16).mp4

After moved I want something like this:

C:/[PartOfItsName]/[PartOfItsName] Season01Episode02 (04.04.16).mp4
C:/[PartOfAnotherName]/[PartOfAnotherName] Season02Episode02 (05.02.16).mp4
C:/[AndAnotherOne]/[AndAnotherOne] Season03Episode04 (02.01.16).mp4

The names of the files will not be modified, only folders with the name between the brackets needs to be created and then move the files to their respective folders.

RogerHN
  • 584
  • 1
  • 11
  • 31
  • 1
    Possible duplicate of [Batch create folders based on part of file name and move files into that folder](http://stackoverflow.com/questions/19992530/batch-create-folders-based-on-part-of-file-name-and-move-files-into-that-folder) – aschipfl May 12 '16 at 09:11

2 Answers2

1

In PowerShell you could do it with:

dir *.mp4 | mv -Dest { [regex]::Match($_, '\[.*\]').Value }

This might work in a batch file:

dir /b *.mp4 > tmp
for /f "tokens=1,2 delims=]" %%f in (tmp) do (
    mkdir "%%f]"
    move "%%f]%%g" "%%f]"
)
del tmp

NB. For both versions, every .mp4 file must have one [..] section, and no square brackets anywhere else in the name.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
1

This batch script works even if there are additional characters before the opening [.

@echo off
for %%F in (*[*]*.mp4) do for /f "delims=[] eol=[ tokens=2" %%A in ("x%%F") do (
  md "c:\[%%A]" 2>nul
  move "%%F" "c:\[%%A]" >nul
)

It could be done easily enough on the command line as a farily long one-liner:

for %F in (*[*]*.mp4) do @for /f "delims=[] eol=[ tokens=2" %A in ("x%F") do @md "c:\[%A]" 2>nul&move "%F" "c:\[%A]" >nul
dbenham
  • 127,446
  • 28
  • 251
  • 390