Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Last active April 26, 2021 15:49
Show Gist options
  • Save JoeGlines/b11c8f364630d562853bbd615c29da39 to your computer and use it in GitHub Desktop.
Save JoeGlines/b11c8f364630d562853bbd615c29da39 to your computer and use it in GitHub Desktop.
Compare lists of items
;*******************************************************
; Want a clear path for learning AutoHotkey; Take a look at our AutoHotkey Udemy courses. They're structured in a way to make learning AHK EASY
; Right now you can get a coupon code here: https://the-Automator.com/Learn
;*******************************************************
#Include <default_Settings>
;************************************************************
;~ gosub Draw_Gui
;~ Draw_Gui:
gui, font, s11 , Verdana ; Set 10-point Verdana.
Gui, Add, Button, gSelect_First_File x1 y5 , &1st File ;x550 y10
Gui, Add, Button, gSelect_Second_File x1 y40,&2nd File ;x550 y10
gui, font, s9 , arial ; Set 10-point Verdana.
Gui, Add, Edit, vPath_First_File x84 y7 w500
Gui, Add, Edit, vPath_Second_File x84 y40 w500
Gui, Add, Button, gReset x1 y265 , Reset ;exit out of script
Gui, Add, Button, gGuiClose x1 y295 , E&Xit ;exit out of script
gui, font, s11 , Verdana ; Set 10-point Verdana.
Gui, Add, ListView, vHeader_Overlap gMyListView r10 x84 y70 w495 grid , Headers in both files ;-Hdr
Gui, Submit, NoHide
Gui, Show,, Compare Files based on Key v.8 ;h900 w750
return
;***********selection*******************
MyListView:
if A_GuiEvent = DoubleClick
{
LV_GetText(RowText, A_EventInfo) ; Get the text from the row's first field.
for k, v in f1
if (k=RowText)
f1_Index:=v
for k, v in f2
if (k=RowText)
f2_Index:=v
gosub Compare_Data ;MsgBox % RowText
}
return
;***********Header selected- now compare*******************
Compare_Data:
;~ MsgBox % "file 1 index is: " F1_Index "`nfile 2 index is: " F2_Index
gosub Compare_Files
return
Select_First_File:
IfWinExist, ahk_class CabinetWClass
WinActivate, ahk_class CabinetWClass
Path := Explorer_GetPath()
IfEqual, path,,SetEnv,Path,c:\
FileSelectFile, First_File , 3,%path% , Select a file you wish to purge contacts from, CSV files (*.csv)
IfEqual, First_File,,return ;break subroutine if blank
;~ First_File:=A_ScriptDir . "\_csv_List_1.csv"
Headers1:=read_Header(First_File)
guiControl,,Path_First_File,%First_File% ;update path
gosub Compare_Headers
return
Select_Second_File:
IfWinExist, ahk_class CabinetWClass
WinActivate, ahk_class CabinetWClass
Path := Explorer_GetPath()
IfEqual, path,,SetEnv,Path,c:\
FileSelectFile, Second_File, 3,%path% , Select a file you wish to purge contacts from, CSV files (*.csv)
IfEqual, Second_File,,return ;break subroutine if blank
;~ Second_File:=A_ScriptDir . "\_csv_List_2.csv"
Headers2:=read_Header(Second_File)
guiControl,,Path_Second_File,%Second_File% ;update path
gosub Compare_Headers
return
;***********Compare Headers*******************
Compare_Headers:
both:=[]
f1:=[]
f2:=[]
for k, v in headers1 ;iterate through first headers
for k2, v2 in Headers2 ;compare each second header to first one
if (v=v2) { ;if second header in first
both[A_index].=v
f1[v]:=k
f2[v]:=k2
}
gosub Update_ListView
return
;***********update ListView*******************
Update_ListView:
for k, v in both
values.=v a_enter
LV_Delete()
for k, v in both
LV_Add("", v)
LV_ModifyCol(1, "Sort")
GuiControl,,Header_Overlap , %URLs% ;updates count
return
;***********read header*******************
read_Header(File_Path){
FileReadLine, Header, %File_Path%, 1 ;read header row
Headers := StrSplit(Header, ",")
return Headers
}
;***********Find header for Index*******************
Find_Index(Headers,Index_Var){
Headers1:=read_Header(First_File)
for k ,v in Headers1
If (v = Index_Var)
Return k
}
Reset:
Reload
return
GuiClose: ; Indicate that the script should exit automatically when the window is closed.
ExitApp
return
Compare_Files:
FileRead,data1, %First_File%
FileRead,data2, %Second_File%
;~ MsgBox % IsObject(headers1)
;~ MsgBox,,first file, % data1
;~ MsgBox,,second file, % data2
d1:=[] ;define object for storage later
d2:=[] ;defin 2
loop, parse, data1,`n,`r
{
IfEqual, A_index ,1,continue ;Skip loop if blank ;used in loop to skip blanks
IfEqual, A_LoopField, ,continue ;Skip loop if blank ;used in loop to skip blanks
o1 := StrSplit(A_LoopField, ",") ;parse row on comma and store in o1 object/array
D1[o1[F1_Index]] := A_LoopField ;insert email and row index into array
}
;***********Second set*******************
loop, parse, data2,`n,`r
{
IfEqual, A_index ,1,continue ;Skip loop if blank ;used in loop to skip blanks
IfEqual, A_LoopField, ,continue ;Skip loop if blank ;used in loop to skip blanks
o2 := StrSplit(A_LoopField, ",")
D2[o2[F2_Index]] := A_LoopField
;~ MsgBox,, , %
}
;~ MsgBox,,one, % disp(d1)
;~ MsgBox,,two, % disp(d2)
SplitPath, First_File,,,,File_1 ; [, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive]
SplitPath, Second_File,,,,File_2
NF_1:="_Just_" File_1
NF_2:="_Just_" File_2
NF_3:="_Both_" File_1 "_" File_2
FileDelete,%NF_1%.csv
FileDelete,%NF_2%.csv
FileDelete,%NF_3%.csv
FileReadLine, Headers1, %First_File%, 1 ;read header row
FileReadLine, Headers2, %Second_File%, 1 ;read header row
FileAppend,%Headers1%`r`n,%Path%\%NF_1%.csv,utf-8 ;header for first file
FileAppend,%Headers2%`r`n,%Path%\%NF_2%.csv,utf-8 ;header for second file
FileAppend,%Headers2%`r`n,%Path%\%NF_3%.csv,utf-8 ;header for second file
;~ MsgBox pause
;~ to do - what columns are written from what files?
;~ need to write headers
;***********now compare items in each object and write to file*******************
for k, v in D2
if !D1[k]
FileAppend %v%`r`n,%Path%\%NF_2%.csv,utf-8 ;f3_Just_Second[v] :=v
else
FileAppend %v%`r`n,%Path%\%NF_3%.csv,utf-8 ;f2_both[v] :=v ;both is the index of the second file
for k, v in D1
if !D2[k]
FileAppend %v%`r`n,%Path%\%NF_1%.csv,utf-8 ;f1_Just_First[v] :=v
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment