Created
January 9, 2022 21:36
-
-
Save jarroddavis68/b77051d7b58d861564816d45bf8ec7de to your computer and use it in GitHub Desktop.
Defer Delete File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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