0

I have a text file with id's separated by new line

I want this text file to be comma separated using Batch script

Refer my code below, this code generates Comma seperated string but still with new line. I want the comma seperated string in single line

@echo on & setlocal ENABLEDELAYEDEXPANSION 
for /f "tokens=* delims=" %%a in (input.txt) do (
set /a N+=1
echo ^%%a^,>>output.txt
)

input.txt is

1
2
3
4
5

Output.txt should be 1,2,3,4,5

2 Answers 2

2

This is easier with PowerShell:

(Get-Content input.txt) -join ',' | Out-File output.txt
Sign up to request clarification or add additional context in comments.

Comments

1
@echo off
    setlocal enableextensions disabledelayedexpansion

    set "first=1"
    (for /f "delims=" %%a in (input.txt) do (
        if not defined first ( set /p"=,%%a" ) else ( set /p"=%%a" & set "first=" )
    )) <nul >output.txt 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.