26

I've seen answers to the questions, but those answers are not from a windows perspective from what I can tell.

Windows uses CR LF, Unix uses LF, Mac uses LF and classic mac uses something else. I don't have the brainpower to tell that somehow, if a file is using a different line ending than what I am typing, I get errors when trying to run the script/program which frankly, don't make much sense. After conversion, the script works just fine.

Is there anyway to preemptively check what line endings a file uses, on Windows?

4
  • With a hex editor, looking for the characters 0x0D and 0x0A respectively. The pair of them (in that order) make up a Windows line end.
    – Ken White
    Commented Aug 27, 2015 at 17:28
  • 6
    If you open the file with notepad, it is obvious if it has non-Windows EOL characters (because notepad doesn't treat them as EOL). That assumes the file is consistent, though - if there are just a few mismatched EOL sequences it may not be obvious. Commented Aug 27, 2015 at 21:13
  • 3
    Underrated question.
    – Marinos An
    Commented Oct 3, 2017 at 12:41
  • See also stackoverflow.com/q/20368781/1337544 Commented Jan 23, 2019 at 21:10

5 Answers 5

9

use a text editor like notepad++ that can help you with understanding the line ends.

It will show you the line end formats used as either Unix(LF) or Macintosh(CR) or Windows(CR LF) on the task bar of the tool.

enter image description here

you can also go to View->Show Symbol->Show End Of Line to display the line ends as LF/ CR LF/CR.

enter image description here

8

Steps:

Then you can execute:

c:\gnuwin32\bin\file.exe my-lf-file.txt

my-lf-file.txt; ASCII text

c:\gnuwin32\bin\file.exe my-crlf-file.txt

my-crlf-file.txt; ASCII text, with CRLF line terminators

Of course you can add c:\gnuwin32\bin to your %PATH% variable, to be able to access it without providing the full path.


UPDATE:

  • If you have git installed you can launch git-bash and run file command from there.

  • Or you can install this subsystem, as described in the official Microsoft documentation, and get access to the file command.

1
  • Update worked well, thanks!
    – Natetronn
    Commented Sep 29, 2021 at 23:39
4

I too am looking for a "native" windows scripting solution. So far, just have to read a line or 2 in VB in binary fashion and inspect the characters.

One tool to check "manually" is Notepad++. The status bar has a newline style indicator on the right end next to the file encoding indicator.

It looks like this in version 7.5.6 enter image description here

Other editors with Hex mode can show you also.

In Powershell, this command returns "True" for a Windows style file and "False" for a *nix style file.

(Get-Content '\\FILESERVER0001\Fshares\NETwork Shares\20181206179900.TXT' -Raw) -match "\r\n$" 

This came from Matt over here: https://stackoverflow.com/a/35354009/1337544

0

In a batch file, you can try converting the file to CRLF and checking if its size increases:

rem check-crlf.bat

@echo off
setlocal

call type "%~1" | c:\Windows\System32\find.exe "" /v > "%~1.temp"
set size1=%~z1
rem add 2 in case the file doesn't have a trailing newline, since find will add it
set /a size1plus2=%size1%+2
call :setsize2 "%~1.temp%"

for /f %%a in ('c:\Windows\System32\findstr /R /N "^" "%~1" ^| c:\Windows\System32\find /C ":"') do set lines=%%a

if %size1plus2% equ %size2% (
    if %lines% equ 2 (
        echo File uses LF line endings!
    ) else (
        echo File uses CRLF or has no line endings!
    )
) else (
    if %size1% lss %size2% (
        echo File uses LF line endings!
    ) else (
        echo File uses CR+LF line endings!
    )
)
del "%~1.temp"
exit /b

:setsize2
set size2=%~z1
exit /b

We're handling the special case of a file without a trailing newline, as well as a file with two LF-terminated newlines, which both lead to an increase of 2 bytes.

Usage:

check-crlf.bat file-i-care-about.txt
0

So the main thing to remember, at least for a computer programmer working on modern software is that any combination of CR and LF, in sequence needs to be treated as a newline. You will almost never see the 'old' mac, which is CR with no LF - I prefer to ignore its relatively minuscule existence.. I tend to use 1-byte file processing, but that is a personal preference (a preference that pays a dividend in this scenario) Show proficiency as a programmer by making your code resilient to line ending format of text files.

2
  • It will be better if you can also tell how to check the line ending character
    – charmian
    Commented Sep 29, 2022 at 2:39
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 29, 2022 at 2:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.