I have recently used Inno Setup for my Java software. I am writing a function to check whether a printer driver exists by calling wmic printer get name /All
and reading it output. But the problem is when I am reading the text file and check if it contains a specific substring by Pos()
, it always returning 0, but when I tried to test with a character it returned the true value. I'm currently using version 5.6.1 Unicode.
I have looked at Delphi Pos always returning 0 but I think it's not my case:
Here is how I did it:
function isContainedInFile(File, Substring: String): Boolean;
var
Lines: TArrayOfString;
i: Integer;
line: String;
begin
Substring := Uppercase(Substring);
Result := False;
if LoadStringsFromFile(File, Lines) then
begin
for i:= 0 to GetArrayLength(Lines) - 1 do
begin
line := Lines[i];
if (Length(line) = 0) then
continue;
line := Uppercase(Trim(line));
Log('Substring:' + Substring + ', Line:' + line + ', Pos:' + IntToStr(Pos(Substring, line)));
if (Pos(Substring, line) <> 0) then
begin
Result:= True;
break;
end;
end;
end;
end;
This is how I called the isContainedInFile()
:
function IsBrotherDriverInstalled(): Boolean;
var
path, brotherPath, ListPrinterPath, ListPrinter: String;
check, index: Integer;
begin
ListPrinterPath := ExpandConstant('{tmp}\printerlist.tdm');
{ Save temporarily the list }
Exec(ExpandConstant('{cmd}'), '/c wmic printer get name /All > "' + ListPrinterPath + '"',
'', SW_HIDE, ewWaitUntilTerminated, check);
{ Check if the list has the printer }
Result := isContainedInFile(ListPrinterPath, PrinterName);
{ Delete the file }
DeleteFile(ListPrinterPath);
end;
Here is my output when the substring has length > 1:
And when the substring has length = 1:
Thanks in advance.