MoveFileFolder.nsh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. ; MoveFile and MoveFolder macros
  3. ;
  4. ; Author: theblazingangel@aol.com (for the AutoPatcher project - www.autopatcher.com)
  5. ; Created: June 2007
  6. ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  7. ;==================
  8. ; MoveFile macro
  9. ;==================
  10. !macro MoveFile sourceFile destinationFile
  11. !define MOVEFILE_JUMP ${__LINE__}
  12. ; Check source actually exists
  13. IfFileExists "${sourceFile}" +3 0
  14. SetErrors
  15. goto done_${MOVEFILE_JUMP}
  16. ; Add message to details-view/install-log
  17. DetailPrint "Moving/renaming file: ${sourceFile} to ${destinationFile}"
  18. ; If destination does not already exists simply move file
  19. IfFileExists "${destinationFile}" +3 0
  20. rename "${sourceFile}" "${destinationFile}"
  21. goto done_${MOVEFILE_JUMP}
  22. ; If overwriting without 'ifnewer' check
  23. ${If} $switch_overwrite == 1
  24. delete "${destinationFile}"
  25. rename "${sourceFile}" "${destinationFile}"
  26. delete "${sourceFile}"
  27. goto done_${MOVEFILE_JUMP}
  28. ${EndIf}
  29. ; If destination already exists
  30. Push $R0
  31. Push $R1
  32. Push $R2
  33. push $R3
  34. GetFileTime "${sourceFile}" $R0 $R1
  35. GetFileTime "${destinationFile}" $R2 $R3
  36. IntCmp $R0 $R2 0 older_${MOVEFILE_JUMP} newer_${MOVEFILE_JUMP}
  37. IntCmp $R1 $R3 older_${MOVEFILE_JUMP} older_${MOVEFILE_JUMP} newer_${MOVEFILE_JUMP}
  38. older_${MOVEFILE_JUMP}:
  39. delete "${sourceFile}"
  40. goto time_check_done_${MOVEFILE_JUMP}
  41. newer_${MOVEFILE_JUMP}:
  42. delete "${destinationFile}"
  43. rename "${sourceFile}" "${destinationFile}"
  44. delete "${sourceFile}" ;incase above failed!
  45. time_check_done_${MOVEFILE_JUMP}:
  46. Pop $R3
  47. Pop $R2
  48. Pop $R1
  49. Pop $R0
  50. done_${MOVEFILE_JUMP}:
  51. !undef MOVEFILE_JUMP
  52. !macroend
  53. ;==================
  54. ; MoveFolder macro
  55. ;==================
  56. !macro MoveFolder source destination mask
  57. !define MOVEFOLDER_JUMP ${__LINE__}
  58. Push $R0
  59. Push $R1
  60. ; Move path parameters into registers so they can be altered if necessary
  61. StrCpy $R0 "${source}"
  62. StrCpy $R1 "${destination}"
  63. ; Sort out paths - remove final backslash if supplied
  64. Push $0
  65. ; Source
  66. StrCpy $0 "$R0" 1 -1
  67. StrCmp $0 '\' 0 +2
  68. StrCpy $R0 "$R0" -1
  69. ; Destination
  70. StrCpy $0 "$R1" 1 -1
  71. StrCmp $0 '\' 0 +2
  72. StrCpy $R1 "$R1" -1
  73. Pop $0
  74. ; Create destination dir
  75. CreateDirectory "$R1\"
  76. ; Add message to details-view/install-log
  77. DetailPrint "Moving files: $R0\${mask} to $R1\"
  78. ; Push registers used by ${Locate} onto stack
  79. Push $R6
  80. Push $R7
  81. Push $R8
  82. Push $R9
  83. ; Duplicate dir structure (to preserve empty folders and such)
  84. ${Locate} "$R0" "/L=D" ".MoveFolder_Locate_createDir"
  85. ; Locate files and move (via callback function)
  86. ${Locate} "$R0" "/L=F /M=${mask} /S= /G=1" ".MoveFolder_Locate_moveFile"
  87. ; Delete subfolders left over after move
  88. Push $R2
  89. deldir_loop_${MOVEFOLDER_JUMP}:
  90. StrCpy $R2 0
  91. ${Locate} "$R0" "/L=DE" ".MoveFolder_Locate_deleteDir"
  92. StrCmp $R2 0 0 deldir_loop_${MOVEFOLDER_JUMP}
  93. Pop $R2
  94. ; Delete empty subfolders moved - say the user just wanted to move *.apm files, they now also have a load of empty dir's from dir structure duplication!
  95. Push $R2
  96. delnewdir_loop_${MOVEFOLDER_JUMP}:
  97. StrCpy $R2 0
  98. ${Locate} "$R1" "/L=DE" ".MoveFolder_Locate_deleteDir"
  99. StrCmp $R2 0 0 delnewdir_loop_${MOVEFOLDER_JUMP}
  100. Pop $R2
  101. ; Pop registers used by ${Locate} off the stack again
  102. Pop $R9
  103. Pop $R8
  104. Pop $R7
  105. Pop $R6
  106. ; Delete source folder if empty
  107. rmdir "$R0"
  108. Pop $R1
  109. Pop $R0
  110. !undef MOVEFOLDER_JUMP
  111. !macroend
  112. ;==================
  113. ; MoveFolder macro's ${Locate} callback functions
  114. ;==================
  115. Function .MoveFolder_Locate_createDir
  116. ${If} $R6 == ""
  117. Push $R2
  118. StrLen $R2 "$R0"
  119. StrCpy $R2 $R9 '' $R2
  120. CreateDirectory "$R1$R2"
  121. Pop $R2
  122. ${EndIf}
  123. Push $R1
  124. FunctionEnd
  125. Function .MoveFolder_Locate_moveFile
  126. Push $R2
  127. ; Get path to file
  128. StrLen $R2 "$R0"
  129. StrCpy $R2 $R9 '' $R2
  130. StrCpy $R2 "$R1$R2"
  131. ; If destination does not already exists simply move file
  132. IfFileExists "$R2" +3 0
  133. rename "$R9" "$R2"
  134. goto done
  135. ; If overwriting without 'ifnewer' check
  136. ${If} $switch_overwrite == 1
  137. delete "$R2"
  138. rename "$R9" "$R2"
  139. delete "$R9"
  140. goto done
  141. ${EndIf}
  142. ; If destination already exists
  143. Push $0
  144. Push $1
  145. Push $2
  146. push $3
  147. GetFileTime "$R9" $0 $1
  148. GetFileTime "$R2" $2 $3
  149. IntCmp $0 $2 0 older newer
  150. IntCmp $1 $3 older older newer
  151. older:
  152. delete "$R9"
  153. goto time_check_done
  154. newer:
  155. delete "$R2"
  156. rename "$R9" "$R2"
  157. delete "$R9" ;incase above failed!
  158. time_check_done:
  159. Pop $3
  160. Pop $2
  161. Pop $1
  162. Pop $0
  163. done:
  164. Pop $R2
  165. Push $R1
  166. FunctionEnd
  167. Function .MoveFolder_Locate_deleteDir
  168. ${If} $R6 == ""
  169. RMDir $R9
  170. IntOp $R2 $R2 + 1
  171. ${EndIf}
  172. Push $R1
  173. FunctionEnd