1.

Solve : delimited file of variable delimiters?

Answer»

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.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%
)
pauseThanks 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.Can I get any kind of assistance in this regard? I need a help on this issue.


Discussion

No Comment Found