0

I have a csv file produced as:

"a","b","c","d"
"a","b","c","d"

But on importing it into an application it reads three lines in which I presume is caused by a carriage return character at the end of the 2nd line. If I open the file in notepad the curosr sits on the 3rd file, hitting back space to return the cursor to the end of line 2 and saving the file works as expected.

How can I remove this carriage return/line feed/return the cursor to the end of line 2?

I'd like to accomplish this in a batfile. I have tried echoing content and then using type/findstr without success.

Really appreciate it - i've spent a lot of time searching but without success so far. Thanks

n34_panda
  • 2,577
  • 5
  • 24
  • 40
  • Related: [Windows batch: echo without new line](http://stackoverflow.com/q/7105433) – aschipfl Jul 22 '16 at 13:25
  • How can I use it to edit the CSV file? I have tried echoing and using type without any luck. The csv file is produced by an outside process, my batfile is simply going to copy the file but in doing so also (hopfully) remove this carriage return on the last line. – n34_panda Jul 22 '16 at 13:36
  • See [my answer](http://stackoverflow.com/a/38527900) below... – aschipfl Jul 22 '16 at 13:39
  • 1
    I would argue that there is not an empty line, and your application that is loading the CSV is flawed. CSV are examples of text files, and there is a long standing convention that every line should be terminated by an end-of-line sequence (EOL), which is carriage return, linefeed on Windows. There are many applications that allow for the last line of a CSV to not end with EOL, But an application should not require that the last line be missing EOL. – dbenham Jul 22 '16 at 18:53
  • I would definitely agree with the that, may send over a bug report too! I've got it working that's to the answer but it was tedious, I've had to strip down the headers too and I was surprised the destination application couldn't handle that (at least it handled text qualifiers...) – n34_panda Jul 22 '16 at 18:55
  • My rant does not solve your problem. You could use [JREPL.BAT](http://www.dostips.com/forum/viewtopic.php?t=6044) `jrepl "\r?\n(?![\s\S])" "" /m /f "yourFile.csv" /o -` – dbenham Jul 22 '16 at 19:01

1 Answers1

1

The trick is to use set /P instead of echo as it does not produce a trailing line-break, and add them explictly but for the last line using echo/:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "FLAG="
> "\path\to\returnfile.csv" (
     for /F "usebackq delims=" %%L in ("\path\to\yourfile.csv") do (
        if defined FLAG echo/
        < nul set /P ="%%L"
        set "FLAG=#"
    )
)

endlocal
exit /B

See also this post for the set /P approach: Windows batch: echo without new line.

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99