Home > Mobile >  Read information from multiple .txt files and sort it into .csv file
Read information from multiple .txt files and sort it into .csv file

Time:11-01

I have four .txt files [Employees, Sign, RoomNumber, PhoneNumber] and want to sort the information into a singular .csv file where [Employees, Sign, RoomNumber, PhoneNumber] are the column headers. This is an example for the .txt files where "Name" is the key to "Sign" and "Sign" is the key to "RoomNumber" and "PhoneNumber":

enter image description here

I got this input thus far (was given to me, there might be other approaches) to program the first two columns and I struggle to program the other two columns, building upon this code.

echo Name;Sign > sorted.csv

for /f  "tokens=2 delims= "  %%x in (TestEmployees.txt) do (  
call :findSign %%x           
)

:findSign
set name=%1

for /f  "tokens=1,2 delims= "  %%a in (TestSign.txt) do (
                if "%name%"=="%%a" (
                echo %name%;%%b >> sorted.csv          
                )
)

:End

The output so far looks like this and it would be kind if you can help me out, just started out with the batch scripts and coding as a whole. Thank you!

enter image description here

I was able to read out the missing information but couldn't implement the code in a way for it to show up correctly in the .csv file.

CodePudding user response:

[Untested due lack of data]

for /f  "tokens=1,2 delims= "  %%a in (TestSign.txt) do (
  if "%1"=="%%a" (
    for /f  "tokens=1,2 delims=|"  %%q in (TestRoomNumber.txt) do (
      if "%%b"=="%%q" (
        for /f  "tokens=1,2 delims=;"  %%u in (TestPhoneNumber.txt) do (
          if "%%b"=="%%u" (
            echo %1,%b,%r,%v
          )
      )
    )
  )
)

It's really just a matter of lather-rinse-repeat.

  • Related