Advertisement
joedigital

ps-read list and remove leading space from filenames

May 19th, 2025
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #gemini 2.5 pro (preview)
  2. <#
  3. .SYNOPSIS
  4.     Reads a file containing a list of filenames and removes leading spaces from each filename.
  5.  
  6. .DESCRIPTION
  7.     This script takes a text file as input, where each line in the file represents a filename.
  8.     It reads each filename, removes any leading spaces, and then outputs the modified filename.
  9.     This script does NOT rename the actual files on the file system; it only processes the strings
  10.     read from the input file.
  11.  
  12. .PARAMETER InputFile
  13.     Specifies the path to the text file containing the list of filenames.
  14.  
  15. .EXAMPLE
  16.     .\Remove-LeadingSpacesFromFile.ps1 -InputFile "C:\Temp\filenames.txt"
  17.     Reads the file "C:\Temp\filenames.txt" and displays the filenames with leading spaces removed.
  18.  
  19. .NOTES
  20.     - The input file should contain one filename per line.
  21.     - This script only modifies the string representation of the filename read from the file.
  22.     - It does not interact with the file system to rename any actual files.
  23. #>
  24. [CmdletBinding()]
  25. param(
  26.     [Parameter(Mandatory=$true)]
  27.     [string]$InputFile
  28. )
  29.  
  30. # Check if the input file exists
  31. if (-not (Test-Path -Path $InputFile -PathType Leaf)) {
  32.     Write-Error "Input file '$InputFile' not found."
  33.     exit 1
  34. }
  35.  
  36. # Read each line from the input file
  37. try {
  38.     $Filenames = Get-Content -Path $InputFile
  39. }
  40. catch {
  41.     Write-Error "Error reading file '$InputFile': $($_.Exception.Message)"
  42.     exit 1
  43. }
  44.  
  45. # Process each filename
  46. foreach ($Filename in $Filenames) {
  47.     # Remove leading spaces using the TrimStart() method
  48.     $TrimmedFilename = $Filename.TrimStart(" ")
  49.  
  50.     # Output the original and trimmed filename
  51.     Write-Host "Original: '$Filename'"
  52.     Write-Host "Trimmed:  '$TrimmedFilename'"
  53.     Write-Host "---"
  54. }
  55.  
  56. Write-Host "Processing complete."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement