Skip to content

Instantly share code, notes, and snippets.

@frostney
Created September 10, 2011 18:11
Show Gist options
  • Save frostney/1208591 to your computer and use it in GitHub Desktop.
Save frostney/1208591 to your computer and use it in GitHub Desktop.
Converts the output of the command line to a string list without the use of pipes or processes. This should work on all unixoid systems.
program cmdtest;
uses
Unix,
Classes,
SysUtils;
function CmdToString(Command: AnsiString): TStringList;
var
formattedDateTime: AnsiString = '';
Filename: AnsiString = '';
tmpStringList: TStringList;
begin
// Format date time string & construct filename
DateTimeToString(formattedDateTime, 'yyyy-mm-dd_hh-nn-ss-z', Now);
FileName := GetTempDir(true) + formattedDateTime + '.txt';
// Create temporary file in temporary folder with timestamp as filename
fpSystem(Command + ' > ' + FileName);
// Create string list
tmpStringList := TStringList.Create;
// Load file into temporary string list
tmpStringList.LoadFromFile(Filename);
// Delete file
DeleteFile(Filename);
// Return temporary string list
Result := tmpStringList;
end;
var
myStringList: TStringList;
i: Integer;
begin
// sw_vers returns the Mac OS X version, doesn't work on Linux of course ;)
myStringList := CmdToString('sw_vers');
for i := 0 to myStringList.Count - 1 do
begin
WriteLn(myStringList[i]);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment