Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
showfiles.ps1
param ( `
[Parameter(Mandatory)] [string] $folder `
)
Set-StrictMode -Version latest
<#
Declare Variables
#>
#Only include files with txt and log extension
[string] $filesInclude = ".*\.txt.*|.*\.log.*"
[string] $CHAR_LINE = "=";
[String] $log = "";
[int] $fileNumber=0;
[Boolean] $bFolderExists = $false;
<#
Function
Name:- showFileContents
Arguments
$psPath
#>
Function showFileContent
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline)] $psPath
)
<#
Declare Variables
#>
Begin
{
[int] $rc = 0;
[string] $filenameFullName = $null;
[string] $filenameLeaf = $null;
}
<#
Process
#>
Process
{
#Increment file number
$script:fileNumber = $script:fileNumber + 1;
#Get FileName from PSPath
$filenameFullName = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($psPath)
#Get filename from full path
$filenameLeaf = Split-Path -Path $filenameFullName -Leaf -Resolve
#Display Space
Write-Host ""
Write-Host ""
#Display Filename
$log = "{0}a) Filename:- {1}" -f $script:fileNumber, $psPath;
Write-Host $log
#Display FilenameLeaf
$log = "{0}b) FilenameLeaf:- {1}" -f $script:fileNumber, $filenameLeaf;
Write-Host $log
#Get length of display log
$logLength = $log.length;
#Prepare Filename Divider
$log = $CHAR_LINE.PadRight($logLength, $CHAR_LINE)
#Display Filename Divider
Write-Host $log
#Get file contents
$fileContents = Get-Content -raw $psPath
#Display File Content
$fileContents
}
<#
Deallocate
#>
End
{
}
}
$bFolderExists = Test-Path -Path $folder;
<#
Get files in passed in folder
recurse sub-directory
only include files matching pattern
for each matching file
call function showFileContents
#>
if ($bFolderExists -eq $false)
{
$log = "Folder {0} does not exist" -f $folder;
Write-Error $log;
}
else
{
Get-Childitem -File $folder -Recurse |
Where-Object {$_.PSPath -match $filesInclude} |
foreach { $_.PSPath | showFileContent }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment