Home > Software design >  File size nearly doubled after Powershell regex replace?
File size nearly doubled after Powershell regex replace?

Time:12-01

I am reading json files from a directory and replacing a number of strings and have noticed that the file size after running the script is almost twice the size. The file has the same number of lines after running, (sometimes one extra line). The replacements are working fine but the folder has thousands of files and the difference after running will be over 100gb!

Just wondering if there is something I've screwed up in the script?

Get-ChildItem G:\VaM\Saves\scene\* -recurse -include *.json,*.vap,*.vaj |
    Foreach-Object { Write-Host $_.FullName -NoNewLine
    (Get-Content -raw -LiteralPath $_.FullName) |
        Foreach-Object {$_ -Replace '" *:.*/Custom/', '" : "Custom/' -Replace 'male/RG', 'male/kill-RG' -Replace '_MAKEUP', 'Makeup' -Replace 'Make up', 'Makeup' -Replace 'Make-up', 'Makeup' -Replace '"SELF:/', '"'} |
        Out-File $_.FullName}

Before After

I ran the script a number of times on a small sample and the result is the same.

I'm running windows 11

CodePudding user response:

Use Set-Content rather than Out-File which may be the culprit here:

Get-ChildItem -Path 'G:\VaM\Saves\scene\*' -Recurse -Include '*.json','*.vap','*.vaj' | & {
    Param(
        [Parameter(ValueFromPipeline)]
        $InputObject
    )
    PROCESS
    {
        (Get-Content -Path $InputObject.FullName -Raw) `
            -Replace '" *:.*/Custom/', '" : "Custom/' -Replace 'male/RG', 'male/kill-RG' -Replace '_MAKEUP', 'Makeup' -Replace 'Make up', 'Makeup' -Replace 'Make-up', 'Makeup' -Replace '"SELF:/', '"' |
            Set-Content -Path $InputObject.FullName
    }
}

The use of the Foreach-Object after your Get-Content is unnecessary since -Raw is being applied which will read the entire contents as a single multi-line string.

  • Related