View unanswered posts | View active topics It is currently 24 Apr 2014 15:15



Post new topic Reply to topic  [ 9 posts ] 
Merge .txt files in folders, adding commas and quotationmark 
Author Message

Joined: 16 Jul 2013 04:22
Posts: 5
Post Merge .txt files in folders, adding commas and quotationmark
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:
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:
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:
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:
"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


16 Jul 2013 04:40
Profile

Joined: 23 Jun 2013 06:15
Posts: 568
Location: Germany
Post Re: Merge .txt files in folders, adding commas and quotation
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:
@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


16 Jul 2013 08:24
Profile

Joined: 16 Jul 2013 04:22
Posts: 5
Post Re: Merge .txt files in folders, adding commas and quotation
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:
@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?


16 Jul 2013 16:18
Profile

Joined: 23 Jun 2013 06:15
Posts: 568
Location: Germany
Post Re: Merge .txt files in folders, adding commas and quotation
Just help those closest to you as much as you can, as much as you want! :D

Regards,
penpen.


16 Jul 2013 16:36
Profile

Joined: 16 Jul 2013 04:22
Posts: 5
Post Re: Merge .txt files in folders, adding commas and quotation
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
Quote:
-åäö
it messes with the batch file and the output file gets empty on this line.

Is it possible to make it work?


17 Jul 2013 08:59
Profile

Joined: 23 Jun 2013 06:15
Posts: 568
Location: Germany
Post Re: Merge .txt files in folders, adding commas and quotation
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:
@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


17 Jul 2013 14:17
Profile

Joined: 10 Feb 2012 02:20
Posts: 3675
Post Re: Merge .txt files in folders, adding commas and quotation
The codepage affects the characters used. Try setting it to 850 or 1252 with the CP command.


17 Jul 2013 18:14
Profile

Joined: 16 Jul 2013 04:22
Posts: 5
Post Re: Merge .txt files in folders, adding commas and quotation
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:


18 Jul 2013 04:59
Profile

Joined: 19 Jul 2013 11:35
Posts: 15
Location: Brazil
Post Re: Merge .txt files in folders, adding commas and quotation
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.


19 Jul 2013 11:53
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 


Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.