Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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.

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)?

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!

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.)

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 AdvanceThanks In Advance

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 supposeI have file A,B,C
Content of the file A is
ID NAME CITY
01 RRR CA
02 EEE AZContent of file B
ID NAME CITY
Content of file C
ID NAME CITY
08 WWW NJ
the o\p file shuld be01 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
102008AAthat is I want to combine content of these files with
*AAI 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

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.

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

To clean empty lines out of a file code
for /F "tokens=* delims=" %%a in (MyFile.in) do echo.%%a>> MyFile.out

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?

sure,I can send the file across to you
can you please guide me how to upload it to the server?
Thanks In Advance

actually if the file has the last line
with spacei mean
aa ggg 123
gg bbb 2345
tt yyy 2789
<space>
this is the incoming fileI 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 problemIf i just rename the file as %2_combined the process runs fine
Please help
I am stuck because of this script
Thanks In Advance

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 -yesYou 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

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.txtJust 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.

OOPS
not quite
Try this:====================================
@echo off > newfile
setLocal EnableDelayedExpansionfor /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
)

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 isOne 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:39Please 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
likerecord1
record2
record3 record4
record5whereas it should be
record1
record2
record3
record4
record5Please guide it is very urgent
Thanks In Advance

This should get around the pipe symbols.
set str=%%a
if not "!str!"==" " set wrk=!str:^|=--sepippipes--!&&echo !str:--sepippipes--=^|!>>combined.txtYou 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.

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
likerecord1
record2
record3 record4
record5whereas it should be
record1
record2
record3
record4
record5Please guide it is very urgent
Thanks In Advance

Hi,
The Above solution didnot work for me..:(
set str=%%a
if not "!str!"==" " set wrk=!str:^|=--sepippipes--!&&echo !str:--sepippipes--=^|!>>combined.txtI 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 advanceThanks In Advance

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.txtAs 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

hi,
the new solution did not work for me.
IT SAYS
: is not recognised as internal or external command,operable or batch file.
ThanksThanks In Advance

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

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 isOne 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:39Please 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
likerecord1
record2
record3 record4
record5whereas it should be
record1
record2
record3
record4
record5Please guide it is very urgent
Thanks In Advance
Thanks In Advance

![]() |
![]() |
![]() |

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