Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #gemini 2.5 pro (preview)
- <#
- .SYNOPSIS
- Reads a file containing a list of filenames and removes leading spaces from each filename.
- .DESCRIPTION
- This script takes a text file as input, where each line in the file represents a filename.
- It reads each filename, removes any leading spaces, and then outputs the modified filename.
- This script does NOT rename the actual files on the file system; it only processes the strings
- read from the input file.
- .PARAMETER InputFile
- Specifies the path to the text file containing the list of filenames.
- .EXAMPLE
- .\Remove-LeadingSpacesFromFile.ps1 -InputFile "C:\Temp\filenames.txt"
- Reads the file "C:\Temp\filenames.txt" and displays the filenames with leading spaces removed.
- .NOTES
- - The input file should contain one filename per line.
- - This script only modifies the string representation of the filename read from the file.
- - It does not interact with the file system to rename any actual files.
- #>
- [CmdletBinding()]
- param(
- [Parameter(Mandatory=$true)]
- [string]$InputFile
- )
- # Check if the input file exists
- if (-not (Test-Path -Path $InputFile -PathType Leaf)) {
- Write-Error "Input file '$InputFile' not found."
- exit 1
- }
- # Read each line from the input file
- try {
- $Filenames = Get-Content -Path $InputFile
- }
- catch {
- Write-Error "Error reading file '$InputFile': $($_.Exception.Message)"
- exit 1
- }
- # Process each filename
- foreach ($Filename in $Filenames) {
- # Remove leading spaces using the TrimStart() method
- $TrimmedFilename = $Filename.TrimStart(" ")
- # Output the original and trimmed filename
- Write-Host "Original: '$Filename'"
- Write-Host "Trimmed: '$TrimmedFilename'"
- Write-Host "---"
- }
- Write-Host "Processing complete."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement