Need Help to remove carriage return from a flat file using b

May 24, 2016 at 07:03:49
Specs: Windows 7
Hi,

I have tried the below script to remove the carriage return from the end of line from a text file.after executing the script it displays only the last record in the file.

can anyone help me on how to get all the records with the CR removed at the end of line.

@echo off
SetLocal DisableDelayedExpansion
for /f "tokens=*" %%a in ('find /n /v "" ^< "old_file.txt"') do (
set line=%%a
SetLocal EnableDelayedExpansion
set line=!line:*]=!
rem "set /p" won't take "=" at the start of a line....
if "!line:~0,1!"=="=" set line= !line!

rem there must be a blank line after "set /p"
rem and "<nul" must be at the start of the line
set /p =!line!^
<nul
endlocal
) > "new_file.txt"


Thanks,
Sam


See More: Need Help to remove carriage return from a flat file using b

Report •


#1
May 24, 2016 at 19:27:53
"rem "set /p" won't take "=" at the start of a line...."
There's the problem with batch. It is truly unfortunate that MS never added a switch to force "echo" to leave off the trailing cr/lf. There's no way that I know of to avoid this problem without compromising the source data in the output. On top of that, you might still have problems with > and < in the input stream. Highly recommend another approach. Here is a vbscript method:
'begin vbscript "nocrlf.vbs"
set fso=createobject("scripting.filesystemobject")
wscript.echo replace(fso.opentextfile(wscript.argunents(0)).readall,vbcrlf,"")
' end vbscript

Implementation (batch or commandline):
cscript /nologo nocrlf.vbs INPUTFILENAME [> OUTPUT-RESULTS-FILE]

I minimized it to make it look cool, but it should probably have some error-checking to see if there's an argument, and an alternative (stdin, or msgbox query) if not.

message edited by nbrane


Report •

#2
May 25, 2016 at 02:06:32
nbrane,

Thanks for your reply, i tried your code but it is not eliminating the CR/LF .

original file:
4SVÇHELLOÇ3AÇODOA
4FOÇPROGRAMÇ3NPÇODOA

After Running the script:
4SV€HELLO€3A€ODOA
4FO€PROGRAM€3NP€ODOA

the delimiter has changed in the new file

what i really want is:
4SVÇHELLOÇ3AÇOA
4FOÇPROGRAMÇ3NPÇOA


Report •

#3
May 25, 2016 at 09:12:19
@echo off & setlocal EnableDelayedExpansion
set /P line=< "input.txt"
type nul > "output.txt"
for /F "skip=1 delims=" %%i in ('type "input.txt"') do (
  set /P line=!line!%%i< nul
) >> "output.txt"


Report •

Related Solutions

#4
May 25, 2016 at 20:12:51
I'm lost, at this point. This:
original file:
4SVÇHELLOÇ3AÇODOA
4FOÇPROGRAMÇ3NPÇODOA
new file
4SV€HELLO€3A€ODOA
4FO€PROGRAM€3NP€ODOA

the delimiter has changed in the new file
what i really want is:
4SVÇHELLOÇ3AÇOA
4FOÇPROGRAMÇ3NPÇOA

So, I take it to mean that "0D0A" (literal, not binary) needs to be replaced by "0A"? If so, that's a "whole new world". "ODOA" does not a binary crlf make. 0D0A is a hexadecimal notation of crlf, rendered into ascii. I guess if I am to be any help at all, I need an exact copy of the test file (no sensitive data, just your working test material). Make a test file, as it appears you have already done. don't change anything. Send it over in pmail or post it here verbatim. Since my language is us/English, and I'm "code-page" challenged, I am probably not much use anyway, but I'll work with what I can to help. Meanwhile, in case I guessed correctly, maybe try this:
set fso=createobject("scripting.filesystemobject")
wscript.echo replace(fso.opentextfile(wscript.argunents(0)).readall,"0D0A","OA")

message edited by nbrane


Report •

#5
May 25, 2016 at 23:22:29
ODOA represents the hexadecimal notation of crlf. I want cr to be removed from the newfile.

Here is the sample data in the test file

4SVÇHELLOÇ3AÇ
4FOÇPROGRAMÇ3NPÇ


Report •

#6
May 26, 2016 at 03:17:19
Have you tried the script I suggested in post #3?

Please at prompt type CHCP and post the number reported. There is a problem with language dependent characters that are handled differently in ANSI (Windows) and ASCII (commabd prompt).


Report •

#7
May 26, 2016 at 05:45:08
Hi IVO,

We're having an epidemic of 'OP ignores reply'.

=====================

M2 Get custom script or take private lessons


Report •

#8
May 26, 2016 at 10:46:45
I copied the sample file into Notepad then saved it as Unicode. At prompt type sample.txt > input.txt then I executed my script in post #3: the output by type output.txt is perfect in console window. If you want it in Notepad window at prompt type

cmd /U (C type output.txt > UNIout.txt

(name as you like) and you are done (coded in UNICODE).

Report •

Ask Question