Merge .txt files in folders, adding commas and quotationmark

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
LiuKang
Posts: 5
Joined: 16 Jul 2013 04:22

Merge .txt files in folders, adding commas and quotationmark

#1 Post by LiuKang » 16 Jul 2013 04:40

I have different folders with a lot of .txt files.

I want to be able to merge the .txt-files in each folder, so the location of the input or output-files should not be specified in the code, the .bat-file is to be placed in any folder and output file should also be generated here.

Example:

meat.txt

Code: Select all

Frankfurter tail turkey doner
<p>Bacon ipsum dolor sit amet turkey sausage brisket pork.</p>

<p>Tongue swine turducken capicola shoulder hamburger pig.<p/>

<p>Ball tip jerky ham, doner <a href=""https://en.wikipedia.org/wiki/Meat"">filet mignon ham</a> hock bresaola jowl andouille pig cow</p>


batman1.txt

Code: Select all

Batman
<p>You either die a hero or you live long enough...</p>

<p>...to see yourself become the villain.</p>


I want to merge these into a single .txt file, which has one files content per row.

I have the following code right now:

Code: Select all

for %%f in (*.txt) do type "%%f" >> combined.txt


Which merges all the content into one file.

But in combined.txt every file gets many rows, instead of one.

I was wondering if it's possible:

    - Wrap quotationmarks around the first line
    - Add a comma after the first line
    - Wrap quotationmarks around the first <p> tag and the last </p> tag
    - Remove the blank spaces/linebreaks between </p> and <p>

I output .txt file I want should look exactly like this:

Code: Select all

"Frankfurter tail turkey doner","<p>Bacon ipsum dolor sit amet turkey sausage brisket pork.</p><p>Tongue swine turducken capicola shoulder hamburger pig.<p/><p>Ball tip jerky ham, doner <a href=""https://en.wikipedia.org/wiki/Meat"">filet mignon ham</a> hock bresaola jowl andouille pig cow</p>"
"Batman","<p>You either die a hero or you live long enough to see yourself become the villain.</p>"


Is this possible?

Would love to get some help!

Cheers!

/ Lui Kang

penpen
Expert
Posts: 1452
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Merge .txt files in folders, adding commas and quotation

#2 Post by penpen » 16 Jul 2013 08:24

If the lines of the textfile you want to combine are not too long (around 8192 bytes), and if the combined file should be created in the same directory then the following may help you:

Code: Select all

@echo off
cls
setlocal
set "combined=combined.txt"
(
  for %%a in (*.txt) do (
    if not "%%a" == "%combined%" (
      echo %%a 1>&2
      set "firstLine=true"
      for /f "tokens=1,* delims=:   " %%b in ('findstr /n "^" %%a') do (
        if defined firstLine (
          set /p =""%%c",""
          set "firstLine="
        ) else if not "%%c" == "" (
          set /p =%%c
        )
      )
      echo "
    )
  )
) > %combined% <nul
endlocal
goto:eof

penpen

LiuKang
Posts: 5
Joined: 16 Jul 2013 04:22

Re: Merge .txt files in folders, adding commas and quotation

#3 Post by LiuKang » 16 Jul 2013 16:18

penpen wrote:If the lines of the textfile you want to combine are not too long (around 8192 bytes), and if the combined file should be created in the same directory then the following may help you:

Code: Select all

@echo off
cls
setlocal
set "combined=combined.txt"
(
  for %%a in (*.txt) do (
    if not "%%a" == "%combined%" (
      echo %%a 1>&2
      set "firstLine=true"
      for /f "tokens=1,* delims=:   " %%b in ('findstr /n "^" %%a') do (
        if defined firstLine (
          set /p =""%%c",""
          set "firstLine="
        ) else if not "%%c" == "" (
          set /p =%%c
        )
      )
      echo "
    )
  )
) > %combined% <nul
endlocal
goto:eof

penpen


WOW!

This works perfectly!

I am so impressive of your knowledge.

Is there any way I can make up for your help?

penpen
Expert
Posts: 1452
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Merge .txt files in folders, adding commas and quotation

#4 Post by penpen » 16 Jul 2013 16:36

Just help those closest to you as much as you can, as much as you want! :D

Regards,
penpen.

LiuKang
Posts: 5
Joined: 16 Jul 2013 04:22

Re: Merge .txt files in folders, adding commas and quotation

#5 Post by LiuKang » 17 Jul 2013 08:59

penpen wrote:Just help those closest to you as much as you can, as much as you want! :D

Regards,
penpen.


Will do! :)

One thing:

If the filename contains any of these chars
-åäö
it messes with the batch file and the output file gets empty on this line.

Is it possible to make it work?

penpen
Expert
Posts: 1452
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Merge .txt files in folders, adding commas and quotation

#6 Post by penpen » 17 Jul 2013 14:17

I have renamed the test text file batman.txt to -åäö.txt and got no errors on execution on XP home and Win7 home,
so i cannot fix it. The only problem i got is with the ampersand (&). I fixed it, maybe this helps you, too:

Code: Select all

@echo off
cls
setlocal
set "combined=combined.txt"
(
  for %%a in (*.txt) do (
    if not "%%a" == "%combined%" (
      echo "%%a" 1>&2
      set "firstLine=true"
      for /f "tokens=1,* delims=:   " %%b in ('findstr /n "^" "%%a"') do (
        if defined firstLine (
          set /p =""%%c",""
          set "firstLine="
        ) else if not "%%c" == "" (
          set /p =%%c
        )
      )
      echo "
    )
  )
) > %combined% <nul
endlocal
goto:eof
If this will not make it work for you on filenames containing -åäö, please post the problematic filename, and your system:
Maybe i have access to such one, then i can test it on it.

penpen

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Merge .txt files in folders, adding commas and quotation

#7 Post by foxidrive » 17 Jul 2013 18:14

The codepage affects the characters used. Try setting it to 850 or 1252 with the CP command.

LiuKang
Posts: 5
Joined: 16 Jul 2013 04:22

Re: Merge .txt files in folders, adding commas and quotation

#8 Post by LiuKang » 18 Jul 2013 04:59

penpen wrote:The only problem i got is with the ampersand (&). I fixed it, maybe this helps you


What can I say, this fixed the problems.

Amazing! :lol:

rodrigolima
Posts: 20
Joined: 19 Jul 2013 11:35
Location: Brazil

Re: Merge .txt files in folders, adding commas and quotation

#9 Post by rodrigolima » 19 Jul 2013 11:53

Hello LiuKang

Greetings From Brazil

I saw your post this week only about merge files into one and put comma and quotationmark.

I did the code below check it:

@echo off
cls

title Merge Files

:: Step 1: Check if file mergedFiles.txt exists
if exist mergedFiles.txt del mergedFiles.txt

:: Step 2 Merge Files
FOR %%i in (*.txt) do (

if "%%i"=="mergedFiles.txt" goto leave

SETLOCAL EnableDelayedExpansion

FOR /F "tokens=*" %%g in (%%i) do (
Set line=!line!%%g,
)

::Remove comma from last tag
echo "!line:~0,-1!" >> mergedFiles.txt

ENDLOCAL

)

:leave
PING 1.1.1.1 -n 1 -w 2000 >NUL
goto:eof


Your post helps me to create a new solution using this concept, here it goes:

I use it to create html reader and extract only table tag "<table></table>" into text file.
After running, I call another batch file to remove duplicated lines.

I used this information to extract data from table cell and use it to send by email(sendmail) to another proccess.

Thank you so much and see you in next Challenge.

Post Reply