Home > Blockchain >  Calling a function for every attribute I want to read from multiple .txt files and write to .csv fil
Calling a function for every attribute I want to read from multiple .txt files and write to .csv fil

Time:11-24

I tried to call a function for every attribute (column) that I want to read from 4 .txt files and then write into a .csv file. One column has flawed output and the code should have a few logic flaws as I haven't learned batch cleanly from scratch. Do you know a fix?

Link to previous solved question: The result I got

echo Name;Prename;Sign;Roomnumber;Phonenumber > sorted.csv

for /f  "tokens=1,2 delims= "  %%a in (TestEmployees.txt) do (
call :findSign %%a %%b
)

:findSign
set prename=%1
set name=%2

for /f  "tokens=1,2 delims= " %%a in (TestSign.txt) do (
    if "%name%"=="%%a" (
      call :findRoomNumber
    )
)
    :End
    :findRoomNumber
    set sign=%1
    
        for /f "tokens=1,2 delims=|" %%q in (TestRoomNumber.txt) do (
            if "%sign%"=="%%q"    (
            
            call :findPhoneNumber
            )
        )
               :End
    :findPhoneNumber
    
                for /f "tokens=1,2 delims=;" %%u in (TestPhoneNumber.txt) do (
                    if "%%b"=="%%u"    (
                   
                        echo %name%;%prename%;%%b;%%r;%%v >> sorted.csv
                    )
                )
                    :End

CodePudding user response:

This is the way I would do it:

@echo off
setlocal EnableDelayedExpansion

rem Load PhoneNumber array
for /F "tokens=1,2 delims=;" %%a in (PhoneNumber.txt) do set "phone[%%a]=%%b"

rem Load RoomNumber array
for /F "tokens=1,2 delims=|" %%a in (RoomNumber.txt) do set "room[%%a]=%%b"

rem Load Sign array
for /F "tokens=1,2" %%a in (Sign.txt) do set "sign[%%a]=%%b"

rem Process Employees file and generate output
> sorted.csv (
   echo Name;Prename;Sign;RoomNumber;PhoneNumber
   for /F "tokens=1,2" %%a in (Employees.txt) do for %%s in (!sign[%%b]!) do (
      echo %%b;%%a;%%s;!room[%%s]!;!phone[%%s]!
   )
)

CodePudding user response:

@ECHO OFF
SETLOCAL
rem The following settings for the directories and filenames are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q74258020_TestEmployees.txt"
SET "filename2=%sourcedir%\q74258020_TestSign.txt"
SET "filename3=%sourcedir%\q74258020_TestRoomNumber.txt"
SET "filename4=%sourcedir%\q74258020_TestPhoneNumber.txt"
SET "outfile=           
  • Related