Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Get-TooLongFileName
- {
- #region Comment Based Help [CBH]
- # trailing blank line required by CBH
- <#
- .SYNOPSIS
- Gets the names of files that are longer than specified.
- .DESCRIPTION
- Uses Robocopy [with /L to avoid any changes] to get file names that are longer than specified.
- Scans the entire folder tree starting at the $Source location.
- Returns [a] $Null [no files]
- [b] a string [one file]
- [c] an array of strings [two or more files]
- .EXAMPLE
- Get-LongFileName -Source C:\temp
- Scans the folder tree starting at C:\temp.
- Returns any file names that are longer than the default of 250 characters.
- .EXAMPLE
- Get-LongFileName -Source C:\temp -MaxFQFN_Length 90
- Scans the folder tree starting at C:\temp.
- Returns any file names that are longer than 90 characters.
- .NOTES
- Version = 2017-02-24_-_21-40
- Robocopy Documentation
- - https://technet.microsoft.com/en-us/library/cc733145
- #>
- # leading blank line required by CBH
- #endregion
- #region Parameters
- [CmdletBinding ()]
- [OutputType ([string[]])]
- Param (
- # Directory to start scanning the folder tree.
- [Parameter (
- Mandatory,
- Position = 0,
- HelpMessage = "Please enter the directory to start scanning the folder tree."
- )]
- [ValidateScript (
- {
- if (Test-Path $_)
- {
- $True
- }
- else
- {
- throw "`n`nPath >> $_ << not found.`n"
- }
- })]
- [string]
- $Source,
- # Longest allowed Fully Qualified File Name length.
- [Parameter (
- Position = 1
- )]
- [ValidateRange (0, 250)]
- [int]
- $MaxFQFN_Length = 250
- )
- #endregion
- # Giving robocopy a REAL destination here makes it include it as a source.
- # Leaving it off makes it complain about not having a destination.
- # That disagrees with the docs. I have no idea why.
- # Using NULL [a string, not the variable $Null] works.
- # Thanks to /u/Martin9700 for the idea.
- $Destination = 'NULL'
- #region Robocopy parameter list
- <#
- source :: Source Directory (drive:\path or \\server\share\path).
- destination :: Destination Dir (drive:\path or \\server\share\path).
- /S :: copy Subdirectories, but not empty ones.
- /XJ :: eXclude Junction points. (normally included by default).
- /R:n :: number of Retries on failed copies: default 1 million.
- /W:n :: Wait time between retries: default is 30 seconds.
- /L :: List only - don't copy, timestamp or delete any files.
- /FP :: include Full Pathname of files in the output.
- /NS :: No Size - don't log file sizes.
- /NC :: No Class - don't log file classes.
- /NDL :: No Directory List - don't log directory names.
- /NP :: No Progress - don't display percentage copied.
- /NJH :: No Job Header.
- /NJS :: No Job Summary.
- #>
- #endregion
- $RC_Params = ($Source, $Destination) +
- "/S /XJ /R:0 /W:0 /L /FP /NS /NC /NDL /NP /NJH /NJS".Split(' ')
- $RC_FileList = robocopy $RC_Params |
- ForEach-Object {$_.Trim()} |
- Where-Object {$_.Length -gt $MaxFQFN_Length} |
- Sort-Object -Property Length
- return $RC_FileList
- } # end of function
- Get-TooLongFileName -Source $env:TEMP -MaxFQFN_Length 50
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement