OgreVorbis

MD5 Appender

Mar 11th, 2026
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. EnableExplicit
  2.  
  3. OpenConsole("MD5 Appender")
  4.  
  5. ;---------------------------------------------------
  6. ; Utility: Make the date and time in correct format
  7. ;---------------------------------------------------
  8. Procedure.s BuildTimestamp()
  9.   Define d = Date()
  10.   Define year  = Year(d)
  11.   Define month = Month(d)
  12.   Define day   = Day(d)
  13.   Define hour  = Hour(d)
  14.   Define min   = Minute(d)
  15.   Define sec   = Second(d)
  16.  
  17.   Define ampm$
  18.  
  19.   If hour >= 12 : ampm$ = "PM" : Else : ampm$ = "AM" : EndIf
  20.   If hour = 0 : hour = 12 : ElseIf hour > 12 : hour - 12 : EndIf
  21.  
  22.   ProcedureReturn Str(month) + "/" + Str(day) + "/" + Str(year) + " " + Str(hour) + ":" + RSet(Str(min),2,"0") + ":" + RSet(Str(sec),2,"0") + " " + ampm$
  23. EndProcedure
  24.  
  25. ;-------------------------
  26. ; Utility: Strip quotes
  27. ;-------------------------
  28. Procedure.s StripQuotes(path$)
  29.   If Left(path$,1) = Chr(34)
  30.     path$ = Mid(path$,2)
  31.   EndIf
  32.  
  33.   If Right(path$,1) = Chr(34)
  34.     path$ = Left(path$,Len(path$)-1)
  35.   EndIf
  36.  
  37.   ProcedureReturn path$
  38. EndProcedure
  39.  
  40. ;--------------------------------
  41. ; Utility: Ensure trailing slash
  42. ;--------------------------------
  43. Procedure.s EnsureSlash(path$)
  44.   path$ = Trim(path$)
  45.   If Right(path$,1) <> "\" And Right(path$,1) <> "/"
  46.     path$ + "\"
  47.   EndIf
  48.   ProcedureReturn path$
  49. EndProcedure
  50.  
  51. ;------------------------------------------
  52. ; Utility: Close the app nicely with a msg
  53. ;------------------------------------------
  54. Procedure NiceEnding()
  55.     PrintN("")
  56.     PrintN("Done.")
  57.     PrintN("Press ENTER to exit. . .")
  58.     Input()
  59.     End
  60. EndProcedure
  61.  
  62. ;-------------------------
  63. ; Prompt user
  64. ;-------------------------
  65. PrintN("Drag SOURCE directory here:")
  66. Define sourceDir$ = StripQuotes(Input())
  67. sourceDir$ = EnsureSlash(sourceDir$)
  68.  
  69. PrintN("")
  70. PrintN("Drag DESTINATION directory here:")
  71. Define destDir$ = StripQuotes(Input())
  72. destDir$ = EnsureSlash(destDir$)
  73.  
  74. PrintN("")
  75.  
  76. If FileSize(sourceDir$) <> -2
  77.   PrintN("Invalid source directory.")
  78.   NiceEnding()
  79. EndIf
  80.  
  81. If FileSize(destDir$) <> -2
  82.   PrintN("Invalid destination directory.")
  83.   NiceEnding()
  84. EndIf
  85.  
  86. ;-------------------------
  87. ; Log file
  88. ;-------------------------
  89. Define logFile$ = destDir$ + "MainOgre.md5"
  90.  
  91. NewList tempLines.s()
  92. Define logExists = ReadFile(0, logFile$)
  93.  
  94. If logExists
  95.   While Eof(0) = 0
  96.     AddElement(tempLines())
  97.     tempLines() = ReadString(0)
  98.   Wend
  99.   CloseFile(0)
  100. EndIf
  101.  
  102. ; Remove trailing blank line if it exists
  103. ;If LastElement(tempLines())
  104. ;  If Trim(tempLines()) = ""
  105. ;    DeleteElement(tempLines())
  106. ;  EndIf
  107. ;EndIf
  108.  
  109. ; Ensure at least two lines
  110. While ListSize(tempLines()) < 2
  111.   AddElement(tempLines())
  112.   tempLines() = ""
  113. Wend
  114.  
  115. ;-------------------------
  116. ; Scan source directory
  117. ;-------------------------
  118. UseMD5Fingerprint()
  119.  
  120. Define dir = ExamineDirectory(0, sourceDir$, "*.*")
  121.  
  122. If dir = 0
  123.   PrintN("Failed to read source directory.")
  124.   NiceEnding()
  125. EndIf
  126.  
  127. Define file$
  128. Define srcFile$
  129. Define dstFile$
  130. Define md5$
  131.  
  132. While NextDirectoryEntry(0)
  133.  
  134.   If DirectoryEntryType(0) = #PB_DirectoryEntry_File
  135.    
  136.     file$ = DirectoryEntryName(0)
  137.    
  138.     srcFile$ = sourceDir$ + file$
  139.     dstFile$ = destDir$ + file$
  140.    
  141.     ; File not present in destination
  142.     If FileSize(dstFile$) = -1
  143.      
  144.       PrintN("Processing: " + file$)
  145.      
  146.       md5$ = FileFingerprint(srcFile$, #PB_Cipher_MD5)
  147.      
  148.       ; Copy file
  149.       CopyFile(srcFile$, dstFile$)
  150.      
  151.       ; Append to log
  152.       AddElement(tempLines())
  153.       tempLines() = md5$ + " *" + file$
  154.      
  155.     EndIf
  156.    
  157.   EndIf
  158.  
  159. Wend
  160.  
  161. FinishDirectory(0)
  162.  
  163. ;-------------------------
  164. ; Update timestamp line
  165. ;-------------------------
  166. Define timestamp$ = BuildTimestamp()
  167.  
  168. SelectElement(tempLines(),1)
  169. tempLines() = "# Generated " + timestamp$
  170.  
  171. ;-------------------------
  172. ; Write log file
  173. ;-------------------------
  174. If CreateFile(0, logFile$)
  175.  
  176.   ForEach tempLines()
  177.     WriteStringN(0, tempLines())
  178.   Next
  179.  
  180.   CloseFile(0)
  181.  
  182. EndIf
  183.  
  184. NiceEnding()
Advertisement
Add Comment
Please, Sign In to add comment