Solved Read the first line of the file that ends carriage return

May 21, 2012 at 06:40:59
Specs: Windows 7
Hi,

Could some one help in reading a comma delimited file that ends with carriage return <CR>. I have tried with below command
for /F "tokens=* delims=," %%j in (abc.txt) do (
)
but it always looking for the <LF> to read the line. Thanks for the help.


See More: Read the first line of the file that ends carriage return

Report •


✔ Best Answer
June 6, 2012 at 13:54:51
Does VBScript can run from command prompt?
It can. WScript runs scripts in a windowed environment. CScript runs scripts in a command line environment. Neither will return control until they complete if you call them from a batch file. CScript is preferred because any error messages will be written to StdErr, and not a dialog box that requires user interaction.

It is surprise to me that we don't have an option in batch script to read file content line by line that ends with carriage return.
It's not really that surprising if you know the history behind it. It simply wasn't designed (and I use that word loosely) for text parsing. Mostly, it's useful to run a series of predefined commands, or some light file system manipulation. The FOR /F command was added by the WinNT line, I think in the Win2K release.

As a scripting language, it's been succeeded by VBScript, which itself has been succeeded by PowerShell. VBScript doesn't support \r as a line ending, but that's easy enough to work around. PowerShell does allow \r as a line ending, but it does not run scripts by default.

How To Ask Questions The Smart Way



#1
May 21, 2012 at 06:58:28
Are you trying to read a specific column or an entire line?

Tony


Report •

#2
May 21, 2012 at 07:07:21
Thanks Tony for quick reply,

I am trying to read entire line (i.e upto <CR>) after that I will have to get the comma separated words from that line.
If you could provide both in simple script that would be great.


Report •

#3
May 21, 2012 at 07:44:09
A few months ago, I posted a utility script that would take a text file with Unix / Mac OS line endings and convert it to Windows line endings. It's one of those scripts I wrote for myself, which is why it converts Mac OS' EoL, but doesn't have a prompt after its job is done.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
May 21, 2012 at 08:31:41
Thanks Razon, I am looking for the batch script solution to do this. I am looking for a way to read only first line with the search of <CR> or any other way, not interested in converting entire file and read first line from the new file.

Anyway thanks for your reply on this.


Report •

#5
May 21, 2012 at 09:03:05
You can use this to find lines ending with a CRLF (0D0A).

@echo off & setlocal enabledelayedexpansion
set LF=^


for /f %%i in ('copy /z "%~dpf0" nul') do set "CR=%%i"
findstr "!CR!!LF!" file.txt

Do not remove the blank lines after the set command.

Tony


Report •

#6
May 21, 2012 at 09:39:55
Tony,
I am looking for the line ending with CR (0D), I am interested only first line of the file.
Some houw your solution not getting the first line (Win 7).
I am not sure why thats happening.

Please see sample file below, please make sure end of the line would be <CR> on all lines

----------------------------------------------------------------------------------------------------------------
Ac,Dt,Fm,To,Msg,S ID
1,42,13,14,15,16
8,32,23,24,25,26
9,22,33,34,35,36
10,12,43,44,45,46
----------------------------------------------------------------------------------------------------------------
I am interested only the first header line and all lines ending with <CR> 0D. Thanks for your help.


Report •

#7
May 21, 2012 at 10:24:35
I'm not seeing the point in looking for a carriage return. What are you actually trying to find in this csv file?

Tony


Report •

#8
May 21, 2012 at 11:43:34
I am processing a file that received from external services. That file has <CR> (0D) at the end of each line. It is more than 1GB in size, so don't want to generate new file by replacing the <CR> to some other characters (or <CR><LF>) then read from the generated file.

I just want to read the first line that ends with <CR> from the file using batch script.

depending on the first line I determine what subroutine to be called next.

Not sure how external party generating the file (May be MAC or other O/S). Hope this information is suffice.


Report •

#9
May 28, 2012 at 11:06:27
Hello, No solution for this? Can some one answer this please? I am first time working on batch script. If there is no solution for this question in batch script then I will stop this thread.

Report •

#10
May 28, 2012 at 19:33:13
First thought is maybe some combination of FOR and TYPE will do what you want.

Second thought is to suggest there is a reason why that script exists, and that the choice of language wasn't done haphazardly.

Third thought is "haphazardly" is hard to spell.

How To Ask Questions The Smart Way


Report •

#11
May 29, 2012 at 09:32:03
Yeah, I'm out of ideas. Some tools are willing to take a \n in lieu of \r\n, but it looks like none of them are willing to take just a \r. I suggest you use a different language. If you're on Win7, your native options are VBScript and PowerShell.

How To Ask Questions The Smart Way


Report •

#12
June 5, 2012 at 13:11:09
Thanks Razor,. from your answers I came to know that we cannot handle carriage return in a file from batch script. As per the requirement I must use only batch script to resolve this issue.

Report •

#13
June 5, 2012 at 14:05:46
Is there a reason? VBScript is the more native scripting language in the Windows family than CMD.

How To Ask Questions The Smart Way


Report •

#14
June 6, 2012 at 12:40:24
Lot of legacy work has been done using batch script. Does VBScript can run from command prompt?

It is surprise to me that we don't have an option in batch script to read file content line by line that ends with carriage return.


Report •

#15
June 6, 2012 at 13:54:51
✔ Best Answer
Does VBScript can run from command prompt?
It can. WScript runs scripts in a windowed environment. CScript runs scripts in a command line environment. Neither will return control until they complete if you call them from a batch file. CScript is preferred because any error messages will be written to StdErr, and not a dialog box that requires user interaction.

It is surprise to me that we don't have an option in batch script to read file content line by line that ends with carriage return.
It's not really that surprising if you know the history behind it. It simply wasn't designed (and I use that word loosely) for text parsing. Mostly, it's useful to run a series of predefined commands, or some light file system manipulation. The FOR /F command was added by the WinNT line, I think in the Win2K release.

As a scripting language, it's been succeeded by VBScript, which itself has been succeeded by PowerShell. VBScript doesn't support \r as a line ending, but that's easy enough to work around. PowerShell does allow \r as a line ending, but it does not run scripts by default.

How To Ask Questions The Smart Way


Report •

#16
June 6, 2012 at 14:31:47
Thanks Razor, very nicely explained. I am not familiar with VBscript. I will try to work on vbscript to get this done.

Is there default support in VBscript to read comma separated file?


Report •

#17
June 6, 2012 at 14:43:31
VBScript, though the TextStream object, gives basic text control. Calling ReadAll on your file will copy the entire file into memory, which you have to manipulate yourself.

To split the file into lines, I would call:

lines = Split(txtFile, vbCr)
To split those lines into fields, I would use Split again:
currentLine = Split(lines(lineNo), ",")

From there, I have all of the fields in an easy to use array. From there, I can make any changes I need, then add it back to the line-split array:
lines(lineNo) = Join(currentLine, ",")

How To Ask Questions The Smart Way


Report •

#18
June 7, 2012 at 12:26:32
Thank you, I can try this option. Thanks for your help.

Report •

Ask Question