Reputation: 119
I use this batch files to create a list of files
@echo off
(for /f "delims=" %%a in ('dir/b/a-d *.tex') do echo %%a,)>liste.dat
the result is like this
file1.tex,
file2.tex,
file3.tex,
...
lastfile.tex,
how do I delete the last comma?
Upvotes: 0
Views: 288
Reputation: 67216
@echo off
setlocal EnableDelayedExpansion
set "comma="
< NUL (
for %%a in (*.tex) do (
set /P "=!comma!%%a"
set comma=,^
%Empty line%
)
echo/
) > liste.dat
EDIT: Reply to a comment
Ops! When I was developing this code I just displayed the output in the screen, where it looks correct:
C:\> test.bat
One.tex,
Three.tex,
Two.tex
Even if the output is sent to a file and the file is displayed in the screen, the output looks correct:
C:\> test.bat > output.txt
C:\> type output.txt
One.tex,
Three.tex,
Two.tex
However, the character inserted after each comma is just a LF so if this file is open with Notepad, the LF's are not converted to "end of lines"; just cmd.exe screen output converts LF to CR+LF pair ("cooked output" instead of "raw output").
The way to fix this detail is inserting a complete CR+LF pair after each comma:
@echo off
setlocal EnableDelayedExpansion
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
set "comma="
< NUL (
for %%a in (*.tex) do (
set /P "=!comma!%%a"
set comma=,!CR!^
%Empty line%
)
echo/
) > liste.dat
Upvotes: 2
Reputation: 14290
@echo off
SETLOCAL EnableDelayedExpansion
SET "in="
(
for %%a in (*.tex) do (
IF "!in!"=="" (
set in=%%a
) ELSE (
echo !in!,
set in=%%a
)
)
echo !in!
)>liste.dat
Upvotes: 3
Reputation: 6042
The problem is that you don't know how many lines/files there will be. So you'll have to use two loops the way your code is written. One to count the lines and one to perform the operations. It's way easier to have a different handling for the first line instead of the last one. So how about this:
@echo off
SETLOCAL EnableDelayedExpansion
SET /a i=0
(for /f "delims=" %%a in ('dir /b/a-d *.tex') do (
IF !i!==1 (
echo ,%%a
) ELSE (
ECHO %%a
SET /a i=1
)
)
)>liste.txt
This will generate something like this:
file1.tex
,file2.tex
,file3.tex
...
,lastfile.tex
This seems equivalent to your desired output. If you actually want to have ,
at the end of the lines instead of at the beginning of the next one, tell me and I'll update the code but it'll be quite ugly.
Upvotes: 0