i need to replace the numbers in a csv-column with the help of a list in another file. I just started to learn about batch processing and found a script that i adjusted to my needs.
This is the code i use and to solve this problem first, i just take the 3rd column, exchange the numbers and write the old one and the new one into a new file:
@echo off & setlocal enabledelayedexpansion
set "Datei=lohn_prod.csv"
set "MA=Mitarbeiter.csv"
set "Ziel=eGecko.csv"
set "t=%temp%\text.tmp"
if exist "%t%" del "%t%"
for /f "tokens=3 delims=;" %%i in ('findstr /n $ "%Datei%"') do set "Line=%%i" & call :ProcessLine
move "%t%" "%Ziel%"
goto :eof
:ProcessLine
for /f "tokens=1 delims=" %%i in ("%Line%") do set "L=%%i"
if not defined L >>"%t%" echo\& goto :eof
echo %L%
for /f "usebackq tokens=1,2 delims=;" %%i in ("%MA%") do set "L=!L:%%i=%%j!"
>>"%t%" echo %Line%, %L%
goto :eof
The old csv (lohn_prod.csv) looks like this:
15;01.09.2016;105;160,25;1;2100;210;
15;01.09.2016;105;40;1;;217;
15;01.09.2016;105;5;72;;;
15;01.09.2016;107;184;1;2213;210;
15;01.09.2016;107;7,25;1;;213;
15;01.09.2016;107;16;1;;216;
15;01.09.2016;108;241,25;1;3200;210;
15;01.09.2016;108;21,25;1;;222;
15;01.09.2016;113;174,75;1;2111;210;
15;01.09.2016;113;16;1;;216;
...
The other file (Mitarbeiter.csv) i use to replace the 3rd column looks like this:
105;40200
107;40201
108;40202
113;40203
115;40204
170;40219
172;40220
195;40222
197;40223
198;40224
199;40225
200;40226
201;40227
220;40228
235;40229
240;40230
...
The problem though is, that it seems to iterate through L again from the beginning on each loop, because i get this result:
105, 4040226
105, 4040226
105, 4040226
107, 4040227
107, 4040227
107, 4040227
108, 40202
108, 40202
113, 40203
113, 40203
115, 40204
170, 40219
170, 40219
172, 4040228
172, 4040228
172, 4040228
185, 40221
185, 40221
185, 40221
...
As it seems, it first finds 105 and replaces it with 40200, then on the next loop it finds the 200 in 40200 and replaces it with 40226, same with 107 and 201 in 40201. Since there is no number 202 in Mitarbeiter.csv, 108 gets correctly replaced with 40202.