Tag Archives: FileSystem

Friday Fun: Creating Sample Files

documentNot too long ago I came across a PowerShell snippet, probably via Twitter, that showed how to use a few classes from the System.IO namespace to create dummy files. Sadly I forgot the record where I saw this first mentioned. What I liked about the code I saw was the ability to create a dummy file of any size. You’re probably wondering why you would ever want to do this. Way, way back in the day when storage was expensive I used to create a few 100MB of dummy files using an old command line utility which I think was called creatfil.exe. The reasoning was that if the server started running out of disk space unexpectedly, I could delete the dummy files and buy myself a little time and breathing room. Fortunately, I think we’ve advanced where that technique is no longer necessary.

But, where you might want some dummy or sample files, is to simulate a user folder with a variety of files. You could use this test directory to hone your PowerShell scripts. Any you may have your own reasons for needing a bunch of sample files. So I took the original code and ran with it, as I usually do. I came up with a function called New-SampleFile.

The function will create a randomly sized file in the specified folder. The default is the current location. As you look through the code you’ll see I’ve added a test to verify the path and to make sure it is a FileSystem path and not something like the registry. Also by default the function will use a randomly generated file name which will be something like rcstwqyg.34e. Personally, I’m going to specify a filename. The function will create the file of a random size between 5 bytes and the value of the MaximumSize parameter, which has a default of 100KB.

The function will not overwrite existing files unless you use –Force. Nor will you see any output unless you use –Passthru. Even those are common parameters, I had to write the PowerShell expressions to implement them. The same is true of –WhatIf. You’ll see in the cmdletbinding attribute that the function supports ShouldProcess. However, the .NET classes I’m calling have no concept of WhatIf. I had to handle that myself

Here’s the function in action:

This also makes it easy to create multiple files.

Or how about this? I want to create a variety of files with different names.

Now I have some test files I can work with

Normally I prefer to use cmdlets wherever possible. But since there is no cmdlet from Microsoft to create dummy files, I have no problem resorting to the .NET Framework. However, I think it is still worth the time to wrap the .NET commands in a user-friendly and easy to use advanced function as I’ve done today.

If you find a way to use this function, I hope you’ll let me know.

Friday Fun: The Measure of a Folder

rulerLast week, I demonstrated how to measure a file with PowerShell. This week let’s go a step further and measure a folder. I’m going to continue to use Measure-Object although this time I will need to use it to measure numeric property values.

Here’s the complete function after which I’ll point out a few key points.

The command will use the current path or you can pipe in a directory name, or the output of a Get-ChildItem expression as I show in the help examples. One thing I added is that I test the path to make sure it is a file system path because anything else wouldn’t really work.

I resolve the path so that I can get the actual name of the current location (.) and then test the provider. The other interesting feature of this function is that I format the results on the fly.

The function has a Unit parameter which has a default value of bytes. But you can also specify one of the PowerShell numeric shortcuts like KB or GB. In the function I use a Switch construct to create a custom property on the fly.

Because there can only be a single value for $Unit, I’m including the Break directive so that PowerShell won’t try to process any other potential matches in the Switch construct. Realistically, there’s a negligible performance gain in this situation but I wanted you to see how this works.

measure-folder

I’ve taken some basic commands you could use interactively such as Get-ChildItem and Measure-Object and built a tool around them using a hashtable of properties to create a custom object. I hope you pick up a tip or two from this. If you have any questions about what I’m doing or why, please don’t hesitate to ask because someone else might have the same question.

Enjoy your weekend!

Get Top Level Folder Usage

This is too long to tweet, even written as a one liner. But this will search a folder for top level subfolders and return the file usage for each.

[cc lang=”PowerShell”]
$folder=”S:\”
dir $folder | where {$_.psIscontainer} | foreach {
$stat= dir $_.fullname -recurse | Measure-Object -property length -Sum
New-Object PSObject -property @{Folder=$_.FullName;Files=$stat.count;Size=$stat.Sum;}}
[/cc]

Again, this would be handy to turn into a function or scriptblock. This is far from perfect. For example, it doesn’t like empty folders. But this should be a good starting point for your own script or function.