Skip to content

Instantly share code, notes, and snippets.

@jarroddavis68
Created January 9, 2022 21:36
Show Gist options
  • Save jarroddavis68/b77051d7b58d861564816d45bf8ec7de to your computer and use it in GitHub Desktop.
Save jarroddavis68/b77051d7b58d861564816d45bf8ec7de to your computer and use it in GitHub Desktop.
Defer Delete File
procedure DeferDelFile(const aFilename: string);
var
LCode: TStringList;
LFilename: string;
procedure C(const aMsg: string; const aArgs: array of const);
var
LLine: string;
begin
LLine := Format(aMsg, aArgs);
LCode.Add(LLine);
end;
begin
if aFilename.IsEmpty then Exit;
LFilename := ChangeFileExt(aFilename, '');
LFilename := LFilename + '_DeferDelFile.bat';
LCode := TStringList.Create;
try
C('@echo off', []);
C(':Repeat', []);
C('del "%s"', [aFilename]);
C('if exist "%s" goto Repeat', [aFilename]);
C('del "%s"', [LFilename]);
LCode.SaveToFile(LFilename);
finally
FreeAndNil(LCode);
end;
if FileExists(LFilename) then
begin
ShellExecute(0, 'open', PChar(LFilename), nil, nil, SW_HIDE);
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment