PS: Trim File

This is a quick script to remove the last bytes from a given file.


Requirements


Usage

Call the script with a file name and the ammount of bytes to trim of at the end of the file.


Script code:

	
##------------------------------------------------------------------------------------------------
##
##  Trim-File.ps1
##
##   Version 1.0.0
##
##   
##   Copyright (c) 2017 Martin Mueller - www.sh-soft.com
##   
##   Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
##   and associated documentation files (the "Software"), to deal in the Software without 
##   restriction, including without limitation the rights to use, copy, modify, merge, publish, 
##   distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
##   Software is furnished to do so, subject to the following conditions:
##   
##   The above copyright notice and this permission notice shall be included in all copies or 
##   substantial portions of the Software.
##   
##   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
##   BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
##   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
##   DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
##   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##   (The MIT License (MIT))
##
##------------------------------------------------------------------------------------------------
<#
.SYNOPSIS
This script can be used to trim file ends...

#>

param( 
	[Parameter(Mandatory=$TRUE)] 
		[String] $FileName, 
	[Parameter(Mandatory=$TRUE)] 
		[int] $TrimLastBytesCount
)

try {
	$TrimFile = [IO.File]::OpenWrite("$FileName")
	$TrimFile.SetLength($TrimFile.Length - $TrimLastBytesCount)
	$TrimFile.Close()
	$TrimFile.Dispose()
}
catch {
	Write-Error -Message "Failed to trim last $TrimLastBytesCount bytes from file: `"$FileName`""
	$TrimFile.Close()
	$TrimFile.Dispose()
}

	

Download the script:

>> Version 1.0.0 (current)
(MD5: 77061529f7a731b67591f65e1e6caa8e)
(SHA1: aedfd877482fec0d5d8d75bb48e3db48b29670e8)
(SHA256: 34fc4067a1c54b2aaa0e08d1c854b2dc4078e5ceaf26197f19c129083dc10a0c)


>> syntax highlighting powered by highlight.js