Delete Files From Log

From NSIS Wiki

Author: Namrok (talk, contrib)


Description

I wrote a function that will read the log file written out using "Dump log to file" and delete just those files that were written, as well as any empty directories left. Just push the location of the logfile onto the stack before you run it. It will leave it there, so you could always pop it off again.

Bo: Depending on which compression you use, this might not work, because for example the lzma compressor generates log files with file entries that do not have the trailing "...100%" that this script relies on.

Bo: This script needs "Get_parent_directory" renamed to un.GetParent to work.

Boris: This script is not compatible with ZipDLL_plug-in. To use it with zipdll make the following changes:

         ...
         StrCpy $R5 $R4 11
         ;MessageBox MB_OK "Filetest: $R5"
         StrCmp $R5 "  Extract :" ChangeFile ; Line specifies a file
         ...
ChangeFile:
         StrCpy $R2 $R4 1024 12 ; Get the new file
         StrLen $R5 $R2
         IntOp $R5 $R5 - "2"
         StrCpy $R2 $R2 $R5 ; trims off the the newline
         Delete "$R1\$R2" ; Try to delete the new file
         Goto NewLine
         ...

The Function

function un.DeleteFromLog
         ; R0 will be used as the log
         ; R1 will be used as the current directory
         ; R2 will be used as the current file
         ; R3 will be the file handle
         ; R4 will be the line
         ; R5 will be temp space
         exch $R0 ; gets the log
         push $R1 ; Backup $R1
         push $R2 ; Backup $R2
         push $R3 ; Backup $R3.  Stack is left in order $R0, $R1, $R2, etc
         push $R4
         push $R5
         FileOpen $R3 "$R0" "r"
NewLine:
         FileRead $R3 $R4
         ;MessageBox MB_OK "New Line: $R4"
         StrCpy $R5 $R4 15
         ;MessageBox MB_OK "Foldertest: $R5"
         StrCmp $R5 "Output folder: " ChangeDir ; Line specifies a directory
         StrCpy $R5 $R4 9
         ;MessageBox MB_OK "Filetest: $R5"
         StrCmp $R5 "Extract: " ChangeFile ; Line specifies a file
         StrCmp $R4 "" DoneAndDone ; Out of lines
         Goto NewLine ; If the line is none of the above, grab a new one.
 
ChangeDir:
         RMDir $R1 ; Try to remove the last directory
         StrCpy $R5 $R1
DeleteParents:
         ; Will attempt to delete parent directorys until it runs out
         ; not aggressive, just removes empty directories recurisvely
         ; before moving onto a new directory to delete files from
         Push $R5
         Call un.GetParent
         Pop $R5
         StrCmp $R5 "" +2
         RMDir $R5
         StrCmp $R5 "" 0 DeleteParents 
 
         StrCpy $R1 $R4 1024 15 ; Get the new directory
         StrLen $R5 $R1
         IntOp $R5 $R5 - "2"
         StrCpy $R1 $R1 $R5 ; trims off the newline at the end
         Goto NewLine
 
ChangeFile:
         StrCpy $R2 $R4 1024 9 ; Get the new file
         StrLen $R5 $R2
         IntOp $R5 $R5 - "10"
         StrCpy $R2 $R2 $R5 ; trims off the ...100% and the newline
         Delete "$R1\$R2" ; Try to delete the new file
         Goto NewLine
 
DoneAndDone:
         FileClose $R3
         Pop $R5 ; Restore the registers
         Pop $R4
         Pop $R3
         Pop $R2
         Pop $R1
         Exch $R0
functionend
Personal tools
donate