remove blank spaces

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of H Man H Man 1 year, 11 months ago.

  • Author
    Posts
  • #21214
    Profile photo of H Man
    H Man
    Participant

    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
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    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
    Profile photo of H Man
    H Man
    Participant

    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
    Profile photo of H Man
    H Man
    Participant

    i solved it

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

You must be logged in to reply to this topic.