mkdir and it’s limitations

OK, so ScriptingGames are started. First task looks pretty cool. I’m not very familiar with working on a registry keys using PowerShell, so it’s great opportunity to start using it also for reg-related tasks. First problem I went into is mkdir behaviour within this PRProvider.

On filesystem it’s not very hard to create even longest path, just type:

mkdir c:temponetwothreefourfive

Everything (maybe except drive) might not be there, command will create full path. Not so in registry… So first thing I did was prepare something to check if given key exists and prepare all folders (keys) from top to bottom. I’m not sure if my way is the best, probably not. Has two main advantages: it works and it’s MINE! 😀

So let’s assume $RegistryPath does not exist. What should I do?

Well, no I know, use –Force parameter…. 😉 But I first tried harder way… 😀
See below:

# $RegistryPath - path to create
# $Check-RegPath - function that returns 2 if path is invalid, 1 if it's valid but does not exist, 0 if it exists
$RegistryParent = $(Split-Path $RegistryPath)
$RegLeafs= $RegistryPath.Split("")
$i = $RegLeafs.Count - 1
do {
    $RegFlag = Check-RegPath $RegistryParent
    if ($RegFlag -eq 1) {
        # Time to move from top to bottom
        $RegistryParent = $(Split-Path $RegistryParent)
    } elseif ($RegFlag -eq 0) {
        # Now it's time to move from bottom to top
        do {
            Write-Debug "$RegistryParent <= folder; $i <= index"
            $RegistryParent = Join-Path $RegistryParent -ChildPath $RegLeafs[$i]
            mkdir $RegistryParent
        } while ($RegistryParent -ne $RegistryPath) 
        Write-Debug "Done!"
} while ($RegFlag -ne 0)

Now what left is:

* take care about ACL (Yak! I’m admin, you see, I don’t really care! 😉 )

* extend error/ information handling


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s