PowerShell:- File System – FileInfo

Background

Determining whether a file is empty could involve a few application logic-specific steps or more generic API calls.

Here is a sample code in Powershell.

 

Script

Outline

  1. Command Line Arguments
    • file
  2. Sanitize Variables
    • file
      • If the file is not specified, raise an exception
        • Exception Type:- System.ArgumentNullException
      • If the specified file does not exist, raise an exception
        • Exception Type:- System.IO.FileNotFoundException
  3. Process File
    • get-item
      • Using get-item get a handle to the file
      • Save handle into an object of System.IO.FileInfo
    • Get file’s length
    • Beautify file length

Code


param ( `
		    [Parameter(Mandatory = $true)] `
		    [string] $file`
		

      )
	  
<#
	Establishes and enforces coding rules in expressions, scripts, and script blocks.
#>
Set-StrictMode -Version latest

<#
	Declare Variables
#>
[string] $log = "";
[Boolean] $bFileExistence = $false;
[System.IO.FileInfo] $objFileInfo = $null;
[long] $fileSizeInBytes =0;
[string] $fileSizeAsString="";

[string] $FORMAT_FILE_FILENAME_NOT_SPECIFIED = "";
[string] $FORMAT_FILE_DOES_NOT_EXISTS = "";
[string] $FORMAT_FILE_OPERATION = "";
[string] $FORMAT_FILE_SIZE = "";
[string] $FORMAT_FILE_SIZE_AND_METRIC = "";

<#
	Set global variables
#>
$FORMAT_FILE_FILENAME_NOT_SPECIFIED = "File not specified";
$FORMAT_FILE_DOES_NOT_EXISTS = "File ( {0} ) does not exist!";
$FORMAT_FILE_OPERATION = "Reviewing file:- {0}";
$FORMAT_FILE_SIZE = "`t File Size: {0}";
$FORMAT_FILE_SIZE_AND_METRIC = "`{0:n3} {1}";

function beautifyFileSize{
param
( 
   
    [Parameter(Position=0)]
    [long] $fileSizeInBytes
   
  , [Parameter(Position=0)]
    [ref]$result    
) 

	[double] $fileSize = 0;
	
	if ($fileSizeInBytes -gt 1E9 )
	{
		$fileSize = $fileSizeInBytes /  ( 1024 * 1E6 );
		
		$result.value = $FORMAT_FILE_SIZE_AND_METRIC -f $fileSize, "GB";
		
	}	
	elseif ($fileSizeInBytes -gt 1E6 )
	{
		$fileSize = $fileSizeInBytes / ( 1024 * 1E3 );
		
		$result.value = $FORMAT_FILE_SIZE_AND_METRIC -f $fileSize, "MB";

	}
	
	elseif ($fileSizeInBytes -gt 1E3 )
	{
		$fileSize = $fileSizeInBytes / 1E3;
		
		$result.value = $FORMAT_FILE_SIZE_AND_METRIC -f $fileSize, "KB";
	}
	else
	{
	
		$result.value = $FORMAT_FILE_SIZE_AND_METRIC -f $fileSizeInBytes, "Bytes";
		
	}	
	
}

try
{
	Write-Host "";
	
	<#
		If file is not specified, raise an exception
	#>
	if ( ($file -eq $null) -or ($file -eq "") )
	{
		
		$log = $FORMAT_FILE_FILENAME_NOT_SPECIFIED;
		
		throw [System.ArgumentNullException] $log
	}

	
	$log = $FORMAT_FILE_OPERATION -f $file;
	Write-Host $log;
	
	<#
		Using Test-Path, determine if file exists
	#>
	$bfileExistence = Test-Path -LiteralPath $file -PathType Leaf;

	<#
		If Specified File does not exist!
	#>
	if (
			( $bfileExistence -eq $false )
	   )
	{
		
		$log = $FORMAT_FILE_DOES_NOT_EXISTS -f $file;

		throw [System.IO.FileNotFoundException] $log
		
	}


	
	<#
		Get File
	#>	
	$objFileInfo = get-item -LiteralPath $file;

	<#
		Get File Length
	#>	
	$fileSizeInBytes = $objFileInfo.Length;
	
	
	<#
		Beautify File Size
	#>	
	beautifyFileSize `
		-fileSizeInBytes ( $fileSizeInBytes ) `
		-result ([ref] $fileSizeAsString)
	
	<#
		Display File Size
	#>		
	$log = $FORMAT_FILE_SIZE -f $fileSizeAsString;
	Write-Host $log
	

	$objFileInfo = $null;
	
}
catch
{

	<#
		InvocationInfo
	#>
	#$PSItem.InvocationInfo | Format-List *

	<#
		Rethrow the current exception
	#>
	throw $PSItem
	
}


Invoke



setlocal

set _errorLevelSave=0

set "_filename=\hrdb-devi$SQL2010hrdb_Data.mdf"

set "_appFolder=."

set "_appScript=fileSystem.file.info.ps1"

set "_appScriptFullName=%_appFolder%%_appScript%"

echo %_appScriptFullName%

powershell %_appScriptFullName% -file '%_filename%'

rem Save Errorlevel
set _errorLevelSave=%ERRORLEVEL%

IF %_errorLevelSave% NEQ 0 (

	echo errorlevel is %_errorLevelSave%
	
)

endlocal

Output

Output – Image

Output – Text


>fileSystem.file.info.singlequotes.cmd
.\fileSystem.file.info.ps1

Reviewing file:- \hrdb-devi$SQL2010$PRD$DATAhrdb_data.mdf
         File Size: 18.595 GB

>


Source Code Control

GitHub

Gist

DanielAdeniji/fileSystem.file.info.ps1
Link

 

References

  1. Microsoft
    • Docs
      • Docs > PowerShell > Scripting > Reference > Microsoft.PowerShell.Management

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s