Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EnableExplicit
- OpenConsole("MD5 Appender")
- ;---------------------------------------------------
- ; Utility: Make the date and time in correct format
- ;---------------------------------------------------
- Procedure.s BuildTimestamp()
- Define d = Date()
- Define year = Year(d)
- Define month = Month(d)
- Define day = Day(d)
- Define hour = Hour(d)
- Define min = Minute(d)
- Define sec = Second(d)
- Define ampm$
- If hour >= 12 : ampm$ = "PM" : Else : ampm$ = "AM" : EndIf
- If hour = 0 : hour = 12 : ElseIf hour > 12 : hour - 12 : EndIf
- ProcedureReturn Str(month) + "/" + Str(day) + "/" + Str(year) + " " + Str(hour) + ":" + RSet(Str(min),2,"0") + ":" + RSet(Str(sec),2,"0") + " " + ampm$
- EndProcedure
- ;-------------------------
- ; Utility: Strip quotes
- ;-------------------------
- Procedure.s StripQuotes(path$)
- If Left(path$,1) = Chr(34)
- path$ = Mid(path$,2)
- EndIf
- If Right(path$,1) = Chr(34)
- path$ = Left(path$,Len(path$)-1)
- EndIf
- ProcedureReturn path$
- EndProcedure
- ;--------------------------------
- ; Utility: Ensure trailing slash
- ;--------------------------------
- Procedure.s EnsureSlash(path$)
- path$ = Trim(path$)
- If Right(path$,1) <> "\" And Right(path$,1) <> "/"
- path$ + "\"
- EndIf
- ProcedureReturn path$
- EndProcedure
- ;------------------------------------------
- ; Utility: Close the app nicely with a msg
- ;------------------------------------------
- Procedure NiceEnding()
- PrintN("")
- PrintN("Done.")
- PrintN("Press ENTER to exit. . .")
- Input()
- End
- EndProcedure
- ;-------------------------
- ; Prompt user
- ;-------------------------
- PrintN("Drag SOURCE directory here:")
- Define sourceDir$ = StripQuotes(Input())
- sourceDir$ = EnsureSlash(sourceDir$)
- PrintN("")
- PrintN("Drag DESTINATION directory here:")
- Define destDir$ = StripQuotes(Input())
- destDir$ = EnsureSlash(destDir$)
- PrintN("")
- If FileSize(sourceDir$) <> -2
- PrintN("Invalid source directory.")
- NiceEnding()
- EndIf
- If FileSize(destDir$) <> -2
- PrintN("Invalid destination directory.")
- NiceEnding()
- EndIf
- ;-------------------------
- ; Log file
- ;-------------------------
- Define logFile$ = destDir$ + "MainOgre.md5"
- NewList tempLines.s()
- Define logExists = ReadFile(0, logFile$)
- If logExists
- While Eof(0) = 0
- AddElement(tempLines())
- tempLines() = ReadString(0)
- Wend
- CloseFile(0)
- EndIf
- ; Remove trailing blank line if it exists
- ;If LastElement(tempLines())
- ; If Trim(tempLines()) = ""
- ; DeleteElement(tempLines())
- ; EndIf
- ;EndIf
- ; Ensure at least two lines
- While ListSize(tempLines()) < 2
- AddElement(tempLines())
- tempLines() = ""
- Wend
- ;-------------------------
- ; Scan source directory
- ;-------------------------
- UseMD5Fingerprint()
- Define dir = ExamineDirectory(0, sourceDir$, "*.*")
- If dir = 0
- PrintN("Failed to read source directory.")
- NiceEnding()
- EndIf
- Define file$
- Define srcFile$
- Define dstFile$
- Define md5$
- While NextDirectoryEntry(0)
- If DirectoryEntryType(0) = #PB_DirectoryEntry_File
- file$ = DirectoryEntryName(0)
- srcFile$ = sourceDir$ + file$
- dstFile$ = destDir$ + file$
- ; File not present in destination
- If FileSize(dstFile$) = -1
- PrintN("Processing: " + file$)
- md5$ = FileFingerprint(srcFile$, #PB_Cipher_MD5)
- ; Copy file
- CopyFile(srcFile$, dstFile$)
- ; Append to log
- AddElement(tempLines())
- tempLines() = md5$ + " *" + file$
- EndIf
- EndIf
- Wend
- FinishDirectory(0)
- ;-------------------------
- ; Update timestamp line
- ;-------------------------
- Define timestamp$ = BuildTimestamp()
- SelectElement(tempLines(),1)
- tempLines() = "# Generated " + timestamp$
- ;-------------------------
- ; Write log file
- ;-------------------------
- If CreateFile(0, logFile$)
- ForEach tempLines()
- WriteStringN(0, tempLines())
- Next
- CloseFile(0)
- EndIf
- NiceEnding()
Advertisement
Add Comment
Please, Sign In to add comment