|
Answer» hello I'm a novice here and facing a question about usage of bat file that you probably can help me
as an input I have a CSV file with repeated sets of values (see attached Input.txt - as SCV files are not allowed here - I CHANGED extension), like: Code: [Select]ID;CODE;VALUE ID;CODE;VALUE ...row by row - and the values are separated with semicolon inside every line.
I need as an output a TXT file with the same name as my CSV input file, but there should be some processing done.
So first of all instead of ';' there should be ',' used.
and the rows should contain (see attached Output.txt) Code: [Select]CODE,CODE,ID,RU,VALUE,B CODE,CODE,ID,RU,VALUE,B ...so first of all as you see I need CODE to be put at the beginning of the line and repeated twice then ID follows then a constat RU to be added, then VALUE and finally one more constant B to be added
I have searched for a solution and all I found is the following code: Code: [Select]@echo off SetLocal EnableDelayedExpansion for /f "tokens=*" %%i in ('dir /a-d /b *.csv') do call :repl "%%i"
:repl for /f "tokens=*" %%k in (%~nx1) do set i=%%k&echo !i:;=,!>>"%~n1.txt"which is doing the following: it creates for every provided CSV file the mirror TXT file and replaces all ';' to ','
Probably I need to use some regular expressions but I can not find how to put it in batch file.
I need from '^([^;]+);([^;]+);([^;]+)$' make '\2,\2,\1,RU,\3,B'
also it would be incredible if as an output there would be another file PRODUCED ErrorLog.txt saying which rows in a file do not fit the regular expression I mentioned, because I think there would be some inconsistencies in the input file, like instead of 3 values there would be passed only 2 values in a row.
hope I explained clearly if somebody has an experience of such transformations - I would be full of thanks if you help me
[attachment deleted by admin]ok
for the HALF of the task I have done the following: Code: [Select]@echo off SetLocal EnableDelayedExpansion for /F "tokens=*" %%i in ('dir /a-d /b *.csv') do call :repl "%%i"
:repl for /f "delims=; tokens=1,2,3" %%a in (%~nx1) do echo %%b,%%b,%%a,RU,%%c,B>>"%~n1.txt" now it is generating me the txt file with needed output
The only thing I still need is to verify each line whether it has 3 values separated by ';' and if not - put a note to ErrorLog.txt file writing the number of erroneous line
Does anyone have an idea how can I check it?
|