Paulo
Paulo

Reputation: 361

Batch file Remove last charater on last loop item

I have batch file that gives-me all files inside a folder and creates a txt file with the file names, separated by comma ",". On the last loop i need the comma don't appear.

Results: DOc1,DOc2,DOc2,DOc2,DOc1,

This is What i need:(Whithout last comma) DOc1,DOc2,DOc2,DOc2,DOc1

I think i'm missing something on my code.

@echo off
<nul (
for /f "eol=: delims=" %%F in ('dir /b /o:n ^| findstr /vile ".bat .txt"') do set /p ="%%F, "
) >fileList.txt

Thanks for any help

Upvotes: 0

Views: 119

Answers (2)

aschipfl
aschipfl

Reputation: 34929

Define a flag-like variable that indicates whether it is the first iteration and apply the separator conditionally:

@echo off
set "FIRST=#"
< nul (
    for /F "eol=: delims=" %%F in ('dir /B /O:N ^| findstr /VILE ".bat .txt"') do (
        if not defined first (set /P =", ") else set "FIRST="
        set /P ="%%F"
    )
) > "fileList.txt"

Upvotes: 2

Compo
Compo

Reputation: 38642

You could use a method of determining whether or not the file is the first returned, adjusting the output accordingly. In this case, the findstr command you're using already, has a method of determining the line number, (its /N option):

@Echo Off
SetLocal EnableExtensions
< NUL (
    For /F "Tokens=1* Delims=:" %%G In (
        'Dir /B /A:-D /O:N ^| %__AppDir__%findstr.exe /VNLIE ".bat .txt"'
    ) Do If %%G Equ 1 (Set /P "=%%H") Else Set /P "=, %%H"
) > "fileList.txt"

Upvotes: 2

Related Questions