I am trying to make an application to test certain functionality that I intend to add later to my main program, but without the risk of breaking my main program.
I'm new to Pascal, and I still don't understand how many things work.
The objective of this program is, through a PowerShell command executed in the same application, to obtain several JSON, each one from a specific directory to go through them and check certain things.
These things can be the presence of specific file extensions in certain directories, or the presence of a large volume of information in them.
Create 3 objects in Delphi for this application. A button to start the program, a Timer and a JvCreateProcess.
The interval property of the timer is 10 (ms). The onTimer
property is the Timer1Timer
method.
Timer1Timer
code:
procedure TUProbando.Timer1Timer(Sender: TObject);
var
Comando: TStrings;
ValorJSON: TStrings;
begin
if ContadorDirectorios = Length(rutas) then
begin
Timer1.Enabled := false;
exit;
end;
Timer1.Enabled := false;
Lista.Clear;
// spacer to improve visibilitys
memo_Salida_Command_gci.Lines.Add('******* ' + rutas[ContadorDirectorios] +
' *******');
Comando := TStringList.Create;
try
// Open Powershell
Comando.Add('C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe "');
// I add the command line to create a JSON with the contents of the specified directory
Comando.Add('"gci ' + rutas[ContadorDirectorios] +
' -Recurse | ConvertTo-Json"');
// I add the command line to the process
JvCreateProcess1.CommandLine := Comando.Text;
// I run the process
JvCreateProcess1.run;
ValorJSON := JvCreateProcess1.ConsoleOutput;
finally
begin
Comando.Free;
end;
end;
end;
The rutas[]
array is a String array containing the directories to check.
rutas: array [0 .. 2] of String = ('C:\Users\operario43\Desktop',
'C:\Users\operario43\Documents', 'C:\Users\operario43\Pictures');
Now, JvCreateProcess1
code:
procedure TUProbando.JvCreateProcess1Terminate(Sender: TObject;
ExitCode: Cardinal);
begin
// Calculate the size of the current directory
// I WANT GET JSON OF PREVIOUS POWER SHELL COMMAND HERE
SizeDirectory := GetSizeFromJson('JSON HERE', Extensiones);
if checkSizeDirectory(SizeDirectory, SizeControlDirectory) then
begin
ShowMessage('This Directory' + rutas[ContadorDirectorios] +
' has important files not protected by a backup');
end;
// +1 counter of directories traversed
inc(ContadorDirectorios);
end;
Other methods that I use:
function GetSizeFromJson(JSON: String; vExtensiones: array of string): integer;
var
Contenedor: TJSONArray;
Jfichero: TJSONObject;
JNombre: TJSONValue;
// value of the 'length' element of each element of the JSON
JSizeFile: TJSONNumber; //
I: integer;
SizeDirectory: Int64;
begin
SizeDirectory := 0;
result := -1;
try
Contenedor := TJSONObject.ParseJSONValue(JSON) as TJSONArray;
if Contenedor = nil then
begin
ShowMessage('Error al parsear el json' + JSON);
exit;
end;
for I := 0 to Contenedor.Count - 1 do
begin
Jfichero := Contenedor.Items[I] as TJSONObject;
if Jfichero <> nil then
begin
// I extract the name of the element
JNombre := Jfichero.GetValue('Name');
// If the extensions of the files in the directory are in the 'Extensions' array
if MatchStr(ExtractFileExt(JNombre.Value), vExtensiones) then
begin
// get the value of the lenght element in bytes
JSizeFile := Jfichero.GetValue('Length') as TJSONNumber;
if JSizeFile <> nil then
begin
// I add the value of 'leght' of the element to the variable SizeDirectory
inc(SizeDirectory, JSizeFile.AsInt);
end; // if JSizeFile <> nil
end; // if MatchStr(ExtractFileExt(JNombre.Value), vExtensiones)
end; // if Jfichero <> nil
end; // for I := 0 to Contenedor.Count - 1
// I return the value of the size of the directory
result := SizeDirectory;
except
on E: Exception do
ShowMessage(E.Message);
end;
end;
// method to find out if the size of the current directory exceeds the recommended size for directories in the variable called SizeControlDirectory
function checkSizeDirectory(vSizeDirectory, vSizeControlDirectory
: Int64): Boolean;
var
Contenedor: TJSONArray;
begin
result := false;
vSizeDirectory := GetSizeFromJson(Contenedor.ToString, Extensiones);
if vSizeDirectory > vSizeControlDirectory then
begin
ShowMessage('Directory ' + rutas[ContadorDirectorios] +
' have files don't protected by Backup');
end;
end;
My question is, how do I get JSON for JvCreateProcess1Terminate method?.
I've made a lot of changes trying to get it to work, and made small pieces of code to make it more manageable.