File system
Files and directories as a whole, and file names.
FileSize ✅
Result.q = FileSize(Filename$)
Returns the file's size in bytes, −1 if there is no such file, or −2 if it is a directory.
DeleteFile ✅
Result = DeleteFile(Filename$)
CopyFile ✅
Result = CopyFile(Source$, Destination$)
An existing destination is replaced.
RenameFile ✅
Result = RenameFile(OldName$, NewName$)
Renames or moves a file.
CreateDirectory ✅
Result = CreateDirectory(Name$)
All four return nonzero on success.
OpenConsole()
f = CreateFile(#PB_Any, "size.txt")
WriteString(f, "12345")
CloseFile(f)
PrintN(Str(FileSize("size.txt")) + " " + Str(FileSize("no such file")) + " " + Str(FileSize("docs")))
CopyFile("size.txt", "size2.txt")
RenameFile("size2.txt", "size3.txt")
PrintN(Str(FileSize("size3.txt")))
DeleteFile("size.txt")
DeleteFile("size3.txt")
Output:
5 -1 -2
5
GetCurrentDirectory ✅
Path$ = GetCurrentDirectory()
The current directory, ending with \.
GetPathPart ✅
Path$ = GetPathPart(FullPathName$)
The directory part of a path, including the final \ (or /).
GetFilePart ✅
Filename$ = GetFilePart(FullPathName$ [, Mode])
The file name without the directory. With #PB_FileSystem_NoExtension, the extension is removed too.
GetExtensionPart ✅
Extension$ = GetExtensionPart(FullPathName$)
The extension, without the ..
OpenConsole()
p$ = "C:\Projects\game\main.pb"
PrintN(GetPathPart(p$))
PrintN(GetFilePart(p$))
PrintN(GetFilePart(p$, #PB_FileSystem_NoExtension))
PrintN(GetExtensionPart(p$))
PrintN(Right(GetCurrentDirectory(), 1))
Output:
C:\Projects\game\
main.pb
main
pb
\
ExamineDirectory ✅
Result = ExamineDirectory(#Directory, Directory$, Pattern$)
Result = NextDirectoryEntry(#Directory)
Name$ = DirectoryEntryName(#Directory)
Type = DirectoryEntryType(#Directory) ; #PB_DirectoryEntry_File or _Directory
Size.q = DirectoryEntrySize(#Directory)
Attributes = DirectoryEntryAttributes(#Directory)
Date.q = DirectoryEntryDate(#Directory, DateType) ; #PB_Date_Created, _Accessed, _Modified
FinishDirectory(#Directory)
Result = IsDirectory(#Directory)
ExamineDirectory starts listing a directory ("" is the current one). It returns non-zero, or 0 if the directory can't be read. With #PB_Any it returns the new directory's number. NextDirectoryEntry moves to the next entry and returns 0 when there are none left. The other functions describe the entry it moved to. FinishDirectory ends the listing.
Pattern$ picks entries by name: * stands for any run of characters and ? for one. "" or "*.*" keeps everything. On Windows, names match without regard to case. An x addition: several patterns separated by |, as in "*.png|*.jpg". The list includes . and .., and the entries come in the order the file system keeps them.
OpenConsole()
dir$ = GetTemporaryDirectory() + "x_listing" + #PS$
CreateDirectory(dir$)
CreateDirectory(dir$ + "pictures")
For i = 1 To 3
f = CreateFile(#PB_Any, dir$ + "note" + Str(i) + ".txt")
WriteString(f, Space(i * 100))
CloseFile(f)
Next
NewList found.s()
If ExamineDirectory(0, dir$, "*.txt|pictures")
While NextDirectoryEntry(0)
AddElement(found())
If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory
found() = DirectoryEntryName(0) + " (directory)"
Else
found() = DirectoryEntryName(0) + " " + Str(DirectoryEntrySize(0)) + " bytes"
EndIf
Wend
FinishDirectory(0)
EndIf
SortList(found(), #PB_Sort_Ascending)
ForEach found() : PrintN(found()) : Next
DeleteDirectory(dir$, "", #PB_FileSystem_Recursive)
Output:
note1.txt 100 bytes
note2.txt 200 bytes
note3.txt 300 bytes
pictures (directory)
DeleteDirectory, CopyDirectory ✅
Result = DeleteDirectory(Directory$, Pattern$ [, Mode])
Result = CopyDirectory(Source$, Destination$, Pattern$ [, Mode])
DeleteDirectory deletes the files that match Pattern$. With "" (or "*.*") as the pattern it then deletes the directory itself. CopyDirectory copies the matching files, making Destination$ if needed.
The Mode flags are #PB_FileSystem_Recursive, which goes into subdirectories, and #PB_FileSystem_Force. For DeleteDirectory, Force deletes read-only files too. For CopyDirectory, it replaces files already there; without it they are kept and the result is 0. Both return non-zero if everything worked.
GetFileAttributes, SetFileAttributes ✅
Attributes = GetFileAttributes(Filename$)
Result = SetFileAttributes(Filename$, Attributes)
GetFileAttributes returns the attributes, or -1 if there's no such file. On Windows they are #PB_FileSystem_ReadOnly, _Hidden, _System, _Archive, _Normal and _Compressed. On Linux they are #PB_FileSystem_ReadUser, _WriteUser, _ExecUser, _ReadGroup, _WriteGroup, _ExecGroup, _ReadAll, _WriteAll and _ExecAll (the permission bits), plus #PB_FileSystem_Link for a symbolic link.
GetFileDate, SetFileDate ✅
Date.q = GetFileDate(Filename$, DateType)
Result = SetFileDate(Filename$, DateType, Date.q)
DateType is #PB_Date_Created, #PB_Date_Accessed or #PB_Date_Modified. Dates are local time, like Date(). Linux has no creation date that can be set. Reading it gives the creation date where the file system keeps one, and otherwise the modification date.
OpenConsole()
name$ = GetTemporaryDirectory() + "x_dated.txt"
f = CreateFile(#PB_Any, name$) : CloseFile(f)
SetFileDate(name$, #PB_Date_Modified, Date(2020, 5, 17, 8, 30, 0))
PrintN(FormatDate("%dd/%mm/%yyyy %hh:%ii", GetFileDate(name$, #PB_Date_Modified)))
DeleteFile(name$)
Output:
17/05/2020 08:30
SetCurrentDirectory ✅
Result = SetCurrentDirectory(Directory$)
GetHomeDirectory, GetTemporaryDirectory, GetUserDirectory ✅
Path$ = GetHomeDirectory()
Path$ = GetTemporaryDirectory()
Path$ = GetUserDirectory(Type)
These return paths that end with the separator (#PS$). Type is one of:
#PB_Directory_Desktop,_Downloads,_Documents,_Videos,_Musics,_Picturesor_Public;#PB_Directory_ProgramData, the user's application data (AppData\Roaming, or~/.local/share);#PB_Directory_AllUserData, the data shared by all users (ProgramData, or/usr/share);#PB_Directory_Programs.
Windows asks the system. Linux reads the XDG user directories (~/.config/user-dirs.dirs), or else uses ~/Desktop and the like.
CheckFilename ✅
Result = CheckFilename(Filename$)
Returns 1 if the name (without a path) can be used for a file. On Windows these make it unusable: / \ : * ? " < > |, control characters, a space or dot at the end, and the names CON, PRN, AUX, NUL, COM1–COM9 and LPT1–LPT9. On Linux it's / or the names . and ...
Constants
#PS$ and #PS are the path separator (\ on Windows, / on Linux). #NPS$ and #NPS are the other one.