Copy files based on folder name

July 4, 2010 at 21:16:05
Specs: Windows 7

I am trying to write a batch script for a media center pc. The idea of this script is to copy all of the avi files with keywords S0 S1 S2 etc and 1x 2x 3x to a TV folder and all the rest of the files to a Movie folder.

I am sure my attempts to write this are very laughable as I have no idea what I am doing and have b---tardized a few scripts I found on this site. The result is something that sort of works. The problem is if there are two avi files (Someshow S22E01.avi and Someshow S22E02.avi) in the directory that is pushd at the start then the first one is 'filtered' correctly and ends up in the TV folder but the second one ends up in the movie folders. I am sure this a result of me not understanding the code I am writing.

Below is a copy of the script, any help would be much appreciated as this is the final step in a fully automatic media center. Thanks

pushd D:\Unsorted Media\Media
FOR /F "tokens=*" %%G IN ('DIR /B /S *S0*.avi *S1*.avi *S2*.avi *S3*.avi *S4*.avi *S5*.avi *S6*.avi *S7*.avi *S8*.avi *S9*.avi *S10*.avi *S11*.avi *S12*.avi *S13*.avi *S14*.avi *S15*.avi *S16*.avi *S17*.avi *S18*.avi *S19*.avi *S20*.avi *S21*.avi *S22*.avi *S23*.avi *S24*.avi *S25*.avi *1x*.avi 2x*.avi *3x*.avi *4x*.avi *5x*.avi *6x*.avi *7x*.avi *8x*.avi *9x*.avi *10x*.avi *11x*.avi *12x*.avi *13x*.avi *14x*.avi *15x*.avi *16x*.avi *17x*.avi *18x*.avi *19x*.avi *20x*.avi *21x*.avi *22x*.avi *23x*.avi *24x*.avi *25x*.avi S0*.avi S1*.avi S2*.avi S3*.avi S4*.avi S5*.avi S6*.avi S7*.avi S8*.avi S9*.avi S10*.avi S11*.avi S12*.avi S13*.avi S14*.avi S15*.avi S16*.avi S17*.avi 18*.avi S19*.avi S20*.avi S21*.avi S22*.avi S23*.avi S24*.avi S25*.avi 1x*.avi 2x*.avi 3x*.avi 4x*.avi 5x*.avi 6x*.avi 7x*.avi 8x*.avi 9x*.avi 10x*.avi 11x*.avi 12x*.avi 13x*.avi 14x*.avi 15x*.avi 16x*.avi 17x*.avi 18x*.avi 19x*.avi 20x*.avi 21x*.avi 22x*.avi 23x*.avi 24x*.avi 25x*.avi') DO (
move "%%G" "D:\Unsorted Media Copy\TV Shows"

pushd D:\Unsorted Media\Media
FOR /F "tokens=*" %%G IN ('DIR /B /S *a*.avi *b*.avi *c*.avi *d*.avi *e*.avi *f*.avi *g*.avi *h*.avi *i*.avi *j*.avi *k*.avi *l*.avi *m*.avi *n*.avi *o*.avi *p*.avi *q*.avi *r*.avi *s*.avi *t*.avi *u*.avi *v*.avi *q*.avi *x*.avi *y*.avi *z*.avi') DO (
move "%%G" "D:\Unsorted Media Copy\Movies"

See More: Copy files based on folder name

Report •

July 4, 2010 at 21:17:11
Oh and because the shows/movies are sometimes in there nested in their own folder which is not required it needs to to also go through the sub folders of the pushd directory.

Report •

July 4, 2010 at 21:38:20
i think it could be simplified and made more efficient. That's a LOT of repetitive code! A sample of actual filenames (ie: formats, not ness. need be actual) would help.

Report •

July 4, 2010 at 22:00:09
Filenames would be those resulting from torrent downloads, e.g. The Simpsons S01E01 HDTV xVid.avi.

My idea was to 'filter' these based on having S0 or 1x in them as most tv shows have the season and episode listed as either SyyEzz (S01E01) or yxzz 1x01. Then all other avi files would be seen as movies and put to the movie folder.

(If your interested the resulting folders are then watched by eventghost which launches therenamer.exe with a command line based on whether it is the movie folder or the tv folder which searches imdb/tmdb or thetvdb and does the renaming and moves to my main movie/tv folders).

Report •

Related Solutions

July 4, 2010 at 22:29:39
maybe like this?
first, the trial/test version:
pushd "d:\unsorted media\media"
for /f "tokens=*" %%a in ('dir /b /s s??e??.avi') do echo to be moved: "%%a"
echo stage 2:
for /f "tokens=*" %%a in ('dir /b /s *x??.avi') do echo to be moved: "%%a"
"run" version
pushd "d:\unsorted media\media"
for /f %%a in ('dir /b /s s??e??.avi') do move "%%a" ...
for /f %%a in ('dir /b /s *x??.avi') do move "%%a" ...

Report •

July 4, 2010 at 22:32:55
Thanks very much, I will check it out when I get home. Really appreciate the help!

Report •

July 6, 2010 at 05:05:18
Findstr's regexp pattern matching caps should be
ideal for this kind of thing. Try the following (regexp may
need a little tweaking depending on actual filenames mix
but be should be a good starting point based on what
you said):

@echo off

setlocal EnableDelayedExpansion

for /r "D:\Unsorted Media\Media" %%f in (*.avi) do (
  echo %%~nf| findstr /i /r "s[0-9][0-9]e[0-9][0-9] [0-9]x[0-9][0-9]"> nul
  if !errorlevel! neq 1 (
    move "%%f" "D:\Unsorted Media Copy\TV Shows"
  ) else (
    move "%%f" "D:\Unsorted Media Copy\Movies"

Report •

Ask Question