Advertisement
Lee_Dailey

Get-TooLongFileName

Feb 24th, 2017
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-TooLongFileName
  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 folder 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 folder 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 folder tree starting at C:\temp.
  24.         Returns any file names that are longer than 90 characters.
  25.     .NOTES
  26.         Version = 2017-02-24_-_21-40
  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 folder tree.
  41.         [Parameter (
  42.             Mandatory,
  43.             Position = 0,
  44.             HelpMessage = "Please enter the directory to start scanning the folder tree."
  45.             )]
  46.             [ValidateScript (
  47.                 {
  48.                 if (Test-Path $_)
  49.                     {
  50.                     $True
  51.                     }
  52.                     else
  53.                     {
  54.                     throw "`n`nPath >> $_ << not found.`n"
  55.                     }
  56.                 })]
  57.             [string]
  58.             $Source,
  59.  
  60.         # Longest allowed Fully Qualified File Name length.
  61.         [Parameter (
  62.             Position = 1
  63.             )]
  64.             [ValidateRange (0, 250)]
  65.             [int]
  66.             $MaxFQFN_Length = 250
  67.         )
  68.     #endregion
  69.  
  70.     # Giving robocopy a REAL destination here makes it include it as a source.
  71.     #    Leaving it off makes it complain about not having a destination.
  72.     #    That disagrees with the docs. I have no idea why.
  73.     #    Using NULL [a string, not the variable $Null] works.
  74.     #        Thanks to /u/Martin9700 for the idea.
  75.     $Destination = 'NULL'
  76.  
  77.     #region Robocopy parameter list
  78.     <#
  79.          source :: Source Directory (drive:\path or \\server\share\path).
  80.     destination :: Destination Dir  (drive:\path or \\server\share\path).
  81.              /S :: copy Subdirectories, but not empty ones.
  82.             /XJ :: eXclude Junction points. (normally included by default).
  83.            /R:n :: number of Retries on failed copies: default 1 million.
  84.            /W:n :: Wait time between retries: default is 30 seconds.
  85.              /L :: List only - don't copy, timestamp or delete any files.
  86.             /FP :: include Full Pathname of files in the output.
  87.             /NS :: No Size - don't log file sizes.
  88.             /NC :: No Class - don't log file classes.
  89.            /NDL :: No Directory List - don't log directory names.
  90.             /NP :: No Progress - don't display percentage copied.
  91.            /NJH :: No Job Header.
  92.            /NJS :: No Job Summary.
  93.     #>
  94.     #endregion
  95.  
  96.     $RC_Params = ($Source, $Destination) +
  97.         "/S /XJ /R:0 /W:0 /L /FP /NS /NC /NDL /NP /NJH /NJS".Split(' ')
  98.    
  99.     $RC_FileList = robocopy $RC_Params |
  100.         ForEach-Object {$_.Trim()} |
  101.         Where-Object {$_.Length -gt $MaxFQFN_Length} |
  102.         Sort-Object -Property Length
  103.  
  104.     return $RC_FileList
  105.    
  106.     } # end of function
  107.  
  108.  
  109.  
  110. Get-TooLongFileName -Source $env:TEMP -MaxFQFN_Length 50
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement