Computing.Net > Forums > Programming > remove carriage return at file end

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

remove carriage return at file end

Reply to Message Icon

Name: ksuchetan
Date: October 9, 2008 at 16:01:11 Pacific
OS: Windows XP
CPU/Ram: 2 gb
Comment:

I want to write a batch script to remove the carriage return if it exists in the file.

Please help

Thanks In Advance

Thanks In Advance



Sponsored Link
Ads by Google

Response Number 1
Name: tonysathre
Date: October 9, 2008 at 17:34:41 Pacific
Reply:

for /f "tokens=* delims=" %%i in (test.txt) do (
echo %%i >> output.txt
)


0

Response Number 2
Name: Judago
Date: October 10, 2008 at 01:32:02 Pacific
Reply:

If you use redirection (> or >>) into a file with echo, with few exceptions, you will always end with a cr/lf at the very end of the file.

I am unaware of any commands to remove this final cr/lf. Your best bet is a third party utility.


0

Response Number 3
Name: klint
Date: October 10, 2008 at 03:39:22 Pacific
Reply:

Can you tell us exactly what you mean?

Do you really mean you want to remove JUST the carriage return (CR character, ASCII 13) but NOT the line feed (LF character, ASCII 10) and ONLY on the last line of the file? Why?

I am puzzled by your request, as it's unusual. Much more typical is wanting to convert DOS files to Unix files, which means removing the Carriage Return from the end of EVERY line.

Or is it simply that you have a blank line at the end of the file that you want to remove (ie both the CR and LF characters)?


0

Response Number 4
Name: IVO
Date: October 10, 2008 at 06:31:33 Pacific
Reply:

Take a look at

http://www.computing.net/answers/pr...

FishMonger posted the right questions, but no response at all... just another thread about the same issue!


0

Response Number 5
Name: klint
Date: October 10, 2008 at 06:40:09 Pacific
Reply:

Thanks for pointing that out Ivo. We can only help if we know what the problem is!

Until I have more information, here's another speculation: the "carriage return" at the end of the file is not a carriage return at all, but a CTRL-Z character (ASCII 26) which is put there by the COPY command (which it does when you don't add the /B switch.)


0

Related Posts

See More



Response Number 6
Name: ksuchetan
Date: October 10, 2008 at 09:00:36 Pacific
Reply:

Hi,

Well I have a blank line at the end of the file and I want to remove both the the CR and LF characters from the file as my program identifies it as a data and process aborts saying required field missing.

Please help!!!
Thanks In Advance

Thanks In Advance


0

Response Number 7
Name: ksuchetan
Date: October 10, 2008 at 09:47:24 Pacific
Reply:

Hi,

Let me quote my entire requirement :

Hi,

I want to combine two or more files into one file,(note the number of files in the folder may vary)

The first row is the header I want to strip the header in each file and then create a combine file.


Now suppose

I have file A,B,C

Content of the file A is

ID NAME CITY
01 RRR CA
02 EEE AZ

Content of file B
ID NAME CITY


Content of file C
ID NAME CITY
08 WWW NJ


the o\p file shuld be

01 RRR CA
02 EEE AZ
08 WWW NJ

I am not sure of the no.of incoming file.
I have to combine files which have a specific pattern in the file name.

like 092008AA
102008AA

that is I want to combine content of these files with
*AA

I have wriiten a the script is as follows:


@echo off
setLocal EnableDelayedExpansion


CD%1
if exist "$2_COMBINED.TXT" del "$2_COMBINED.TXT"

for /f "tokens=* delims= " %%t in ('dir/b/a-d *%2') do (
for /f "tokens=* skip=1 delims= " %%a in (%%t) do (
if not "%%a"=="" echo %%a >> $2_COMBINED.TXT
)
)

But this script has two major problems,

1) If the incoming files is empty this script doesnot create a $2_combined file so the dependant process doesnot run as there is no file

This issue is resolved as few helpful poster requested me to create a combine file with null value that is
type :

TYPE NUL>"%2_COMBINED.TXT"

Instead of: if exist "$2_COMBINED.TXT" del "$2_COMBINED.TXT".

Please help me with the second issue.


2) At the end of the file there is some value added I am not sure it is ctrl+Z or carriage return,
I mean if my incoming file has a blank row some special character is added to the file the dependant process doesnot identify it


Please help this is very urgent for me.

Thanks In Advance

Thanks In Advance


0

Response Number 8
Name: klint
Date: October 10, 2008 at 10:22:32 Pacific
Reply:

Your script (with problem 1 fixed) looks fine to me. I don't see what special character could be there. The only thing that writes to it is

echo %%a >> $2_COMBINED.TXT

and %%a is just a single line.

One possible problem could be the way your application parses this combined file. In a typical text file, every line ends with an appropriate line terminator (i.e. CR LF on Windows, or just LF on Unix.) Even the last line is supposed to end like that. However, it could be that your application reads the last line (terminated by the last CR LF), and processes it, and then tries to read the next line, but since it is now at the end of the file it reads 0 characters, and can't cope with it.


0

Response Number 9
Name: ksuchetan
Date: October 10, 2008 at 10:32:56 Pacific
Reply:

well frankly telling this script works fine if the incoming file doesnot have an empty row in the end of the file.

if the file has then while combining it adds the CR\LF character which breaks the file.

Can you please help me to cleanse the file before running this script that means if the endof file is empty row remove it and then let it pass through thie piece of code.

Your help will be really appreciated.

Thanks In Advance

Thanks In Advance


0

Response Number 10
Name: IVO
Date: October 10, 2008 at 13:04:39 Pacific
Reply:

To clean empty lines out of a file code

for /F "tokens=* delims=" %%a in (MyFile.in) do echo.%%a>> MyFile.out


0

Response Number 11
Name: klint
Date: October 10, 2008 at 14:32:23 Pacific
Reply:

Ivo, I think ksuchetan is already doing that - in fact he's even checking if %%a is empty before appending to the file. So at the moment I'm not sure what the answer is, I'm stuck.

Perhaps if the original input files can be uploaded to a server so we can try the script ourselves with the actual files?


0

Response Number 12
Name: ksuchetan
Date: October 10, 2008 at 14:35:34 Pacific
Reply:

sure,I can send the file across to you

can you please guide me how to upload it to the server?

Thanks In Advance


0

Response Number 13
Name: ksuchetan
Date: October 10, 2008 at 14:42:47 Pacific
Reply:

actually if the file has the last line
with space

i mean

aa ggg 123
gg bbb 2345
tt yyy 2789
<space>


this is the incoming file

I want to go to the end of file and remove this space
else this script attaches ^z or the CR\LF after this space which causes the problem

If i just rename the file as %2_combined the process runs fine

Please help

I am stuck because of this script

Thanks In Advance


0

Response Number 14
Name: Judago
Date: October 10, 2008 at 15:53:49 Pacific
Reply:

Here's a link to a utiltiy program I have already posted in one of your last threads.

It can remove empty lines, lines that only contain spaces and convert DOS text files to unix format(Also seems it can do a whole lot more). If you decide to download it the following command will remove all empty and blank lines except the final cr/lf(unfortunately).


sfk filter yourtextfile.txt -no-empty-lines -no-blank-lines -write -yes

You will need to either rename the file you download to sfk.exe or change sfk in the line above to whatever the download in called, as they include their version number.

Be warned that this will process the file without asking, overwriting whatever empty or blank lines it may contain. Be sure to test in on an unimportant copy.

You could then remove all the cr's in the file, but it will still have a single line feed at the very end of the file.


sfk remcr yourtextfile.txt


0

Response Number 15
Name: Judago
Date: October 10, 2008 at 17:02:45 Pacific
Reply:

I've figured out how to remove the last crlf using sfk.

After your for loops are finished and you have all of your info in the text file


echo ----####@@crlfkiller@@####---->>yourtextfile.txt
sfk filter yourtextfile.txt -no-empty-lines -no-blank-lines -write -yes
sfk replace yourtextfile.txt -bin /0D0A2D2D2D2D23232323404063726C666B696C6C65724040232323232D2D2D2D0D0A// -yes


If you also want the file converted to unix format/lf only then add this line at the end:


sfk remcr yourtextfile.txt

Just be warned that it will change yourtextfile.txt so try it on an unimportant copy first.

A couple of notes, sfk doesn't like it's commands in capital letters and don't add a space between "----####@@crlfkiller@@####----" and ">>" it will stop things from working.


0

Response Number 16
Name: Mechanix2Go
Date: October 10, 2008 at 17:50:59 Pacific
Reply:

OOPS

not quite
Try this:

====================================
@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (thefile) do (
set str=%%a
if not "!str!"==" " echo.!str!>> newfile
)

@echo off > outfile

for /f "tokens=* delims= " %%a in (newfile) do (
echo %%a>> outfile
)


0

Response Number 17
Name: ksuchetan
Date: October 13, 2008 at 12:25:58 Pacific
Reply:

Hi,

I have two issues with this script now,

I have modified the script as follows now.

@echo off
setLocal EnableDelayedExpansion


CD%1
TYPE NUL>"%2_COMBINED.TXT"
TYPE NUL>"COMBINED.TXT"

for /f "tokens=* delims= " %%t in ('dir/b/a-d *%2*') do (
for /f "tokens=* delims= " %%a in (%%t) do (
set str=%%a
if not "!str!"==" " echo.!str!>> COMBINED.TXT
)
)

for /f "tokens=* delims= " %%a in (COMBINED.TXT) do (
echo %%a>> %2_COMBINED.TXT
)


the issue is

One of the file has record

AAA|I|130001164821|130000143139|Deborah!|RN|||2008-10-06 03:08:39

the combine file has this record as
AAA|I|130001164821|130000143139|Deborah08:39

Please guide.

More over when i try

copy /b *%2* %2_combined.txt

The records of one file are copied in one line
and the next file copy starts from the same line


like

record1
record2
record3 record4
record5

whereas it should be

record1
record2
record3
record4
record5

Please guide it is very urgent

Thanks In Advance


0

Response Number 18
Name: Judago
Date: October 13, 2008 at 14:25:01 Pacific
Reply:

This should get around the pipe symbols.


set str=%%a
if not "!str!"==" " set wrk=!str:^|=--sepippipes--!&&echo !str:--sepippipes--=^|!>>combined.txt

You problem of data being on the same line is caused by removing the last cr/lf, without it data will end up on the same line like that.


Did anything finally fix your cr/lf problem? It's generally a courtesy to post back with the outcome to close the thread.


0

Response Number 19
Name: ksuchetan
Date: October 13, 2008 at 14:44:16 Pacific
Reply:

Thanks!!

For the help in time will try and let you know can

some please guide me how to solve this issue.

copy /b *%2* %2_combined.txt

The records of one file are copied in one line
and the next file copy starts from the same line


like

record1
record2
record3 record4
record5

whereas it should be

record1
record2
record3
record4
record5

Please guide it is very urgent

Thanks In Advance


0

Response Number 20
Name: ksuchetan
Date: October 13, 2008 at 17:18:15 Pacific
Reply:

Hi,

The Above solution didnot work for me..:(

set str=%%a
if not "!str!"==" " set wrk=!str:^|=--sepippipes--!&&echo !str:--sepippipes--=^|!>>combined.txt

I still get the same o\p

I understand the copy command does work fine but I am facing the above issue.

Can some please help me with this


Thanks In advance

Thanks In Advance


0

Response Number 21
Name: Judago
Date: October 14, 2008 at 00:57:57 Pacific
Reply:

I got my variables mixed up when I changed them to the variables you were using(wrk should have been str). Anyway here's a more flexible way of doing the same thing:


set str="%%a"
if not !str!==" " set str=!str:|=^|!&&echo !str:~1,-1!>>combined.txt

As for your other problem, you need to add a linebreak(cr/lf) to the end of the input files if you are using copy file+file+file. If it doesn't have the extra linebreak it will simply copy to the same line.

If you are using for loops and appending the info the you could add an extra line break into the output file using echo.>>file at the end of each input file, then clean it up afterwards.

If your using copy file+file... and the input files can't have the extra cr/lf at the end of line in the long term you may need to add the linebreak, copy, then remove the linebreak in each source file.

I'm not sure if your using sfk as per my suggestion, but anyway here's what I think is probably a better way of using it:


sfk filter yourtextfile.txt -no-empty-lines -no-blank-lines -write -yes
echo.>>yourtextfile.txt
sfk replace yourtextfile.txt -bin /0D0A0D0A// -yes


0

Response Number 22
Name: ksuchetan
Date: October 14, 2008 at 05:18:50 Pacific
Reply:

hi,

the new solution did not work for me.

IT SAYS

: is not recognised as internal or external command,operable or batch file.


Thanks

Thanks In Advance


0

Response Number 23
Name: ksuchetan
Date: October 14, 2008 at 06:02:05 Pacific
Reply:

and regarding the copy command

can you suggest me in general how to combine multiple files using copy command sequentially.

Thanks In Advance

Thanks In Advance


0

Response Number 24
Name: ksuchetan
Date: October 14, 2008 at 09:56:22 Pacific
Reply:

IVO\M2\JUDAGO...

Please help me with this script

Hi,

I have two issues with this script now,

I have modified the script as follows now.

@echo off
setLocal EnableDelayedExpansion


CD%1
TYPE NUL>"%2_COMBINED.TXT"
TYPE NUL>"COMBINED.TXT"

for /f "tokens=* delims= " %%t in ('dir/b/a-d *%2*') do (
for /f "tokens=* delims= " %%a in (%%t) do (
set str=%%a
if not "!str!"==" " echo.!str!>> COMBINED.TXT
)
)

for /f "tokens=* delims= " %%a in (COMBINED.TXT) do (
echo %%a>> %2_COMBINED.TXT
)


the issue is

One of the file has record

AAA|I|130001164821|130000143139|Deborah!|RN|||2008-10-06 03:08:39

the combine file has this record as
AAA|I|130001164821|130000143139|Deborah08:39

Please guide.

another issue is

copy /b *%2* %2_combined.txt

The records of one file are copied in one line
and the next file copy starts from the same line


like

record1
record2
record3 record4
record5

whereas it should be

record1
record2
record3
record4
record5

Please guide it is very urgent

Thanks In Advance

Thanks In Advance


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: remove carriage return at file end

Insert carriage return at column? www.computing.net/answers/programming/insert-carriage-return-at-column/17254.html

Eval reg entry w/carriage returns www.computing.net/answers/programming/eval-reg-entry-wcarriage-returns/15573.html

Copy text via specific characteris www.computing.net/answers/programming/copy-text-via-specific-characteris-/15143.html