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 16414 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*.