remove blank spaces

Welcome Forums General PowerShell Q&A remove blank spaces

This topic contains 3 replies, has 2 voices, and was last updated by

3 years, 10 months ago.

  • Author
  • #21214

    Points: 0
    Rank: Member

    How could you remove blank spaces from a directory of txt files but have each file save speratley. the code snip it I have combines all the txt files into 1

    heres what I have so far

    @(gc (dir) -match '\S'  | out-file c:\FileWithNoEmptyLines.txt
  • #21216

    Points: 0
    Rank: Member

    Hey there H Man, does this help? It'll copy the text files, remove the spaces from the copies, and copy them back to the original dir. Might be a little dirty, but it works.

    $Files = Get-ChildItem -Path C:\scripts\Spaces -File *.txt
    ForEach[$File in $Files]{Copy-Item $File.FullName -Destination C:\scripts\Spaces\Changed 
                                Rename-Item $File.FullName [$File.Name -replace " ",""]
                                Copy-Item -Path C:\scripts\Spaces\Changed\*.txt -Destination C:\scripts\Spaces
  • #21218

    Points: 0
    Rank: Member

    Hey WIll thanks for the response

    I was looking to remove the spaces with in each message itself not the name

    but thanks that's still pretty cool how u did that

    heres the code to remove the blanks internally in the file

    @(gc c:\FileWithEmptyLines.txt) -match '\S'  | out-file c:\FileWithNoEmptyLines

    i need to save each indiviual file after the spaces are removed not as 1 whole file

  • #21219

    Points: 0
    Rank: Member

    i solved it

    ForEach($File in $Files){
    $Directory = 'C:\Testing\E\'
    $path = $File.FullName 
    $name = $
    $extension = $file.extension
    @(gc  $path) -match '\S'  | out-file "$($Directory)$($name)$($Extension)"

The topic ‘remove blank spaces’ is closed to new replies.