QlikTip #11: Einlesen von Ini-Dateien

QlikView bietet eine Reihe von Funktionalitäten an, wie man xml-, Excel-, csv-,txt-Dateien, usw. strukturiert einlesen kann.
Dieser Tip zeigt, wie man auch .ini-Dateien sinnvoll auslesen kann.
“Typische” Ini-Dateien haben folgende Struktur (siehe auch http://en.wikipedia.org/wiki/INI_file):

[Section1]
Name1=Value1
Name2=ValueXX
Name3=ValueYY
[Section2]
Name4=Value3

Nachfolgender Code ladet eine beliebige Ini-Datei und bringt diese in strukturierte Form:

// **************************************************************
// this assumes that the ini-file defined below is located in the
// same folder as the qvw-file.
// **************************************************************
SET vIniFilePath = 'Test4.ini';

// **************************************************************
// Create a temporary table
// Note when reading the data-transformation:
// You have to read the load-statements bottom-up !!!
// **************************************************************
Temp_Ini:
Load
	// get the name
	if(_FlagSection = 0,left(FullField, index(FullField,'=')-1)) as Name,

	// get the value
	if(_FlagSection = 0,mid(FullField, index(FullField,'=')+1)) as Value,
	*
;
Load
	// just again a load to fill up the blank rows with the corresponding section
	if(_FlagSection = 0, text(peek('Section',RecNo()-2)),Section) as Section,
	FullField,
	Id,
	_FlagSection
	;
Load
	// if we are dealing with a section (see below) we cut the string inside
	// the '[' and ']' characters
	if (left(FullField,1) = '[' and right(FullField,1) = ']',purgechar((FullField),'[]')) as Section,

	// if we have a '[' character at the beginning and a ']' character at the end of
	// the string we are dealing with a section, so flag the record as section
	if (left(FullField,1) = '[' and right(FullField,1) = ']',1,0) as _FlagSection,
	*;
LOAD
	RecNo() as Id,
	@1 as FullField
FROM $(vIniFilePath)
(txt, codepage is 1252, no labels, delimiter is ',', msq);

// **************************************************************
// Load the values again to 'drop' the rows with sections in it.
// **************************************************************
Ini:
LOAD
	Id as LineNo,
	Section,
	Name,
	Value,
	FullField as LineString // just for testing and review,
RESIDENT Temp_Ini
WHERE (_FlagSection = 0)

	// prevent comments
	and (left(Value,1) <> ';')

	// prevent comments
	and (left(Name,1) <> ';')

	// prevent selecting empty-lines
	and ((len(Name) > 0) and (len(Value) > 0))
Order BY Id
;

//drop the temporary table which is not used anymore
Drop TABLE Temp_Ini;

Damit lassen sich die Daten in strukturierter Form an der Oberfläche anzeigen:

Anzeige der Daten der Ini-Datei

Anzeige der Daten der Ini-Datei

Verbesserungen:

Dinge, die ich in meinem Script nicht beachtet habe:

  • Escape Character
  • Kommentare am Ende einer Zeile
  • usw. :)

Downloads

QlikView-Applikation mit Beispiel-Dateien (.zip, 1.2 MB)

Bookmark and Share

and tagged , , , , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

One Comment

  1. Helmut Dollinger
    Posted October 17, 2009 at 12:18 | Permalink

    … das habe ich gesucht, danke für die detailliere Beschreibung und die Lösung meines Problems :)

    Gruss
    H. Dollinger