On windows, how would I detect the line ending of a file?
Asked Answered
S

5

27

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?

Seville answered 27/8, 2015 at 17:23 Comment(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.Exum
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.Trotter
Underrated question.Witherite
See also https://mcmap.net/q/47415/-anything-like-dos2unix-for-windows/1337544Lawana
S
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

Stocktaking answered 14/5, 2020 at 18:18 Comment(0)
W
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.

Witherite answered 3/10, 2017 at 15:32 Comment(1)
Update worked well, thanks!Suburban
L
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://mcmap.net/q/535094/-powershell-how-to-i-test-if-a-line-of-text-contains-line-feed-or-carriage-return

Lawana answered 23/1, 2019 at 21:20 Comment(0)
F
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
Forspent answered 7/1, 2021 at 16:22 Comment(0)
A
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.

Apology answered 20/9, 2022 at 22:45 Comment(2)
It will be better if you can also tell how to check the line ending characterKelleykelli
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.Harkins

© 2022 - 2024 — McMap. All rights reserved.