wiltomap
wiltomap

Reputation: 4223

DOS batch removing last empty line in output TXT file

The following code lists the files' names in a directory :

for /f %%a in ('dir /b D:\folder\*.xml') do (
    echo %%a >> lines.txt
)

The result is (including lines numbers) :

1 86002_2014_1.xml 
2 86014_2014_1.xml 
3 86014_2014_2.xml 
4 86016_2014_1.xml 
5 86017_2014_1.xml
6

I always have an empty line at the end of any output TXT file from a Batch...

Is there a way to get a clean file without this line ?

Upvotes: 0

Views: 951

Answers (1)

MC ND
MC ND

Reputation: 70923

The output of your command generates a set of lines, all terminated with an CRLF pair. This is the normal behaviour. ....text...0x0D0x0A, the standard line termination in windows.

So, in your case, if there is another line or not is just a point of view. But, as for you, the last CRLF is not desired, it is necessary to discard the ending CRLF in all the lines (<nul set /p ".=text") and convert them in an starting CRLF (echo()

set "first="
<nul (for /f "delims=" %%a in ('dir /b D:\folder\*.xml') do (
    if defined first (echo() else (set "first=1")
    set /p ".=%%a" 
)) > lines.txt

Upvotes: 1

Related Questions