Batch: delete line feed from end of text file using filepaths

筅森魡賤 提交于 2020-01-15 08:51:53

问题


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

回答1:


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%"


来源:https://stackoverflow.com/questions/41983065/batch-delete-line-feed-from-end-of-text-file-using-filepaths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!