PoshCode Archive  Artifact [c86ff7c6fc]

Artifact c86ff7c6fcdee2bdf5fff5f05f252e071e6b1e09f5759013300054d11a3aaf5f:

  • File WriteFileName.ps1 — part of check-in [38a7a635d8] at 2018-06-10 13:16:26 on branch trunk — Write multiline overwriting messages, typically for iterating through long file names. (user: James Gentile size: 3549)

# encoding: ascii
# api: powershell
# title: WriteFileName
# description: Write multiline overwriting messages, typically for iterating through long file names.
# version: 0.1
# type: function
# author: James Gentile
# license: CC0
# x-poshcode-id: 2877
# x-archived: 2012-01-12T06:27:10
# x-published: 2012-07-28T17:31:00
# functions to print overwriting multi-line messages.  Test script will accept a file/filespec/dir and iterate through all files in all subdirs printing a test message + file name to demostrate.
# e.g. PS>.\writefilename.ps1 c:\
# call WriteFileName [string]
# after done writing series of overwriting messages, call WriteFileNameEnd

function WriteFileName ( [string]$writestr )                        # this function prints multiline messages on top of each other, good for iterating through filenames without filling
{                                                                   # the console with a huge wall of text.  Call this function to print each of the filename messages, then call WriteFileNameEnd when done
                                                                    # before printing anything else, so that you are not printing into a long file name with extra characters from it visible.
    if ($Host.Name -match 'ise') 
    { write-host $writestr; return }
    if ($global:wfnlastlen -eq $null) {$global:wfnlastlen=0}              

	$writelines = [math]::divrem($writestr.length+$cleft, [console]::bufferwidth, [ref]$rem)
	#if ($rem -ne 0) {$writelines+=1}

    $cwe = ($writelines-(([console]::bufferheight-1)-$ctop))                                       # calculate where text has scroll back to.
    if ($cwe -gt 0) {$ctop-=($cwe)}
	write-host "$writestr" -nonewline    
	if ($global:wfnlastlen -gt $writestr.length)
		write-host (" " * ($global:wfnlastlen-$writestr.length)) -nonewline                    # this only overwrites previously written text if needed, so no need to compute buffer movement on this
	$global:wfnlastlen = $oldwritestrlen

    if ($ctop -lt 0) {$ctop=$cleft=0}
function WriteFileNameEnd ( $switch=$true)                                                      # call this function when you are done overwriting messages on top of each other
{                                                                                               # and before printing something else.  Default switch=$true, which prints a newline, $false restores cursor position same line.
    if ($Host.Name -match 'ise') 
    { return }
    if ($global:wfnoldctop -ne $null -and $global:wfnoldcleft -ne $null) 
        if ($global:wfnoldcleft -ne 0 -and $switch)
            write-host ""

    write-host "Checking: " -nonewline
    dir $args -recurse -ea 0 -force | %{WriteFileName ("$($_.fullname) ..."*(get-random -min 1 -max 100))}
    #WriteFileName "Final Test String."
    write-host "Done! exiting."