Page 1 of 1

Script help

Posted: Thu Apr 07, 2011 6:53 am
by captain_twinkie
So I have ripping a lot of movies and shows for my media server, and I have a bunch of movies that are not found in IMDB, but I found a app that can meta tag for me, the problem is the videos need to already in a folder that is named the movie.

So I am not sure if there is a app out there that can do this, or if there is a batch script I can run to do this.

I essentially need it to look at the files that I have, create a folder for it, and then move the file to that folder.

Like A.avi to Folder A
B.avi to Folder B

etc

Re: Script help

Posted: Fri Apr 08, 2011 10:41 am
by snoopy
I suppose you're talking about windows, huh?

for linux, cd to the directory that contains the avi's:

Code: Select all

#!/bin/bash

ls | grep .avi | while read -r FILE

do
DIR="(echo "$FILE" | sed 's/\(.*\)\..*/\1/')"
mkdir $DIR
mv $FILE $DIR
done
Or something of the like. I just whipped out the code, and copied the sed part, so you should test it and tweak before you just run it on all on your files. I'm not sure how it will handle files that have multiple periods in the filename (I think it will cut everything off after the first period- test it with an "echo $DIR" and the mkdir and mv commands commented out to see how it processes different characters, esp. spaces and periods.)

If you're in windows, a POSIX shell, like cygwin, will handle this script properly.

[EDIT]Tutorial on sed: http://www.grymoire.com/Unix/Sed.html

Re: Script help

Posted: Fri Apr 08, 2011 12:12 pm
by Jeff250
To make it robust to file names with arbitrary white space:

Code: Select all

#!/bin/sh

for FILE in *.avi
do
    DIR="$(echo "$FILE" | sed 's/\(.*\)\..*/\1/')"
    mkdir -p "$DIR"
    mv "$FILE" "$DIR/"
done
MinGW also provides a POSIX shell on Windows.

There's also this thing called "Windows Power Shell" that I know nothing about, but if it's worth anything, it will support regular expressions.