Last Updated: February 25, 2016
·
2.278K
· purekrome

Update -all- .NET code files to have CRLF end of file markers

Before I customized my windows TortoiseGit settings to tell git to DO NOT MODIFY MY LINE ENDINGS for any of my .NET code files ... git changed the CRLF's to LF's.

Screw that. I want my CRLF's back, please - because these specific projects will only every be used on a Windows PC with Visual Studio 2012.

So.. without further a due, here's the steps to fix it up.

Create the script and put this into the root folder of your repository.

foreach ($ext in @("*.cs", "*.js", "*.html", "*.csproject", "*.sln", "*.css"))  {
    (dir -Recurse -Filter $ext) | foreach { 
        $file = gc $_.FullName
        $file | sc $_.FullName
        }

}

I saved that PowerShell script as normalize-crlf.ps1

Execute the PowerShell script.

NOTE: This will only change the powershell policy while the powershell window is open. Once it's closed, the policy will revert back to the (currently set) default setting.

1. Start PowerShell.
2. PS C:\> Set-ExecutionPolicy Unrestricted -Scope Process
3. Change directory to the root folder of your repository (eg. cd C:\Projects\XWing)
4. Execute the powershell script.

Eg.

Screenie

Special special thanks to @tdecreton from JabbR.net and RavenDb code - where this was copied from ...

3 Responses
Add your response

Wow, PowerShell is really crappier than I thought... you could do it with a singe line using UnxUtils...

find -regex '.*\.\(cs\|js\|c\|html\|csproject\|sln\)' -exec sed -i 's/[^\r]$/&\r/' +

over 1 year ago ·

Hi Ismaell :)

thanks for your comment - that single line might make it look a lot nicer. But, reducing a few lines to a single line doesn't always mean 'better' code. Secondly, to (poorly) quote Jamie Zawinski "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."

But yes, this can be seen as a trivial piece of code and Unix can shine when it comes to sed/awk/gawk etc.

I'm just not sure that I would say "PowerShell is really crappier" because I've decided to use a 2x for-eaches. I could also do something similar in Unix or on another CLI.

But you point is taken :) Ta!

over 1 year ago ·

@Ismaell, you could do it in one line of PS as well, I don't think that's the point?

(For what it's worth, something like):

ls *.cs, *.js, *.html, *.csproject, *.sln -r | % { sc $_ (gc $_) }

over 1 year ago ·