Batch to detect unix and windows line endings

March 28, 2011 at 03:10:22
Specs: Windows 7
Hi,

I am stuck with my windows batch script :(

I want to check if a text file contains unix style line endings (LF)
and windows style line endings (CR LF).

I am able to detect windows style line endings with this command:

for /f "tokens=*" %%t in ( 'findstr /r "$" test.txt ) do set win_line_ending=1

But I don't know how to detect the unix style line ending.

Help is really appreciated!


See More: Batch to detect unix and windows line endings

Report •

#1
March 28, 2011 at 03:11:13
Forgot to say: I only want to use windows commands (no extra tools)

Report •

#2
March 28, 2011 at 05:54:17
If you're using Win7, and you plan on using Powershell, you can use this line:
$unixEOF = (Get-Content .\someFile.txt -Delimiter [String].Empty) -Match "[^`r]`n"

It's also possible with VBScript, but that'd take more than one line.

How To Ask Questions The Smart Way


Report •

#3
March 28, 2011 at 07:06:42
Thanks but unfortunately the script has to work in the default shell on any
system >= Win XP.

But more than one line would be ok!


Report •

Related Solutions

#4
March 28, 2011 at 07:44:54
Well then, I might as well use a version that works with batch scripts:
Set fso = CreateObject("Scripting.FileSystemObject")
Set Win = New RegExp : Win.Pattern = "\r\n"
Set Unix = New RegExp : Unix.Pattern = "[^\r]\n"

For Each arg In WScript.Arguments
  file = fso.OpenTextFile(arg).ReadAll
  If Win.Test(file) Then
    EOL = "=Win32"
  ElseIf Unix.Test(file) Then
    EOL = "=UNIX"
  Else
    EOL = "=Unknown"
  End If
  WScript.Echo fso.GetFileName(arg) & EOL
Next 'arg

Usage (assuming you called the script checkEOL.vbs):
cscript //nologo checkEOL.vbs <files to test>

How To Ask Questions The Smart Way


Report •

#5
March 28, 2011 at 08:29:05
Works like a charm. Thank you very much! :-)

I would like to integrate this into my *.bat file... is it possible?
I guess not because I need cscript to run the vbscript, right?


Report •

#6
March 28, 2011 at 08:41:47
Found this already to integrate into batch:

http://www.computerhope.com/forum/i...


Report •

Ask Question