Search code examples
windowsbatch-filevlookup

Windows Batch Script VLOOKUP Output Assistance


Data File 1:

Account,Department,Location,Amount  
10000,DEPT_100,LOC_101,1000  
20000,DEPT_200,LOC_200,2000  
30000,DEPT_300,LOC_300,3000  
40000,DEPT_400,LOC_400,4000

Data File 2:

Account,Department,Location,Amount  
10000,DEPT_100,LOC_100,1000  
20000,DEPT_200,LOC_200,2000  
30000,DEPT_300,LOC_300,3000  
40000,DEPT_400,LOC_400,4000  

Desired Output (Data File 1 - Data File 2 - Difference:)

10000,DEPT_100,LOC_101,1000 - 10000,DEPT_100,LOC_100,1000 - Difference: LOC_100

Current Output:

10000,DEPT_100,LOC_100,1000 - 10000DEPT_100LOC_1001000,
@echo off
setlocal enabledelayedexpansion

set "file1=input_file1.csv"
set "file2=input_file2.csv"
set "output_file=differences_output.csv"

:: Concatenate columns in both files
for /f "usebackq tokens=1-4,* delims=," %%a in ("%file1%") do (
    set "line=%%a,%%b,%%c,%%d - %%a%%b%%c%%d,%%e"
    echo !line!>>temp_file1.csv
)

for /f "usebackq tokens=1-4,* delims=," %%a in ("%file2%") do (
    set "line=%%a,%%b,%%c,%%d - %%a%%b%%c%%d,%%e"
    echo !line!>>temp_file2.csv
)

:: Perform VLOOKUP
findstr /v /g:temp_file1.csv temp_file2.csv > %output_file%

:: Loop through records in the output file and display differences
for /f "usebackq tokens=1-5,* delims=-," %%a in ("%output_file%") do (
    echo Record: %%a,%%b,%%c,%%d,%%e - Difference: %%f
)

:: Cleanup temporary files
del temp_file1.csv
del temp_file2.csv

echo Done. Differences written to %output_file%

Solution

  • Mmm... I think you should first define what "File difference" means...

    However, if you have two files with the same number of lines that match one-by-one and want to know which fields in the second file don't appears in the first file, then this solution do the trick:

    @echo off
    setlocal EnableDelayedExpansion
    
    < test2.txt (
       for /F "delims=" %%a in (test1.txt) do (
          set /P "line2="
          set "diffs= !line2:,= ! "
          for %%b in (%%a) do set "diffs=!diffs: %%b =  !"
          if "!diffs!" neq "     " (
             echo %%a - !line2! - Difference:!diffs!
          )
       )
    )
    

    Output:

    10000,DEPT_100,LOC_101,1000 - 10000,DEPT_100,LOC_100,1000 - Difference:   LOC_100
    

    Be aware that there are several undefined details or limitations. For example: the lines can not have spaces, nor asterisks, nor question-marks, nor exclamation-marks. There must be 4 fields in each line, precisely. And others...