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

Author Topic: Joining multiple txt. files and adding part of file name to each row  (Read 2797 times)

0 Members and 1 Guest are viewing this topic.

hudsucker

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows XP
    Hi All,

    I have a folder with multiple txt files in csv format with name convention Something_something_something_XXXXXXXX. txt where XXXXXXXX is date.

    Now I need to join those files in one and each row needs to have a the date XXXXXXXX from respective file added as column.
    So far I was able to find way to add whole file name to the row. I have found how trim file name to keep only XXXXXXXX.txt

    But I am not able either to remove the .txt nor to add the trimed name to rows. Below is the code for adding name file:

    for %%A in (*.txt) do (
      echo Processing file '%%A'
       
      FOR /F "delims=" %%L in (%%A) do (
        ECHO %%L , %%A >> New.txt
      )
    )
    I have found this to trim the file name so it leaves last 11 chararacters so XXXXXXXX.txt
    SET VAR=%%A
    CALL ECHO/%%VAR:~-11%%&ECHO/

    don't know how to use it in ECHO %%L , %%A >> New.txt

    I have seen the %~nA supposedly should return only file name so I could get rid of txt but could not find syntax that would work.

    This is probably easy but I am sooo... confused with examples I have seen that I can figure out how to join the dots. :-[

    foxidrive



      Specialist
    • Thanked: 266
    • Experience: Experienced
    • OS: Windows 8
    Re: Joining multiple txt. files and adding part of file name to each row
    « Reply #1 on: February 20, 2013, 03:30:46 AM »
    There are different ways to skin this cat.  This splits the filename into tokens divided by _ and . and takes the 4th one, which is XXXXXXXX here.

    Test it on copies of your files as it overwrites the original.

    Code: [Select]
    @echo off
    for %%A in (*.txt) do (
    for /f "tokens=4 delims=_." %%Z in ("%%A") do (
     echo Processing file '%%A'
     
      FOR /F "delims=" %%L in (%%A) do (
        ECHO %%L,"%%Z" >> New.tmp
      )
    move /y new.tmp "%%A"
    )

    )


    hudsucker

      Topic Starter


      Newbie

      • Experience: Beginner
      • OS: Windows XP
      Re: Joining multiple txt. files and adding part of file name to each row
      « Reply #2 on: February 20, 2013, 08:21:02 AM »
      Hi foxidrive

      Worked perfectly - actually the files were Something-something_something-something_XXXXXXXX. txt but with your explanation I was able to figure out this detail myself.

      Thanks a million!!

      wasimsono



        Newbie

        • Experience: Experienced
        • OS: Windows 7
        Re: Joining multiple txt. files and adding part of file name to each row
        « Reply #3 on: February 08, 2018, 09:41:43 PM »
        Hi guys

        I have came here through another forum in search of same topic. I found following code
        Code: [Select]
        @echo off
        for %%A in (*.txt) do (
          echo Processing file '%%A'

          FOR /F "delims=" %%L in (%%A) do (
            ECHO %%L%%A >> DataFile.txt
          )
        )

        and used it in my batch file. It works perfectly only when the batch file is run directly by clicking file name in any folder. Problem comes in the scene when I call this batch file in MS-Access. I used a button on form in MS-Access and used following code on Click event of the button.
        Code: [Select]
        strReportpath = "F:\meter_pictures\"
        Shell strReportpath & "Combine.bat"
        but it not working.
        Is any one have an idea what mistake I'm doing?