19

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?

charles ross
  • 531
  • 4
  • 15
13steinj
  • 367
  • 2
  • 7
  • 16
  • 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 Aug 27 '15 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. – Harry Johnston Aug 27 '15 at 21:13
  • 3
    Underrated question. – Marinos An Oct 03 '17 at 12:41
  • See also https://stackoverflow.com/q/20368781/1337544 – charles ross Jan 23 '19 at 21:10

4 Answers4

6

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

h3t1
  • 1,050
  • 2
  • 17
  • 28
Anil D
  • 61
  • 1
  • 2
5

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.

Marinos An
  • 7,669
  • 3
  • 45
  • 80
3

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

charles ross
  • 531
  • 4
  • 15
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
staafl
  • 3,021
  • 1
  • 25
  • 23