Advertisement
Lee_Dailey

Get-LongFileName

Feb 23rd, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-LongFileName
  2.     {
  3.     #region Comment Based Help [CBH]
  4.     #    trailing blank line required by CBH
  5.  
  6.     <#
  7.     .SYNOPSIS
  8.         Gets the names of files that are longer than specified.
  9.     .DESCRIPTION
  10.         Uses Robocopy [with /L to avoid any changes] to get file names that are longer than specified.
  11.         Scans the entire tree starting at the $Source location.
  12.         Returns [a] $Null [no files]
  13.                 [b] a string [one file]
  14.                 [c] an array of strings [two or more files]
  15.     .EXAMPLE
  16.         Get-LongFileName -Source C:\temp
  17.        
  18.         Scans the directory tree starting at C:\temp.
  19.         Returns any file names that are longer than the default of 250 characters.
  20.     .EXAMPLE
  21.         Get-LongFileName -Source C:\temp -MaxFQFN_Length 90
  22.        
  23.         Scans the directory tree starting at C:\temp.
  24.         Returns any file names that are longer than 90 characters.
  25.     .NOTES
  26.         Version = 2017-02-23_-_22-27
  27.  
  28.         Robocopy Documentation
  29.         - https://technet.microsoft.com/en-us/library/cc733145
  30.     #>
  31.  
  32.     #    leading blank line required by CBH
  33.     #endregion
  34.  
  35.     #region Parameters
  36.     [CmdletBinding ()]
  37.     [OutputType ([string[]])]
  38.  
  39.     Param (
  40.         # Directory to start scanning the tree.
  41.         [Parameter (
  42.             Mandatory,
  43.             Position = 0
  44.             )]
  45.             [ValidateScript ({Test-Path $_})]
  46.             [string]
  47.             $Source,
  48.  
  49.         # Longest allowed Fully Qualified File Name length.
  50.         [Parameter (
  51.             Position = 1
  52.             )]
  53.             [ValidateRange (0, 250)]
  54.             [int]
  55.             $MaxFQFN_Length = 200
  56.         )
  57.     #endregion
  58.  
  59.     # giving robocopy a REAL destination here makes it include it as a source.
  60.     #    leaving it off makes it complain about not having a destination.
  61.     #    that disagrees with the docs. i have no idea why.
  62.     $Destination = "$env:Temp\FolderThatBetterNotBeThere"
  63.  
  64.     #region Robocopy parameter list
  65.     <#
  66.          source :: Source Directory (drive:\path or \\server\share\path).
  67.     destination :: Destination Dir  (drive:\path or \\server\share\path).
  68.              /S :: copy Subdirectories, but not empty ones.
  69.             /XJ :: eXclude Junction points. (normally included by default).
  70.            /R:n :: number of Retries on failed copies: default 1 million.
  71.            /W:n :: Wait time between retries: default is 30 seconds.
  72.              /L :: List only - don't copy, timestamp or delete any files.
  73.             /FP :: include Full Pathname of files in the output.
  74.             /NS :: No Size - don't log file sizes.
  75.             /NC :: No Class - don't log file classes.
  76.            /NDL :: No Directory List - don't log directory names.
  77.             /NP :: No Progress - don't display percentage copied.
  78.            /NJH :: No Job Header.
  79.            /NJS :: No Job Summary.
  80.     #>
  81.     #endregion
  82.  
  83.     $RC_Params = ($Source, $Destination) + "/S /XJ /R:0 /W:0 /L /FP /NS /NC /NDL /NP /NJH /NJS".Split(' ')
  84.    
  85.     $RC_FileList = robocopy $RC_Params |
  86.         ForEach-Object {$_.Trim()} |
  87.         Where-Object {$_.Length -gt $MaxFQFN_Length} |
  88.         Sort-Object -Property Length
  89.  
  90.     return $RC_FileList
  91.    
  92.     } # end of function
  93.  
  94.  
  95.  
  96. Get-LongFileName -Source $env:TEMP -MaxFQFN_Length 50
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement