Last active
January 14, 2021 15:52
-
-
Save CarlTBarnes/67e8a4e53c12ec820d9d6ebbba6957a3 to your computer and use it in GitHub Desktop.
Remove Directory of Files using Directory() and API DeleteFile
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
RemoveDirectoryFiles(STRING FolderPath,STRING Specs2Purge,LONG ShowWindow=0,<*LONG OutCountRemoved>),LONG,PROC ! Returns Error Cnt | |
DB(STRING DBString) !OutputDebugString wrapper DebugOutput.clw https://gist.github.com/CarlTBarnes/d3c7a3779b094f0ed48186d04a9b45e6 | |
MODULE('Win32') | |
DeleteFileA(*cstring lpFileName),BOOL,RAW,PROC,PASCAL,DLL(1) | |
GetLastError(),LONG,PASCAL,DLL(1) | |
END | |
!Specs2Purge can be multiple using semicolon e.g. *.tmp;*.$$$;*.xxx | |
!===================================================== | |
RemoveDirectoryFiles PROCEDURE(STRING pFolder,STRING pSpecs,LONG ShowWindow,<*LONG OutCountRemoved>) ! ,LONG,PROC Returns Error Cnt | |
QNdx LONG,AUTO | |
X LONG,AUTO | |
DirQ QUEUE(FILE:Queue),PRE(DirQ) | |
END ! DirQ:Name DirQ:ShortName(8.3?) DirQ:Date DirQ:Time DirQ:Size DirQ:Attrib | |
DirQRecords LONG | |
FilesWindow WINDOW('Remove'),AT(,,343,150),FONT('Segoe UI',9,,FONT:regular),GRAY,CENTER | |
LIST,AT(1,1),USE(?ListFiles),FULL,VSCROLL, | | |
FORMAT('120L(1)|M~Name~@s255@52L(1)|M~Short Name~@s13@40R(1)|M~Date~C(0)@d1@40R(1)|M~Time~C(0)@T4@56R(1)|M~Size~C(0)@n13@16L(1)|M~Attr~@n3@') ,| | |
FROM(DirQ) !, #FIELDS(DirQ:name,DirQ:shortname,DirQ:date,DirQ:time,DirQ:size,DirQ:attrib) | |
END | |
CFileName CSTRING(261) | |
RetFailCnt LONG | |
RemovedCnt LONG | |
DbIt SHORT(1) !OutputDebug | |
PathClip &STRING | |
ConfirmDelete BOOL !If ShowWindow < 0 it will pause to Confirm. Mostly done with UsBackDoor | |
!-- MAP -- | |
!RemoveDirectoryFiles2(STRING pFolder, STRING pSpecs, LONG ShowWindow=0, <*LONG OutCountRemoved>),LONG,PROC | |
!DB PROCEDURE(STRING DBString) !OutputDebugString wrapper | |
! MODULE('Win32') | |
! DeleteFileA(*cstring lpFileName),BOOL,RAW,PROC,PASCAL,DLL(1) | |
! GetLastError(),LONG,PASCAL,DLL(1) | |
! END | |
CODE ! Begin processed code | |
DO RemoveRtn | |
IF ~OMITTED(OutCountRemoved) THEN OutCountRemoved = RemovedCnt. | |
RETURN(RetFailCnt) | |
RemoveRtn ROUTINE | |
IF DBit THEN DB('RemoveDirectoryFiles Path=' & pFolder &' Specs=' & pSpecs ) . | |
RetFailCnt = -1 !-1 = Invalid Parameter | |
FREE(DirQ) | |
X=LEN(CLIP(pFolder)) | |
IF X AND pFolder[x]='\' THEN X -= 1. !Strip trailing \ | |
IF X < 1 THEN EXIT. !Must pass Path as at least '.' | |
PathClip &= pFolder[1 : X] | |
IF ~pSpecs THEN EXIT. !Must pass Specs as at least '*' | |
RetFailCnt = 0 !Parms are good | |
LOOP !Parse multiple specs: *.PDF;*.$$$;*.WMF;*.TMP | |
X=INSTRING(';',pSpecs,1) | |
IF ~X THEN X = LEN(CLIP(pSpecs))+1. | |
IF DBit THEN DB('Dir ' & PathClip & '\' & SUB(pSpecs,1,X-1) ) . | |
IF SUB(pSpecs,1,X-1) THEN | |
DIRECTORY(DirQ,PathClip & '\' & SUB(pSpecs,1,X-1),ff_:NORMAL) | |
END | |
pSpecs=LEFT(SUB(pSpecs,X+1,999)) | |
WHILE pSpecs | |
DirQRecords = RECORDS(DirQ) | |
IF ~DirQRecords THEN EXIT. | |
IF ShowWindow < 0 THEN !Negative means CONFIRM Message | |
ShowWindow = -1 * ShowWindow | |
ConfirmDelete = 1 | |
END | |
!Can pass ShowWindow as 20 and Window only shows if >= 20 files | |
IF (ShowWindow AND DirQRecords >= ShowWindow) or DirQRecords > 99 THEN | |
OPEN(FilesWindow) | |
0{PROP:Text} = 'Remove ' & DirQRecords &' Files: ' & pFolder | |
DISPLAY | |
IF ConfirmDelete THEN | |
CASE MESSAGE('Delete listed files?', | | |
'Confirm Delete', ICON:Cut, BUTTON:Yes+BUTTON:No) | |
OF BUTTON:No ; EXIT | |
END !CASE | |
END | |
END | |
LOOP QNdx = 1 TO DirQRecords | |
GET(DirQ,QNdx) | |
IF BAND(DirQ:Attrib, ff_:DIRECTORY) THEN CYCLE. | |
!11/12/19 REMOVE(PathClip & '\' & DirQ:Name, REMOVE:FILESONLY) | |
!11/12/19 IF ERRORCODE() > 3 THEN RetFailCnt += 1. | |
CFileName=CLIP(PathClip & '\' & DirQ:Name) | |
IF ~DeleteFileA(CFileName) THEN !RTL Remove() is 100x slower | |
IF GetLastError() > 3 THEN RetFailCnt += 1. | |
DB('DeleteFileA error '& GetLastError() &' '& CFileName) | |
ELSE | |
RemovedCnt += 1 | |
END | |
END !LOOP | |
EXIT | |
DB PROCEDURE(STRING DBString) | |
!OutputDebugString wrapper DebugOutput.clw https://gist.github.com/CarlTBarnes/d3c7a3779b094f0ed48186d04a9b45e6 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment