1

This question is related to this question: Batch: delete line feed from end of text file?

So I have a txt-file containing some information and I want to remove the last rows LFCR (line feed, carriage return) chars.

I need to point out the source file path and the destination file path using variables and the filename as a parameter (%1).

The batch code

@echo off
set @srcfile="D:\AgrCC\AgrTest\Report Results\%1"
set @dstfile="D:\AgrCC\AgrTest\Data Export\%1"
setlocal DisableDelayedExpansion
set "firstLineReady="
(
    for /F "eol=$ delims=" %%a in (%@srcfile%) DO (
        if defined firstLineReady (echo()
        set "firstLineReady=1"
        <nul set /p "=%%a"
    )
) > %@dstfile%

But instead of removing the LFCR chars I end up with a file looking like this in the destination folder:

Destination file content

D:\AgrCC\AgrTest\Report Results\bg1baa.725
Community
  • 1
  • 1
dadde
  • 649
  • 1
  • 11
  • 24

1 Answers1

4

In the for-loop you need the usebackq option, so that you can still use a quoted filename to be safe against spaces.

Do not add the quotation marks to the variable content, instead use the extended set syntax.

set "var=content"`

You should change

set "@srcfile=D:\AgrCC\AgrTest\Report Results\%~1"
set "@dstfile=D:\AgrCC\AgrTest\Data Export\%~1"
...
for /F "usebackq eol=$ delims=" %%a in ("%@srcfile%") DO (
...
) > "%@dstfile%"
jeb
  • 78,592
  • 17
  • 171
  • 225