Are you doing anything with each line, or are you just copying the file?
Get-Content <file> -Raw | Out-File <newfile> -NoNewLine
Or just use the Copy-Item cmdlet.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am using powershell to read the contents of a file line by line and write to a new file (line by line), but the new file is always bigger than the old.
The script looks like below.
for example with an input file of size 1627KB the newfile is 1635KB - 8 KB bigger. The original file had 7983 lines.
Might I be adding an extra control character to the end of each line by writing to the new file line by line.
Thanks
Script:
$filename = "c:\dev\20221209.csv"
$newfile = "c:\dev\new.txt"
$halffile = "c:\dev\half.txt"
New-Item $newfile
New-Item $halffile
$file = New-Object System.IO.StreamReader -Arg $filename
$tot = (Get-Content $filename).Length
$half = $tot/2
$half = $half+1;
$lnum =0
while (($line = $file.ReadLine()) -ne $null) {
$lnum = $lnum + 1;
Add-Content -Path $newfile -Value $line
If ($lnum -lt $half)
{
Add-Content -Path $halffile -Value $line
}
}
echo "done"
Are you doing anything with each line, or are you just copying the file?
Get-Content <file> -Raw | Out-File <newfile> -NoNewLine
Or just use the Copy-Item cmdlet.
The issue appears to be that the file has come from linux that only has linefeed (LF) and not carriage return (CR)
When i use Add-Content it adds LF and CR to the end of each line - so an extra character on each line
To get round this i have used Add-Content with -NoNewLine (so no LF or CR on each line) and manually added a LF to each line.
So in the above script
$lf = "`n"; # Line feed
$linelf = $line + $lf; # Add line feed to the end of the line
Add-Content -NonewLine -Path $newfile -Value $linelf # adds the line with only the line feed to the file
This way the file sizes are the same.