Home > OS >  PowerShell: (Save files with the original name) Out-File -FilePath (keep the same name, without rena
PowerShell: (Save files with the original name) Out-File -FilePath (keep the same name, without rena

Time:11-04

just to understand the saving option on Powershell. The code below mix words from a particular file, into one folder, it save that file with a different name.

((Get-Content -Path C:\Folder1\file.txt -Raw ) -split "\s " | 
    Sort-Object {Get-Random} ) -join ' ' |
        Out-File -FilePath C:\Folder1\NewFile.txt

Now, I modify a little bit the code. Because I want to select all text files, from the same folder, but save them with the same name. So I want to modify each files, but keeping the original name on save.

I believe I made something wrong. Can you help me, please?

((Get-Content -Path C:\Folder1\*.txt -Raw ) -split "\s " | 
    Sort-Object {Get-Random} ) -join ' ' |
        Out-File -FilePath C:\Folder1\-W

CodePudding user response:

Try this

$Fllist=get-childitem "c:\folder" | ?{$_.name -ilike "*.txt"}

foreach($File in $Fllist) {
    ((Get-Content -Path "C:\Folder1\$($File.name)" -Raw -Encoding UTF8) -split "\s " |
      Sort-Object {Get-Random} ) -join ' ' |
      Out-File -FilePath "c:\Folder1\$($File.name)"
}
  • Related