Second Stanza

December 27, 2008

Using IDisposable Objects in PowerShell

Filed under: Powershell — Tags: , , — dfbaskin @ 10:50 pm

In C#, you can easily dispose of objects by making use of the using statement, so long at the object implements the IDisposable interface.

So how do you do this in PowerShell? Fortunately, Adam Weigert and Keith Dahlby have done the hard work:

Thanks guys!

November 2, 2008

Powershell Script for Shrinking MP3 Files

Filed under: Powershell — Tags: — dfbaskin @ 10:45 pm

I needed to shrink a bunch of lectures in MP3 format so that they would fit on a DVD. I compiled SoX to include MP3 support and then used the following Powershell script to compress the files:


$srcPaths = 'E:\MP3s\Year One', 'E:\MP3s\Year Two';
$dstPath = 'E:\MP3s\DVD';
$pathPrefixLen = 8;

Write-Host;
Write-Host '--------------------------------------------------------------------';
Write-Host 'MP3 Audio Update Utility';
Write-Host '--------------------------------------------------------------------';

foreach( $srcPath in $srcPaths )
{
    Write-Host 'Source: ' $srcPath;
}
Write-Host '  Dest: ' $dstPath;
Write-Host;

foreach( $srcPath in $srcPaths )
{
    Write-Host 'Reading ' $srcPath;
    $fileList = Get-ChildItem -path $srcPath -Recurse *.mp3

    foreach( $srcFile in $fileList )
    {
        $srcFullName = $srcFile.FullName;
        $dstFullName = [System.IO.Path]::Combine( $dstPath, $srcFile.FullName.Substring( $pathPrefixLen ) );
        $tmpFullName = [Regex]::Replace( $dstFullName, '\.mp3$', '.__temp__.mp3', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase );

        if( ! [System.IO.File]::Exists( $dstFullName ) )
        {
            Write-Host $dstFullName;
    
            & 'sox.exe' $srcFullName '-r' '12k' '-c' '1' '-S' $tmpFullName 'dither' | out-null
            if( $lastExitCode -ne 0 )
            {
                Write-Host 'Stopped on error.'
                exit;
            }
    
            [System.IO.File]::Move( $tmpFullName, $dstFullName );
        }
    }
}

A file included in the SoX source code (‘INSTALL’) has instructions for building SoX with MP3 support under Windows. Here are the requirements (excerpted from this file):

Appendix: How To Compile SoX with MP3 Support on Win32
------------------------------------------------------

The following text, untested by the SoX team, was originally written by `Enter
AG' and released to the public domain.

= How To Compile Sox with MP3 Support =

== Requirements ==

The following requirements have been tested. Nevertheless, other versions may
work as well.

o Microsoft Windows (Win32, Win2k, Win2003).

o CMake (Cross Platform Make) version 2.4
  available on [http://www.cmake.org] 

o Microsoft Visual Stuio 2008 (also earlier Versions)

o SoX source distribution version 14.0.1
  available on [http://sox.sourceforge.net] 

o LAME source distribution version 3.97
  available on [http://lame.sourceforge.net] 

o MAD source distribution version 0.15.1b
  available on [http://www.underbit.com/products/mad/] 

May 23, 2008

Timing Script using Powershell

Filed under: Powershell — Tags: — dfbaskin @ 12:32 pm

Here’s an example of a simple Powershell script that can be used to created a tab-separated file of timing events.


#----------------------------------------------------------------------------
# Windows Powershell Timer.
#----------------------------------------------------------------------------

$timingName = "Timing";
$timingPoints = @(
    "Step One",
    "Step Two",
    "Step Three",
    "Step Four"
);

$resultFile = $timingName + ' Timing ' + [DateTime]::Now.ToString( "yyyy-MM-dd hh-mm-ss" ) + ".csv";

$timeStamps = @();
$textItems = @();
[DateTime] $timeStamp = [DateTime]::Now;

# Add timing message
function AddMsg( [string] $msgTxt )
{
    $SCRIPT:timeStamp = [DateTime]::Now;
    $SCRIPT:timeStamps += $SCRIPT:timeStamp;
    $SCRIPT:textItems += $msgTxt;
}

$resultTxt = @();
$resultTxt += "StartTime`tEndTime`tDuration`tTotalDuration`tEvent";

[DateTime] $startTimeStamp = [DateTime]::Now;
[DateTime] $prevTimeStamp = $startTimeStamp;
foreach( $timingPoint in $timingPoints )
{
    Read-Host "Press Enter";
    AddMsg( $timingPoint );
    Write-Host( $prevTimeStamp.ToString('yyyy-MM-dd hh:mm:ss.ffffff') + " / " +
                $timeStamp.ToString('yyyy-MM-dd hh:mm:ss.ffffff') + " / " +
                $timeStamp.Subtract( $prevTimeStamp ).ToString() + " / " +
                $timeStamp.Subtract( $startTimeStamp ).ToString() );
    $resultTxt += $prevTimeStamp.ToString('yyyy-MM-dd hh:mm:ss.ffffff') + "`t" +
                  $timeStamp.ToString('yyyy-MM-dd hh:mm:ss.ffffff') + "`t" +
                  $timeStamp.Subtract( $prevTimeStamp ).ToString() + "`t" +
                  $timeStamp.Subtract( $startTimeStamp ).ToString() + "`t" +
                  $timingPoint;
    $prevTimeStamp = $timeStamp;

    Write-Host( $timingPoint );
}

Write-Host;
Write-Host( "Created file:" );
Write-Host( $resultFile );
$resultTxt | Set-Content $resultFile;
Write-Host;



Blog at WordPress.com.