Deprecated: Function jetpack_form_register_pattern is deprecated since version jetpack-13.4! Use Automattic\Jetpack\Forms\ContactForm\Util::register_pattern instead. in /home4/robohara/public_html/www.robohara.com/wp-includes/functions.php on line 6078
Moving Files into Folders [PowerShell] « RobOHara.com

Moving Files into Folders [PowerShell]

Lately I’ve been going back through some of my old DVDs and Blu-rays and re-ripping the ones where I missed commentary tracks or special features. The ultimate goal is to put each movie into its own folder (along with any associated files, like subtitle tracks or movie posters), but I tend to let them pile up for a while in one place which ends up in a big house cleaning mess when it’s time to organize them all.

I’ve been doing a lot of PowerShell scripting at work, and have started writing a few scripts for my home computer/network as well. This seemed like it would be a pretty helpful and easy script to write, which it was.

The first thing the script needs to do is scan a folder folder for all video files. I’m ripping most of my files to .mkv these days, but occasionally I still have some .mp4 and .avi files that sneak in there, so I created a for loop to search for all three.

Once the script has collected all the movie files, it creates a folder with that same name, and then moves all the files matching that name into the folder. For example, let’s say there are six files in my movie directory:

Movie One.mkv
Movie Two.mp4
Movie Two.jpg
Movie Three.avi
Movie Three.jpg
Movie Three.sub

The script scans the directory for all the movie files (Movie One.mkv, Movie Two.mp4, and Movie Three.avi). Then it creates three folders (Movie One, Movie Two, and Movie Three) and moves “Movie One.*” into the Movie One folder, etc. The most helpful part is that the script moves all the Movie Three files into the Movie Three folder.

As with all my scripts, there are probably shorter and possibly cleaner ways to achieve this same result, but this is what I came up with.

$extensions = "mkv","avi","mp4"
foreach ($extension in $extensions) {
$Folders = Get-ChildItem -Name *.$extension
foreach ($Folder in $Folders) {
$Folder = $Folder -replace ".$extension", ""
new-item $Folder -itemtype directory
Move-Item -Path .\$Folder*.??? -Destination .\$Folder
}
}

The script works like a champ!

2 comments to Moving Files into Folders [PowerShell]

.xX[ MY INFO/LINKS ]Xx.

My EMAIL
My RSS FEED
My SUBSCRIPTION (Blog)
My Twitter
My YouTube

My Books
My Portfolio
My Podcasts
Review-O-Matic (Reviews)

.xX[ SUB-PAGES ]Xx.

My ARCADE GAMES
My SOFTWARE
My PHOTO GALLERY
My WRITING ADVICE
Every CAR I'VE OWNED
Every STATE I'VE VISITED

Latest Tweets