Calendar

<<  October 2017  >>
MonTueWedThuFriSatSun
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

RecentComments

None

 
 
     
 

A more powerful DirToXml. Don't go to sleep yet because this might get a bit more interesting as it goes on. I was learning powershell and decided to work on something that I knew well. Xml and one of the popular downloads from the Xml Xsl Portal, DirToXml. So here is a simple Powershell version dir2xml.zip (1.26 kb)

USAGE
    Get-DirAsXml -Indir <pathToDirectory>
SYNOPSIS
    Formats the directory tree as XML.
PARAMETERS
    -Indir <string[]>
        The directories to be processed
    -Outfile <string>
        Optional output file.
    -Rootname <string>
        Optional name of the root node.
    -Props <object>
        Optional hash table of file properties to include 
in the xml attributes. EXAMPLES Get-DirAsXml c:\temp Get-DirAsXml c:\temp xmlFile.xml Get-DirAsXml d:\week\log
, z:\arch\log
, c:\today\log
allSystemLogfiles.xml
    gci c:\temp | where {$_ -like '*xml*'} | Get-DirAsXml Get-DirAsXml c:\temp -p @{CreationTime="yyy-MM-dd";
                           LastAccessTime="yyy-MM-dd"}
    Get-DirAsXml hkcu:\software\microsoft

It is a fairly straightforward replacement for DirToXml and it can handle multiple input folders.
This is very useful if you have groups of files in many different places that you want to put into a supergroup. MP3 files! I have them scattered all over the place C:\music D:\library\music USB:\music CD:\music \\remoteshare\c$\music

The -props parameter takes a hash of name=value pairs seperated by ';'. The value is used for formatting so  CreationTime="yyy-MM-dd" will produce CreationTime="2000-01-01". An empty value will just passthrough the property to the attribute so Length="" will produce Length="101".

I started looking at file properties because I wanted more information about media files. You can get a lot of media information from the shell that isn't shown in the 'file' file properties. Things like Author, Dimensions, Camera , Audio Compresion etc. Anything you can see when you rightclick on a file and choose Properties. Some of these are the same as the normal file properties i.e. CreationDate but there are a few more that are useful. I added an ExtendedProps parameter.

Another set of property information is the file version information. This contains things like ProductName, ProductVersion, FileBuildPart and refer to executable and dll properties. I added a FileVersionInfo parameter.

That is nearly all the bases covered for properties but there are others. EXIF information for image files might be useful if you want the xml to be a base for a photo library. Some of this is available in ExtendedProperties as it is read by the shell but not all. Codec information for video files is useful if the xml will be used in a video library. GSpot is a very useful tool and can be used to output an attributes file for each of the files in a folder. So I added a CustomProps parameter which is a scriptblock that is used like

PS> Get-DirAsXml C:\Video -o videolibrary.xml -CustomProps {
    param ($element, $directory, $file, $prefix, $namespace)
        # read a file on disk and
        # calculate $calculatedvalue here
        [void]$element.SetAttribute("mycalculatedattribute"
            , $namespace
            , $calculatedvalue)
}

I sometimes use naming to indicate the type of file i.e. xxxxxxx.detail.xml This is my details file and it is also an xml file. FileType will tell you is is an XML Document but i want to know that it is a details file. Here is a handy CustomProps script to get that and it is used the same as above.

{param ($element, $directory, $file, $prefix, $namespace)
    $type = ""
    switch -regex ($file.Name){
        ".*.detail.xml$" {$type="Detail"}
        ".*.html$" {$type="Html"}
        ".*.playlist.xml$" {$type="Playlist"}
        ".*.summary.xml$" {$type="Summary"}
        ".*.ttaf.xml$" {$type="TTAF"}
        ".*.smi$" {$type="Subtitle"}
        ".*.srt$" {$type="Subtitle"}
        ".*.wmv$" {$type="Movie"}
    }
    [void]$element.SetAttribute("BBCType", $namespace, $type)
}

Sometimes people name files as MovieX.TS.XVID.AC3.Pirate.avi which indicates the codecs used. Getting this information from the name using a custom script is quicker than using GSpot or ExtendedProps and the shell.

With all of these properties coming from different places and with similar names I decided to add namespaces for attributes so I added -EPNamespace -EPPrefix, -VINamespace -VIPrefix, -CPNamespace -CPPrefix. You don't have to use them but if you are getting name clashes then they come in handy.

I wanted to know how this xml file was created and what parameters were used so i added an -IncludeMeta parameter. This is useful if you want to keep track of a lot of removable media. The USB stick can have a copy of Get-DirAsXml and autorun it when it is plugged in and save it to a central repository. Or you can run Get-DirAsXml manualy from a central repository each time a USB stick is connected or changed. That way you can keep track of all your removable media.

A friend has a whopping USB drive full of movies. I keep putting new stuff there, she watches it and deletes it. By doing xml diffs of her disk with the central repsitory I know what she has, what she has already seen and what I have just given her. It is a sort of sneakernet syncronisation using Get-DirAsXml and a USB stick.

Get-DirAsXml.zip (3.16 kb)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Add comment