1

While trying to transfer file from Windows to Unix Azure environment, I am getting error dos2unix format error

dos2unix -o /xyz/home/ABC_efg.txt failed to execute dos2unix format change.

I tried to run a PS script to fix it but does seem to work .

Get-ChildItem -File -Recurse *.txt | % { $x = get-content -raw -path $_.fullname; $x -replace "`r`n","`n" | set-content -NoNewline -path $_.fullname }
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Anupam
  • 284
  • 5
  • 21

2 Answers2

0

Well, part of the issue is that you are piping a string to Set-Content and then trying to use that string to determine where to save the file. Try changing the last part from:

$x -replace "`r`n","`n" | set-content -NoNewline -path $_.fullname

to this:

set-content -NoNewline -path $_.fullname -value ($x -replace "`r`n","`n")

If that doesn't update the formatting like you expect it to you may need to use the -Encoding parameter for Set-Content. I'm not real familiar with encoding though, so I am not sure about that.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • I tried replacing the last part but the output behavior is not as per expectation . Will try to explore Encoding as i am also not familiar with it. Thanks for your suggestions @TheMadTechnician – Anupam Dec 13 '19 at 12:21
0

Instead of using -replace, I would prefer to read the content(s) as string array and join these strings with "`n".
Something like this:

$files = Get-ChildItem -File -Recurse -Filter '*.txt' | Select-Object -ExpandProperty FullName
$files | ForEach-Object { 
    (Get-Content -Path $_) -join "`n" | Set-Content -Path $_ -NoNewline -WhatIf
}

Remove the -WhatIf switch if you are satisfied with the outout shown in the console.

Theo
  • 57,719
  • 8
  • 24
  • 41