There are no column names in the file and the column separator has been changed from ";" to ","
At the beginning of the script below, a comma-separated csv file with column names in UTF-8 is created.
Because fields can contain "," (comma), the best is to use to use Loop parse, A_LoopReadLine, CSV, when the file are handling.
Code: Select all
SetBatchLines -1 ; Kör programmet så fort som möjligt
#SingleInstance Force ; Endast "ett" program kan köras samtiodigt
#NoEnv ; Undviker kontroll av tomma variabler för att se om dessa är miljövariabler, Ökar prestandan och kompatibilitet med framtida AutoHotkey ver. (rekommenderas till alla nya script)
; The main file
colName := "
( LTrim Join QC ; Q = Quotes, C = Comments
""levArtNr"" ; N-2401S
,""benämning"" ; S-KROK
,""exTxt1"" ; LÄNGD 8,5 CM
,""exTxt2"" ; SVART
,""inPris"" ; 3.36
,""antal"" ; 24
,""bestKv"" ; 24
,""userNr"" ; 114
,""user"" ; Elvira Norén
,""okänd1"" ; (okänd funktion)
,""lev"" ; FLOWERPOWER
,""levName"" ; AB Flower Power Import
,""levAdr1"" ; Vapenvägen 8
,""levAdr2"" ;
,""Ort"" ; 645 33 NOWHERE
,""okänd2"" ; (okänd funktion)
,""to"" ; Red Riding Hood
,""toMail"" ; [email protected]
,""tfn"" ; 0583 14570
,""fax"" ; 0583 10018
,""date"" ; 2022-05-16
,""clock"" ; 10:19:44
,""typ"" ; ORD
,""varugrp"" ; 3350
,""godsMärk"" ; Heminredning
,""avrop"" ;
,""multi"" ;
,""fraktGräns"" ; 15000
)"
; - - - - - - - - - - - - - - - - - - - -
; Create and read a test-CSV
regInfo =
( LTrim
"J-213441","ÄNGEL","18 X 27 CM","SORTERAT","113.52",6,6,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33 NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:51","ORD",3350,"Heminredning",,,15000
"J-123484","ÄNGEL","HÖJD 45 CM","SORTERAT","122.32",4,4,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33 NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:47","ORD",3350,"Heminredning",,,15000
"N-21343W","KEDJA MED KROK","LÄNGD 55 CM","VIT","6.16",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33 NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:55","ORD",3350,"Heminredning",,,15000
"N-4761350S","AMPELKEDJA","LÄNGD 80 CM","SVART","25.08",18,18,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33 NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:32","ORD",3350,"Heminredning",,,15000
"0012400","S-KROK","LÄNGD 5,5 CM","SVART","3.36",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33 NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:48","ORD",3350,"Heminredning",,,15000
,,,,,,,,,,,,,,,,,,,,,,,,,,,
)
sourceInfo := colName "`n" regInfo
; MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % colName
; MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % regInfo
; MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % sourceInfo
tmpFile1 := A_WorkingDir "\tmp_testfile.csv"
If !FileExist(tmpFile1) ; Create a new sourcefile if not exist
{ FileAppend %sourceInfo%, %tmpFile1%, UTF-8
If ErrorLevel
{ SplitPath tmpFile1, OutFileName, OutDir
MsgBox 16, Row %A_LineNumber% -> %A_ScriptName%,
( LTrim
Couldn't create the file!`n
File name .: %OutFileName%
Path .: %OutDir%
Is the file opened by another program?`n
This program will be stopped!
)
ExitApp
}
}
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/*
FileRead fileInfo, *t *P65001 %tmpFile1%
If ErrorLevel
{ SplitPath tmpFile1, OutFileName, OutDir
MsgBox 16, Row %A_LineNumber% -> %A_ScriptName%,
( LTrim
Unknown error! `t(ErrorLevel .: %ErrorLevel%)`n
Couldn't read or found the file.
File name . . .:`t%OutFileName%
Path .: %OutDir%`n
This program will be stopped!
)
ExitApp
}
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % fileInfo
*/
; Show each fields
FileEncoding UTF-8
Loop read, %tmpFile1%
{ lineNumber := A_Index
Loop parse, A_LoopReadLine, CSV
{ MsgBox 4, Row %A_LineNumber% -> %A_ScriptName%, Field %lineNumber%-%A_Index% is:`n%A_LoopField%`n`nContinue?
IfMsgBox, No
Return
}
}
Assume that the result array is called infoCol (or something else)
To reach a specific information I want to write infoCol(column1, header)
eg. an instruction like infoCol("J-213441", "exTxt1") gives the result 18 X 27 CM
or
colName := "exTxt2"
ProdName := "0012400"
infoCol(prodName, colName) gives the result SVART
information about the source
a)
Assume that the first column will not contain any duplicates.
But If duplicates exist only the column6 "antal" is added to the first product.
(If it's hard to solve - ignore my wish (it happens once or twice a year - I can handle it another way))
b)
the regInfo in the testfile have both empty rows and if the first column is empty the "row" is ignored.
c)
It is easy for the 0s to disappear at the beginning of 0012400 (the text is converted to a number)