I am fairly new with Pure Basic. I've just written a packer program that uses the packer library. I'm sure there are a dozen of these, so what is one more? Mine lets you choose multiple folders and a couple of other options.
Is General Discussion the best place to share programs?
I'm trying to make a videogame with it. So much to learn!

(Now updated to v1.1)
Code: Select all
EnableExplicit
; Initialize variables
Global ConfigFile.s = GetPathPart(ProgramFilename()) + "Multi-Folder_Packer_Config.TXT"
Global SourcePath.s, DestPath.s
Global SourceCount
Global SelectedSource.s, SelectedDest.s, PackFileName.s, PackFilePath.s
Global DetailsFile.s
Global PackID, DetailsID
Global PackerMethod.b, FolderHierarchy.b
Global Folder.s
Global PathName.s
Global Key.s
Global Dim FolderArray.s(1000)
Procedure EndProgram()
; Update Config.TXT with the latest source and destination paths
If CreateFile(0, ConfigFile)
; Write SelectedSource if it’s set, otherwise use SourcePath or "0"
If SelectedSource <> ""
WriteStringN(0, SelectedSource)
ElseIf SourcePath <> ""
WriteStringN(0, SourcePath)
Else
WriteStringN(0, "0")
EndIf
; Write SelectedDest if it’s set, otherwise use DestPath or "0"
If SelectedDest <> ""
WriteStringN(0, SelectedDest)
ElseIf DestPath <> ""
WriteStringN(0, DestPath)
Else
WriteStringN(0, "0")
EndIf
CloseFile(0)
Else
ConsoleColor(12, 0)
PrintN("Warning: Could not update Multi-Folder_Packer_Config.TXT.")
EndIf
ConsoleColor(8, 0)
PrintN("")
PrintN("Press RETURN to exit...")
Input()
End
EndProcedure
Procedure YesNo()
Repeat
Key = Inkey(); wait for key press
If LCase(Key) = "y" Or LCase(Key) = "n"
Break
EndIf
Delay(7)
ForEver
Select LCase(Key)
Case "y"
ConsoleColor(11, 0)
PrintN("Yes")
Case "n"
ConsoleColor(11, 0)
PrintN("No")
EndSelect
EndProcedure
Procedure Menu12()
Repeat
Key = Inkey(); wait for key press
If LCase(Key) = "1" Or LCase(Key) = "2"
Break
EndIf
Delay(7)
ForEver
EndProcedure
Procedure GetFolderName()
; Remove trailing backslash if it exists
If Right(PathName, 1) = "\"
PathName = Left(PathName, Len(PathName) - 1)
EndIf
; Extract the last folder name
Folder = GetFilePart(PathName)
EndProcedure
; Open the console for user interaction
OpenConsole()
; Check if Config.TXT exists, create if not
If FileSize(ConfigFile) = -1
If CreateFile(0, ConfigFile)
WriteStringN(0, "0")
WriteStringN(0, "0")
CloseFile(0)
Else
ConsoleColor(12, 0)
PrintN("Error: Could not create Multi-Folder_Packer_Config.TXT.")
EndProgram()
EndIf
EndIf
; Read Config.TXT
If ReadFile(0, configFile)
SourcePath = ReadString(0)
DestPath = ReadString(0)
CloseFile(0)
Else
ConsoleColor(12, 0)
PrintN("Error: Could not read Multi-Folder_Packer_Config.TXT.")
EndProgram()
EndIf
; Validate source and destination paths
If SourcePath <> "0" And FileSize(SourcePath) <> -2
SourcePath = "0"
EndIf
If DestPath <> "0" And FileSize(DestPath) <> -2
DestPath = "0"
EndIf
SourceCount = -1
Define LoopQuery.s
Define InitialSource.s = ""
If SourcePath <> "0" ; Set initial value from Multi-Folder_Packer_Config.TXT once
InitialSource = SourcePath
EndIf
ConsoleColor(14, 0)
PrintN("Multi-Folder Packer v1.1 by Carm3D")
PrintN("")
ConsoleColor(8, 0)
PrintN("This little program will let you select multiple")
PrintN("folders (1000 limit), choose a compression option,")
PrintN("and pack the Data into a single file for your Pure")
PrintN("Basic projects. Enjoy!")
PrintN("")
Repeat
; Prompt user to choose source folder
ConsoleColor(14, 0)
PrintN("")
If SourceCount = -1
PrintN("Add folder to pack?(y/n)")
Else
PrintN("Add another folder to pack?(y/n)")
EndIf
YesNo()
If LCase(Key) = "n"
Break
EndIf
SelectedSource = PathRequester("Select Source Folder", InitialSource)
If SelectedSource = ""
ConsoleColor(12, 0)
PrintN("No folder selected.")
Else
SourceCount = SourceCount + 1
FolderArray(SourceCount) = SelectedSource
PathName = SelectedSource
GetFolderName()
ConsoleColor(11, 0)
PrintN(Folder + " added.")
InitialSource = SelectedSource
EndIf
ForEver
If SourceCount = -1
ConsoleColor(12, 0)
PrintN("No folders selected. Exiting.")
EndProgram()
EndIf
; Choose destination folder
Define InitialDest.s = ""
If DestPath <> "0"
InitialDest.s = DestPath
EndIf
Repeat
Define TryAgain.s
ConsoleColor(14, 0)
PrintN("")
PrintN("Select the destination folder for the packed file:")
SelectedDest = PathRequester("Select Destination Folder", InitialDest)
If SelectedDest = ""
ConsoleColor(12, 0)
PrintN("No destination folder selected. Try again?(y/n)")
YesNo()
If LCase(Key) = "n"
EndProgram()
EndIf
Else
ConsoleColor(11, 0)
PrintN(SelectedDest)
PrintN("chosen for Destination folder.")
Break
EndIf
ForEver
; Choose Packed File Name
Repeat
Define TryAgain.s
ConsoleColor(14, 0)
PrintN("")
PrintN("Enter the name for the packed file (e.g., archive.pack):")
PackFileName = Input()
If PackFileName = ""
ConsoleColor(12, 0)
PrintN("No file name entered. Try again?(y/n)")
YesNo()
If LCase(Key) = "n"
EndProgram()
EndIf
Else
Break
EndIf
ForEver
; Construct full path for packed file
PackFilePath = SelectedDest + PackFileName
ConsoleColor(14, 0)
PrintN("")
PrintN("Choose Packer Method:")
ConsoleColor(8, 0)
PrintN("1 = Zip Packer")
PrintN("2 = BriefLZPacker (can NOT preserve folder heirarchy!)")
Menu12()
Select LCase(Key)
Case "1"
ConsoleColor(11, 0)
PrintN("Zip Packer")
PackerMethod = 1
Case "2"
ConsoleColor(11, 0)
PrintN("BriefLZPacker")
PackerMethod = 0
EndSelect
If PackerMethod
UseZipPacker()
Else
UseBriefLZPacker()
EndIf
Define TryAgain.s
Define FolderChoice.s
ConsoleColor(14, 0)
PrintN("")
PrintN("Choose to retain original folder hierchy:")
ConsoleColor(8, 0)
PrintN("1 = Pack files in folders, keeping original hierchy")
PrintN("2 = Pack files together in a single root location")
Menu12()
Select LCase(Key)
Case "1"
ConsoleColor(11, 0)
PrintN("Folder Hierarchy")
Case "2"
ConsoleColor(11, 0)
PrintN("No Hierarchy")
EndSelect
If Key = "1"
FolderHierarchy = 1
Else
FolderHierarchy = 0
EndIf
; Create packed file
PackID = CreatePack(#PB_Any, PackFilePath)
If PackID = 0
ConsoleColor(12, 0)
PrintN("Failed to create packed file.")
EndProgram()
EndIf
; Prepare details file in destination folder
DetailsFile = selectedDest + "PackDetails.txt"
If CreateFile(1, DetailsFile)
DetailsID = 1
Else
ConsoleColor(12, 0)
PrintN("Failed to create details file.")
ClosePack(PackID)
EndProgram()
EndIf
; Add files from all folders in FolderArray() to pack and record details
Define offset.q = 0
Define i.l
For i = 0 To SourceCount - 1
PathName = FolderArray(i)
GetFolderName()
If FolderHierarchy
AddPackDirectory(PackID, Folder) ; Add folder structure if retaining hierarchy
EndIf
If ExamineDirectory(0, FolderArray(i), "*.*")
While NextDirectoryEntry(0)
If DirectoryEntryType(0) = #PB_DirectoryEntry_File
Define FileName.s = DirectoryEntryName(0)
Define FilePath.s = FolderArray(i) + FileName
Define FileSize.q = FileSize(FilePath)
If FolderHierarchy
AddPackFile(PackID, FilePath, Folder + "/" + FileName) ; e.g., "Images/file1.txt"
WriteStringN(DetailsID, "File: " + Folder + "/" + FileName)
Else
AddPackFile(PackID, FilePath, FileName) ; Flat structure
WriteStringN(DetailsID, "File: " + FileName)
EndIf
WriteStringN(DetailsID, "Size: " + Str(FileSize))
WriteStringN(DetailsID, "Offset: " + Str(offset))
WriteStringN(DetailsID, "")
offset + FileSize
EndIf
Wend
FinishDirectory(0)
Else
ConsoleColor(12, 0)
PrintN("Could not examine directory: " + FolderArray(i))
EndIf
Next
ClosePack(PackID)
CloseFile(DetailsID)
PrintN("")
ConsoleColor(8, 0)
PrintN("Packing complete. Details saved to")
ConsoleColor(14, 0)
PrintN(DetailsFile)
EndProgram()
// Moved from "General Discussion" to "Applications - Feedback and Discussion" (Kiffi)