0

I have file.txt where I have values in format:

text1
text2
text3

Anyone have the idea to open such a file in a script and modify it to

'text1','text2','text3'

so far i have done it manually in notepad++ but i'd rather put it in some script just don't know how:

first step: 
find what: ^|$  
replace with: '

and

second step:
find what: [\r\n]+
replace with: ,
3
  • 1
    Batch hasn't any REGEX support (except the very crippled version in findstr, which isn't helpful here). When you don't want to switch to another language (for example PowerShell) and are willing to use a third-party tool, I recommend jrepl Commented May 20, 2021 at 12:54
  • 1
    it could be powershell, perl, python, but I don't know them. I would like to put such a script in the flow of the process Commented May 20, 2021 at 13:01
  • Your comment above, implies that you do know batch files; so where is the batch file code you want us to help you to fix? Commented May 20, 2021 at 15:16

1 Answer 1

2

You want to convert a file with lines into a single-line file, where the single (original) lines are single-quoted and separated by a comma.

Not very efficient, but straightforward:

@echo off
set "infile=input.txt"
set "outfile=output.txt"
del "%outfile%" 2>nul
for /f "usebackq delims=" %%a in ("%infile%") do (
  if not exist "%outfile%" (
    <nul set /p "='%%a'" > "%outfile%
  ) else (
    <nul set /p "=,'%%a'" >>"%outfile%"
  )
)
>>"%outfile%" echo(
type "%outfile%"

Take line by line, if it's the first line, write the quoted value, else write a comma plus the quoted value, both with the <nul set /p trick to write without a linefeed.

Sign up to request clarification or add additional context in comments.

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.