Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
CuddleChunks
Sep 18, 2004

This is a very handy thread, thanks for making it! I just started learning Powershell a bit to automate some tasks at home. After reading the OP and some of the other examples in here I'm going to make some modifications to my script and then post it for all to laugh at.

Thanks for the cool info, powershell has been lots of fun to work with.

Adbot
ADBOT LOVES YOU

CuddleChunks
Sep 18, 2004

How do I properly use command-line arguments in a function? I would like to be fancy and do some error checking and whining at the user if they fail to give me the right parameters.
code:
Example command line: 
.\scriptname "c:\test" "d:\output"

Inside the file: 
function Fun {
	param(
	[parameter(position=0,Mandatory=$true,HelpMessage ="Need a Source Directory" )] $srcdir,
	[parameter(position=1,Mandatory=$true,HelpMessage="Need a Destination Directory")] $dstdir
	)
     write-host "srcdir:" $srcdir "dstdir:" $dstdir
}
$s = $args[0]
$d = $args[1]
Fun $s, $d
What happens every time is that the first parameter gets both of the command-line arguments separated by a space so the variable $srcdir is full of the complete command-line.

I've thrashed at this for a bit and it just doesn't seem to work like I expect. I've tried it like in the code above, calling the $arg[0] and $arg[1] right in the function call and still it acts stupid.

CuddleChunks
Sep 18, 2004

Okay, that makes sense now, thank you.

I was confusing where to use the parameter list and should have put that at the head of the script since I want to manipulate *those* parameters first before we ever get to the function declaration.

CuddleChunks
Sep 18, 2004

Very cool! So here we go, this is designed to work with the Windows version of Handbrake to convert video files that are on the Approved list over to iPod-ready files using the m4v file extension. Feed the script a source and destination directory and then one-by-one it will chew through your files in the source directory and convert them over using the designated Handbrake profile.

The Options section of code makes it easy to point the program at your install of Handbrake and change what profile you use or to update the list of approved files.

I hope this is handy for someone else because it's been a lifesaver for me. I've got tons of shows in AVI format that I want to watch on my iPod but don't want to point and click my way through each series. Now I just let my machine grind away all night on the files without me having to bother with a GUI.

Extra features:
- builds the destination directory if it doesn't exist.
- checks for existing files and skips them so you can safely run this against a directory that you download new content into and it will only process new files.
- spawns one process at a time and documents what file it's working on so that you can see what it's doing in the Powershell window.

code:
# Convert files in srcdir to designated output directory for iPod viewing using Handbrake
# Built 3/29/2011 by DCB  Modified 4/8/2011  Contact: [email]cuddlechunks@gmail.com[/email]
# Parameters: Source Directory, Output Directory
#
# Verify that we got the source and destination directories off the command line. 
param(
      [parameter(position=0,Mandatory=$true,HelpMessage ="Need a Source Directory" )] $srcdir,
      [parameter(position=1,Mandatory=$true,HelpMessage="Need a Destination Directory")] $dstdir
      )

###### OPTIONS #######
# Location of Handbrake CLI 
$HBloc = '"c:\program files\handbrake\handbrakecli"' 
# Name of Preset to use
$presetname = "iPhone & iPod Touch"
# File extensions for valid filetypes
$validfiles = ".avi", ".m4v", ".mp4", ".mkv"

function HBConvert 
{
	# Get input files, filter by approved extensions
	$srcfiles = dir -Path $srcdir | where {$_.psIsContainer -ne $true} | where {$validfiles -contains $_.extension}

	# Check for existence of output dir, create if missing
	if (Test-Path $dstdir) 
	   { echo "Output Path Exists" }
	else { echo "Making directory $dstdir"
	       mkdir $dstdir}

	# Process file list
	foreach ($movie in $srcfiles) 
	{
		$fname = [System.IO.Path]::GetFileNameWithoutExtension($movie)
		$fall = $dstdir + "\" + $fname + ".m4v"

		# Check for existence of output file, skip next step if exists
		if (Test-Path $fall)  
		{
		     echo "Skipping $fall, already exists."
		}
		else 
		{ # Process the files
		      $HBparams= " -i " + '"' + $movie.FullName + '"' + " -o " + '"' + $fall + '" --preset ' + $presetname
		      echo "Now converting: $fname"
		      [system.diagnostics.process]::start($HBloc,$HBparams).WaitForExit() 
		}
	} # end foreach
} # end HBConvert

HBConvert
This was a lot of fun to build, even with some of the frustrations that come with not having a formal book or training program for the language. Thanks to this thread and google I was able to puzzle out the above over the course of a few pleasant evenings. I've been using the script to chew through my files for a couple weeks now and hope it helps someone else.

CuddleChunks
Sep 18, 2004

adaz posted:

code:
$fall = "$dstdir\$fname.m4v"
:3: Awww, thanks powershell. Yeah, I'm coming from a background in procedural languages where it wouldn't occur to me to glob it all together like you have above. Still, I'll keep that in mind for the future. Also, I'll tweak the Echo statements since they worked during testing but I don't want to have something weird happen down the line.

Thanks for the tips!

Adbot
ADBOT LOVES YOU

CuddleChunks
Sep 18, 2004

I want to rewrite my powershell handbrake script in C# so I can slap a big dumb gui on it but look at that code up above. Just look at it.

:3: Oh powershell, you're so pretty.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply