Richard's profileRichard Siddaway's BlogPhotosBlogLists Tools Help

Blog


    November 05

    Creating Temporary files

    One thing I need to do when testing file system scripts is generate a bunch of temporary files for experimenting with.  In the past I have just copied in whatever I could find to use as test data.  It is possible to easily generate test data of this sort using the New-TempFile function below.

    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    ## asssume that want temp files in multiples of 1KB or empty
    function New-TempFile {
    param (
    [string]$path = "C:\Test",
    [int]$size = 1KB
    )
    if (!(Test-Path -Path $path)){throw "Invalid path"}
    $file = [System.IO.Path]::GetRandomFileName()

    if ($size -eq 0){
    New-Item -Path $path -Name $file -ItemType 'File'
    }
    else {
    $data = "*" * 128
    $lines = [System.Math]::Ceiling(($size / 1kb) * 8)
    for ($i=1; $i -le $lines; $i++){
    Add-Content -Path $(Join-Path -Path $path -ChildPath $file) -Value $data
    }
            Get-ChildItem -Path $path -Filter $file
    }
    }

     

    The function takes two arguments – a path and a file size. My default values are given but they can be easily changed to suit you system.

    After checking that the path exists I generate a random file name using GetRandomFile().  We could use GetTempFileName() but that creates an empty file in the temporary folder which isn’t quite what I want.

    If the size is zero an empty file is created on the given path. Otherwise if size is supplied e.g.

    New-TempFile -size 5kb

    then a number of 128byte lines are written into the file.  If larger files are being routinely created increase the size of the string to 512 bytes – or put a test on say 100kb size and use the bigger string in that case.

    Add-Content is used to write the data into the file.

    One enhancement is to allow the setting of the file extension.

    Technorati Tags: ,,

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.
    Richard Siddaway has turned off comments on this page.

    Trackbacks

    The trackback URL for this entry is:
    http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!2595.trak
    Weblogs that reference this entry
    • None