Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: need help: How to write a batch to combine more 100 text files to one text file?  (Read 16416 times)

0 Members and 1 Guest are viewing this topic.

netsking

    Topic Starter


    Starter

    Hi, there,

    I am new comer in here.
    Could anybody help me to write a batch to combine more than 100 text files to one text file?
    I knew I can do it by copy command like: file1.txt+file2.txt.... file all.txt. however it is not efficient way.
    Thanks a lot in advance.

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Are the files all sequential? Are the names basically the same? Are they in the same location? Can you give a list of SOME of the names?
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    netsking

      Topic Starter


      Starter

      Yes. They are all in the same directory and the file names are sequential.

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Put this in a batch file.

      @echo off
      for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

      That should do it.
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      netsking

        Topic Starter


        Starter

        Put this in a batch file.

        @echo off
        for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

        That should do it.

        I make a batch file like:

        @echo off
        for /l %%a in (0,1,50)
        songci_%%a.txt>>endresult.txt

        the file name is songci_#.txt,
        But run this batch, nothing happened. I am freshman. Could you tell me more? Thanks a lot.



        Helpmeh



          Guru

        • Roar.
        • Thanked: 123
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        @echo off
        for /l %%a in (0,1,50) do type songci_%%a.txt >> endresult.txt
        That should work.
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        Salmon Trout

        • Guest
        @echo off
        for /l %%a in (0,1,50) do type songci_%%a.txt>>endresult.txt

        netsking

          Topic Starter


          Starter

          I got it. I rewrite it  as you mentioned. This time it works. Thank you very much for your help.


          billrich



            Rookie

            Thanked: 1
            Write a batch to combine more than 100 text files to one text file?


            C:\batch\backup>type  combine.bat
            Code: [Select]
            @echo off
            echo. > c:\tmp2\endresult.txt
            cd c:\batch\backup\

            for /f %%a in ('dir /b  *.txt') do type %%a >> c:\tmp2\endresult.txt

            echo  type  endresult.txt

            type c:\tmp2\endresult.txt

            C:\batch\backup>

            OUTPUT:

            C:\batch\backup>combine.bat
             type  endresult.txt

             hello liz
             hello liz


            C:\batch\backup>

            Salmon Trout

            • Guest
            note that by adding more switches  to dir /b you can govern the order in which the files are added

            Code: [Select]
            /O          List by files in sorted order.
             sortorder    N  By name (alphabetic)       S  By size (smallest first)
                          E  By extension (alphabetic)  D  By date/time (oldest first)
                          - Prefix to reverse order

            the case does not matter.

            for example dir /b /on would sort by name a - z and dir /b /o-n would sort z - a


            Helpmeh



              Guru

            • Roar.
            • Thanked: 123
              • Yes
              • Yes
            • Computer: Specs
            • Experience: Familiar
            • OS: Windows 8

            C:\batch\backup>type  combine.bat
            Code: [Select]
            @echo off
            echo. > c:\tmp2\endresult.txt
            cd c:\batch\backup\

            for /f %%a in ('dir /b  *.txt') do type %%a >> c:\tmp2\endresult.txt

            echo  type  endresult.txt

            type c:\tmp2\endresult.txt

            C:\batch\backup>

            OUTPUT:

            C:\batch\backup>combine.bat
             type  endresult.txt

             hello liz
             hello liz


            C:\batch\backup>
            The problem was solved. We've made a dollar already, so we don't need the extra $0.10.
            Where's MagicSpeed?
            Quote from: 'matt'
            He's playing a game called IRL. Great graphics, *censored* gameplay.

            ghostdog74



              Specialist

              Thanked: 27
              I knew I can do it by copy command like: file1.txt+file2.txt.... file all.txt. however it is not efficient way.
              if you can, download GNU tools(see my sig) and use cat
              Code: [Select]
              c:\test> cat file*txt > newfile.
              that's it.

              billrich



                Rookie

                Thanked: 1
                The problem was solved. We've made a dollar already, so we don't need the extra $0.10.

                Casper had a better way:

                "Download GNU tools(see my sig) and use cat

                Code:
                c:\test> cat *.txt >  newfile.txt"

                We saw no output for:

                @echo off
                for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

                Will it work?  You must modify ENDNUMBER  each time the program is run?


                Casper's  code and my code are much better.  We showed  the output. Proof the code works.


                 
                 
                 
                « Last Edit: December 14, 2009, 10:07:43 PM by billrich »

                billrich



                  Rookie

                  Thanked: 1
                  C:\batch\backup>dir
                   Volume in drive C has no label.
                   Volume Serial Number is F4A3-D6B3

                   Directory of C:\batch\backup

                  12/14/2009  10:24 PM    <DIR>          .
                  12/14/2009  10:24 PM    <DIR>          ..
                  12/14/2009  06:08 PM               192 combine.bat
                  12/13/2009  12:25 PM                13 liz1212.txt
                  12/13/2009  12:25 PM                13 liz1213.txt
                  12/14/2009  10:25 PM                26 newfile
                                 4 File(s)            244 bytes
                                 2 Dir(s)  305,526,177,792 bytes free

                  C:\batch\backup>type  *.txt > newfile.

                  liz1212.txt



                  liz1213.txt



                  C:\batch\backup>cat newfile
                   hello liz
                   hello liz

                  C:\batch\backup>cat *.txt > newfile.txt


                  C:\batch\backup>
                  « Last Edit: December 14, 2009, 10:09:08 PM by billrich »

                  Salmon Trout

                  • Guest
                  bill, you may only have a small b in your "name", and not a big one, these days, but man you sure are a *censored*.


                  ghostdog74



                    Specialist

                    Thanked: 27
                    what happened?? did i miss anything??

                    Salmon Trout

                    • Guest
                    what happened?? did i miss anything??

                    bill said hi to some chick called Liz. Twice.

                    billrich



                      Rookie

                      Thanked: 1
                      Code: [Select]
                      c:\test> cat *.txt > newfile.txt

                      We all have gone around the barn instead of directly through the barn.

                      The following one line by Casper is the solution:

                      cat *.txt > newfile.txt

                      or the batch equivilant :

                      type *.txt > newfile.txt

                      p.s.  Try it; it works.

                      Geek-9pm


                        Mastermind
                      • Geek After Dark
                      • Thanked: 1026
                        • Gekk9pm bnlog
                      • Certifications: List
                      • Computer: Specs
                      • Experience: Expert
                      • OS: Windows 10
                      Quote
                      I knew I can do it by copy command like: file1.txt+file2.txt.... file all.txt. however it is not efficient way.

                      OK. Glad to see you got a cool answer.Neat
                      Now I have a question.
                      In what way is the more efficient?
                       

                      billrich



                        Rookie

                        Thanked: 1
                        OK. Glad to see you got a cool answer.Neat
                        Now I have a question.
                        In what way is the more efficient?
                         

                        I agree copy, cat   and  type are all efficient. 

                        More than one way to skin a cat.


                        Helpmeh



                          Guru

                        • Roar.
                        • Thanked: 123
                          • Yes
                          • Yes
                        • Computer: Specs
                        • Experience: Familiar
                        • OS: Windows 8
                        We all have gone around the barn instead of directly through the barn.

                        The following one line by Casper is the solution:

                        cat *.txt > newfile.txt

                        or the batch equivilant :

                        type *.txt > newfile.txt

                        p.s.  Try it; it works.
                        But there could be other .txt files in the folder.
                        Where's MagicSpeed?
                        Quote from: 'matt'
                        He's playing a game called IRL. Great graphics, *censored* gameplay.

                        Salmon Trout

                        • Guest
                        billrich totally missed the point once again.

                        billrich



                          Rookie

                          Thanked: 1
                          But there could be other .txt files in the folder.

                          dir /s  *.txt or type *.txt or cat *.txt finds all the text (.txt ) files in the folder.

                          Test the above commands in any folder with text (.txt ) files.

                          _______________________________________ ________

                          The following code found nothing and/or many duplicates:

                          @echo off
                          for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

                          The Casper code works:

                          cat *.txt > newfile.txt

                          -------------------------------------
                          Dias de verano  who now calls her/himself Salmon Trout is confused and lost as usual.

                          Geek-9pm


                            Mastermind
                          • Geek After Dark
                          • Thanked: 1026
                            • Gekk9pm bnlog
                          • Certifications: List
                          • Computer: Specs
                          • Experience: Expert
                          • OS: Windows 10
                          Quote
                          The Casper code works:
                          cat *.txt > newfile.txt
                          OK. That is True.
                          What I do not understand is why he ended to put all hte TXT files into one. Any pratical application can get to all the files anyway.
                          What real application is the out there that has to have all hte TXT files in a directory merged into just one?

                          I think the right answer is You don't have too!

                          It is like asking an Eagle if he ...
                          knows how to hike in to Tuolumne Meadows in Yosemite.


                          He would say "I don't have too!"


                          Helpmeh



                            Guru

                          • Roar.
                          • Thanked: 123
                            • Yes
                            • Yes
                          • Computer: Specs
                          • Experience: Familiar
                          • OS: Windows 8
                          Good point geek. And bill, the op already said that the problem was solved, so I don't understand why there are other solutions that don't function as properly poster after the fact.
                          Where's MagicSpeed?
                          Quote from: 'matt'
                          He's playing a game called IRL. Great graphics, *censored* gameplay.

                          ghostdog74



                            Specialist

                            Thanked: 27
                            We all have gone around the barn instead of directly through the barn.

                            The following one line by Casper is the solution:

                            cat *.txt > newfile.txt

                            or the batch equivilant :

                            type *.txt > newfile.txt

                            p.s.  Try it; it works.

                            no, they are not equivalent. You should try it yourself and see. type will give you the file name as well.

                            ghostdog74



                              Specialist

                              Thanked: 27
                              OK. Glad to see you got a cool answer.Neat
                              Now I have a question.
                              In what way is the more efficient?
                               
                              i think he meant that if he has 100 text files, he is not going to type file1+file2+.....file100 on the command line like that. the more convenient way, which i think he missed out,
                              Code: [Select]
                              c:\test> copy file*txt output.txt

                              billrich



                                Rookie

                                Thanked: 1
                                And bill, the op already said that the problem was solved, so I don't understand why there are other solutions that don't function as properly poster after the fact.

                                The following code does not work:

                                @echo off
                                for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

                                ------------------------------------------------

                                Casper did nothing wrong by offering  a simple one line solution.  The readers of this board  enjoy more than one solution.

                                Please thank Casper for his insight:

                                The Casper code works:
                                cat *.txt > newfile.txt

                                ______________________________
                                Geek,

                                I have no idea what Netsking, the original poster, intentions are.  

                                Why does  Netsking, OP, need all the text files in a folder in one large file?

                                Perhaps for his Boss?

                                Thanks Ceek for your insight.   A few people understood the problem and the solutions.  It was really quite a simple problem.  Casper showed us the simple solution.

                                Thanks Casper.

                                ghostdog74



                                  Specialist

                                  Thanked: 27
                                  OK. That is True.
                                  What I do not understand is why he ended to put all hte TXT files into one. Any pratical application can get to all the files anyway.
                                  well, if you have not encountered this scenario, it doesn't mean other's won't. Say for example, you have many text log files which you generate each day and its cluttering up your screen when you do dir listings. And they are many years old. Then you might want to concat them according to each month and each year to get rid of these pieces to reduce the amount of files you have. this is just an example, but there certainly are uses for concatenation of files.


                                  Helpmeh



                                    Guru

                                  • Roar.
                                  • Thanked: 123
                                    • Yes
                                    • Yes
                                  • Computer: Specs
                                  • Experience: Familiar
                                  • OS: Windows 8
                                  Hey bill, try reading next time.
                                  I got it. I rewrite it  as you mentioned. This time it works. Thank you very much for your help.


                                  Where's MagicSpeed?
                                  Quote from: 'matt'
                                  He's playing a game called IRL. Great graphics, *censored* gameplay.

                                  billrich



                                    Rookie

                                    Thanked: 1
                                    Hey bill, try reading next time.

                                    I meant to say I could not get the following code to work on my machine:

                                    @echo off
                                    for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

                                    The solutions offered here on the Hope Board are not just for the Original poster.  Many readers can often use the information.

                                    I wonder if Helpme can show us the output for:

                                    @echo off
                                    for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

                                    Must we offer a new "ENDNUMBER" each time we run the code?

                                    I use XP.  Will the above code run for XP?

                                    An example of the Output of all code offered will help the OP plus the readers.

                                    Thank you Helpmeh  for your time and effort.



                                    Helpmeh



                                      Guru

                                    • Roar.
                                    • Thanked: 123
                                      • Yes
                                      • Yes
                                    • Computer: Specs
                                    • Experience: Familiar
                                    • OS: Windows 8
                                    You replace ENDNUMBER with the number in the last file. The OP perfectly understood, and it seemed very obvious to replace it.
                                    And no, I can not run it, as I am not on a computer.
                                    Where's MagicSpeed?
                                    Quote from: 'matt'
                                    He's playing a game called IRL. Great graphics, *censored* gameplay.

                                    ghostdog74



                                      Specialist

                                      Thanked: 27
                                      what bill (or joan or liz or mike or ??) meant is OP won't know the ENDNUMBER before hand unless he can find out from dir *.txt and change the code. Its probably better not to use the for loop range + step method, but just use the output from dir file*.txt
                                      Code: [Select]
                                      for .... (dir file*.txt) do (
                                        type .... >> output.txt
                                      )

                                      BatchFileBasics



                                        Hopeful

                                        Thanked: 18
                                        how about this? counts txt files. i found this code somewhere off the forums a while ago, it was in my batch bank

                                        replace the "set I=1" for the number the file sequence starts

                                        Code: [Select]
                                        @echo off
                                        :recurse
                                        set I=1

                                        cd
                                        FOR /f "tokens=*" %%P IN ('dir /A-d /b *.txt') do (call :showfiles "%%P")
                                        echo.
                                        echo Filecount: %I%

                                        for /l %%a in (0,1,%I%) do type file%%a.txt >> endresult.txt
                                        :showfiles
                                        echo %1
                                        set /a I+=1
                                        goto :eof
                                        When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

                                        ghostdog74



                                          Specialist

                                          Thanked: 27
                                          how about this? counts txt files. i found this code somewhere off the forums a while ago, it was in my batch bank

                                          replace the "set I=1" for the number the file sequence starts

                                          Code: [Select]
                                          @echo off
                                          :recurse
                                          set I=1

                                          cd
                                          FOR /f "tokens=*" %%P IN ('dir /A-d /b *.txt') do (call :showfiles "%%P")
                                          echo.
                                          echo Filecount: %I%

                                          for /l %%a in (0,1,%I%) do type file%%a.txt >> endresult.txt
                                          :showfiles
                                          echo %1
                                          set /a I+=1
                                          goto :eof

                                          why not just do dir file*txt ? there's no need to call another loop to count total files. Of course, unless there other files with names like filetest.txt , fileblahblah.txt and you want to isolate those that have numbers following "file", which you could do that with dir file*txt | findstr /I "file[0-9]*.txt"

                                          billrich



                                            Rookie

                                            Thanked: 1
                                            how about this? counts txt files. i found this code somewhere off the forums a while ago, it was in my batch bank

                                            replace the "set I=1" for the number the file sequence starts

                                            Code: [Select]
                                            @echo off
                                            :recurse
                                            set I=1

                                            cd
                                            FOR /f "tokens=*" %%P IN ('dir /A-d /b *.txt') do (call :showfiles "%%P")
                                            echo.
                                            echo Filecount: %I%

                                            for /l %%a in (0,1,%I%) do type file%%a.txt >> endresult.txt
                                            :showfiles
                                            echo %1
                                            set /a I+=1
                                            goto :eof


                                            Where is Output?

                                            Helpmeh



                                              Guru

                                            • Roar.
                                            • Thanked: 123
                                              • Yes
                                              • Yes
                                            • Computer: Specs
                                            • Experience: Familiar
                                            • OS: Windows 8

                                            Where is Output?
                                            Why don't YOU run it and put out any output it makes, instead of "Where is output?" over and over again. Sometimes you really sound like a broken record.
                                            Where's MagicSpeed?
                                            Quote from: 'matt'
                                            He's playing a game called IRL. Great graphics, *censored* gameplay.

                                            Salmon Trout

                                            • Guest
                                            Why don't YOU run it and put out any output it makes, instead of "Where is output?" over and over again. Sometimes you really sound like a broken record.

                                            I don't think I would have bought that record in the first place, let alone played it after it was broken. Anyhow, wasn't he banned? Why is he now unbanned?

                                            Helpmeh



                                              Guru

                                            • Roar.
                                            • Thanked: 123
                                              • Yes
                                              • Yes
                                            • Computer: Specs
                                            • Experience: Familiar
                                            • OS: Windows 8
                                            I don't think I would have bought that record in the first place, let alone played it after it was broken. Anyhow, wasn't he banned? Why is he now unbanned?

                                            New name...lowercase b is all it takes.
                                            Where's MagicSpeed?
                                            Quote from: 'matt'
                                            He's playing a game called IRL. Great graphics, *censored* gameplay.

                                            billrich



                                              Rookie

                                              Thanked: 1
                                              New name...lowercase b is all it takes.

                                              -------------------------------------

                                              Show your code and show output and stop faking it.
                                              « Last Edit: December 16, 2009, 04:27:44 PM by Fed »

                                              Helpmeh



                                                Guru

                                              • Roar.
                                              • Thanked: 123
                                                • Yes
                                                • Yes
                                              • Computer: Specs
                                              • Experience: Familiar
                                              • OS: Windows 8
                                              -------------------------------------

                                              Show your code and show output and stop faking it.
                                              1. I've shown my code, heck, YOU've even used it in your posts.
                                              2. As I've said before, I don't have access to a computer. I am on my iPod.
                                              3. How am I faking? Read the OP's post. The code worked great for him.
                                              Where's MagicSpeed?
                                              Quote from: 'matt'
                                              He's playing a game called IRL. Great graphics, *censored* gameplay.

                                              billrich



                                                Rookie

                                                Thanked: 1
                                                Read the OP's post. The code worked great for him.

                                                The problem is on my end.  I could not get the code to work on my machine.
                                                I thought I could look at your code and output  and figure what I was doing wrong.

                                                @echo off
                                                for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

                                                I might try again.

                                                Helpmeh



                                                  Guru

                                                • Roar.
                                                • Thanked: 123
                                                  • Yes
                                                  • Yes
                                                • Computer: Specs
                                                • Experience: Familiar
                                                • OS: Windows 8
                                                The problem is on my end.  I could not get the code to work on my machine.
                                                I thought I could look at your code and output  and figure what I was doing wrong.

                                                @echo off
                                                for /l %%a in (0,1,ENDNUMBER) do type file%%a.txt >> endresult.txt

                                                I might try again.

                                                Did you change ENDNUMBER? You haven't posted anything but a copypasta of my code. Post the code in YOUR batch file.
                                                Where's MagicSpeed?
                                                Quote from: 'matt'
                                                He's playing a game called IRL. Great graphics, *censored* gameplay.

                                                BatchFileBasics



                                                  Hopeful

                                                  Thanked: 18
                                                  When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

                                                  Helpmeh



                                                    Guru

                                                  • Roar.
                                                  • Thanked: 123
                                                    • Yes
                                                    • Yes
                                                  • Computer: Specs
                                                  • Experience: Familiar
                                                  • OS: Windows 8
                                                  I think bill is just frustrated because the code doesn't work for him. That happens to the best of us. We get impatient and angry.
                                                  Where's MagicSpeed?
                                                  Quote from: 'matt'
                                                  He's playing a game called IRL. Great graphics, *censored* gameplay.