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

Author Topic: delimited file of variable delimiters  (Read 2960 times)

0 Members and 1 Guest are viewing this topic.

pp8771

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows 7
    delimited file of variable delimiters
    « on: June 13, 2018, 10:48:46 AM »
    I have a very unusual batch requirement.
    I have file with a header and number of rows which are considered as data in the subsequent rows.
    Number of header columns can vary and can be determined from the number of delimters. if there are three delimiters in a row - i will have four columns in the file.
    The data rows(subsequent rows after the header) are not constant it can vary 2 to end of file

    Sample file

    Quote
    COMPID|COMPNAME|ADDRESS|YEAROFESTABLISTMENT
    100|XYC|AWER RD|12072018
    120|BNM|PQTY RD|12082018


    Required Output will be header rows followed by subsequent rows.In the end of each row i will mention also the folder name(F1) and file name (u1.dat).
    There are multiple files in each such folders.

    1)
    Quote
    COMPID,COMPNAME,ADDRESS,YEAROFESTABLISTMENT##"100","XYC","AWER RD","12072018"##F1.u1
    COMPID,COMPNAME,ADDRESS,YEAROFESTABLISTMENT##"120","BNM","PQTY RD","12082018"##F1.u1

    other output  of single row
    2) COMPID,COMPNAME,ADDRESS,YEAROFESTABLISTMENT##F1.u1

    Please how we can we transalate the requirement using batch to obtain the above output.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: delimited file of variable delimiters
    « Reply #1 on: June 13, 2018, 12:10:27 PM »
    You really haven't specified enough details.  Really need to know what the folder structure is and are you expecting each file to have its own output file or are you combining all the files into one.

    Regardless of that, here is the base code to do one file. Hopefully you can figure out the rest.
    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion

    set "filename=u1.dat"
    set "folder=F1"

    REM Get header row
    set /p "header="<"%filename%"
    set "header=%header:|=,%"

    REM get base file name witout exention
    FOR %%G IN ("%filename%") do set "basename=%%~nG"

    REM Read file
    FOR /F "skip=1 usebackq delims=" %%G IN ("%filename%") DO (
    set "line=%%~G"
    set line="!line:|=","!"
    echo %header%##!line!##%folder%.%basename%
    )
    pause
    « Last Edit: June 13, 2018, 12:37:47 PM by Squashman »

    pp8771

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Windows 7
      Re: delimited file of variable delimiters
      « Reply #2 on: June 14, 2018, 07:45:45 PM »
      Thanks for the response.

      I used your code and not able to print what I actually require

      Code: [Select]
      @echo off
       setlocal enabledelayedexpansion
       set WORKING_DIRECTORY=%cd%
      pushD %WORKING_DIRECTORY%
      REM echo %WORKING_DIRECTORY%
      for /f "usebackq tokens=*" %%a in (`dir /b/s/a:d MigrationPoc`) do (
       echo:%%~nxa
        set "vfolder=%%~nxa"
       for /f "usebackq tokens=*" %%a in (`dir /a-d /b %%a` ) do  (
        echo %%a
        echo:%%~na
        set vfilenamewithext=%%a
        set vfilename=%%~na
        set /p "header="<"!vfilenamewithext!"
        set "header=%header:|=,%"
       FOR /F "skip=1 usebackq delims=" %%G IN ("!vfilenamewithext!") DO (
          set "line=%%~G"
          set line="!line:|=","!"
          echo %header%##!line!##!vfolder!.!vfilename!
       )
       )
       )
       pause
      popD


      But issue with reading the file  persists and I cannot able to print the line number along with file contents also.

      My output is:

      Code: [Select]
      F1
      u1.sql
      u1
      REM THE FILE f1 contains
       COMPID|COMPNAME|ADDRESS|YEAROFESTABLISTMENT
      100|XYC|AWER RD|12072018
      120|BNM|PQTY RD|12082018

      rem sample output:
      COMPID,COMPNAME,ADDRESS,YEAROFESTABLISTMENT##"100","XYC","AWER RD","12072018"##F1.u1##<line no> 2
      COMPID,COMPNAME,ADDRESS,YEAROFESTABLISTMENT##"120","BNM","PQTY RD","12082018"##F1.u1##<line no> 3

      The system cannot find the file specified.
      The system cannot find the file u1.sql.
      b1.sql
      b1
      The system cannot find the file specified.
      The system cannot find the file b1.sql.
      c1.sql
      c1
      The system cannot find the file specified.
      The system cannot find the file c1.sql.
      d1.sql
      d1
      The system cannot find the file specified.
      The system cannot find the file d1.sql.
      Sch_B
      File Not Found
      Sch_C
      File Not Found
      Sch_D
      a1.sql
      a1
      The system cannot find the file specified.
      The system cannot find the file a1.sql.
      b1.sql
      b1
      The system cannot find the file specified.
      The system cannot find the file b1.sql.
      c1.sql
      c1
      The system cannot find the file specified.
      The system cannot find the file c1.sql.
      d1.sql
      d1
      The system cannot find the file specified.
      The system cannot find the file d1.sql.

      Note if the folder contains no file i need to skip that folder which is not happening.
      « Last Edit: June 14, 2018, 07:57:34 PM by pp8771 »

      pp8771

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Windows 7
        Re: delimited file of variable delimiters
        « Reply #3 on: June 15, 2018, 05:33:37 PM »
        Can I get any kind of assistance in this regard? I need a help  on this issue.