Script help

For system help, all hardware / software topics NOTE: use Coders Corner for all coders topics.

Moderators: Krom, Grendel

Post Reply
User avatar
captain_twinkie
DBB Ace
DBB Ace
Posts: 222
Joined: Sun Mar 07, 2004 3:35 pm
Location: Orem, Utah

Script help

Post 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
User avatar
snoopy
DBB Benefactor
DBB Benefactor
Posts: 4435
Joined: Thu Sep 02, 1999 2:01 am

Re: Script help

Post 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
Arch Linux x86-64, Openbox
"We'll just set a new course for that empty region over there, near that blackish, holeish thing. " Zapp Brannigan
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6530
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: Script help

Post 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.
Post Reply