BlitzMax/模組/系統/檔案系統
BlitzMax 檔案系統模組包含用於對計算機的檔案和目錄執行操作的命令。
OpenFile、ReadFile 和 WriteFile 返回一個流物件,用於讀取和/或寫入檔案資料。
可以使用 ReadDir、NextFile 和 CloseDir 命令的組合逐個檔案檢查目錄,或者可以使用 LoadDir 將目錄的檔名讀取到字串陣列中。
可以使用 FileType、FileTime、FileSize 和 FileMode 命令檢查檔案屬性。
可以使用 CreateFile、CreateDir DeleteFile 和 DeleteDir 命令建立和刪除檔案和目錄(資料夾)。
最後,FileSystem 模組包含各種實用程式函式,用於以系統無關的方式處理檔案路徑。這些命令包括 RealPath、StripDir、StripExt、StripAll、ExtractDir 和 ExtractExt。
函式 StripDir$( path$ )
描述:從檔案路徑中刪除目錄
示例:
' stripdir.bmx
print stripdir("mypath/myfile.bmx") 'prints myfile.bmx
函式 StripExt$( path$ )
描述:從檔案路徑中刪除副檔名
示例:
' stripext.bmx
print stripext("mypath/myfile.bmx") 'prints mypath/myfile
函式 StripAll$( path$ )
描述:從檔案路徑中刪除目錄和副檔名
示例:
' stripall.bmx
print stripall("mypath/myfile.bmx") 'prints myfile
函式 StripSlash$( path$ )
描述:從檔案路徑中刪除尾部斜槓
資訊:StripSlash 不會從“根”路徑中刪除尾部斜槓。例如,“/”或(僅限 Win32)“C:/”。
示例:
' stripslash.bmx
print stripslash("mypath/") 'prints mypath
函式 ExtractDir$( path$ )
描述:從檔案路徑中提取目錄
示例:
' extractdir.bmx
print extractdir("mypath/myfile.bmx") 'prints mypath
函式 ExtractExt$( path$ )
描述:從檔案路徑中提取副檔名
示例:
' extractext.bmx
print extractext("mypath/myfile.bmx") 'prints bmx
函式 CurrentDir$()
描述:獲取當前目錄
返回值:當前目錄
示例:
' currentdir.bmx cd$=currentdir() print "CurrentDir()="+cd$
函式 RealPath$( path$ )
描述:獲取檔案路徑的真實絕對路徑
示例:
' realpath.bmx
print realpath("realpath.bmx") 'prints full path of this source
print realpath("..") 'prints full path of parent directory
函式 FileType( path$ )
描述:獲取檔案型別
返回值:如果 path 處檔案不存在,則返回 0;如果檔案是普通檔案,則返回 FILETYPE_FILE (1);如果檔案是目錄,則返回 FILETYPE_DIR (2)
示例:
' filetype.bmx
print filetype(".") 'prints 2 for directory type
print filetype("filetype.bmx") 'prints 1 for file type
print filetype("notfound.file") 'prints 0 for doesn't exist
函式 FileTime( path$ )
描述:獲取檔案時間
返回值:path 處檔案上次修改的時間
示例:
' filetime.bmx
print filetime("filetime.bmx")
函式 FileSize( path$ )
描述:獲取檔案大小
返回值:path 處檔案的大小(以位元組為單位),如果檔案不存在,則返回 -1
示例:
' filesize.bmx
' the following prints the size of this source file in bytes
print filesize("filesize.bmx")
函式 FileMode( path$ )
描述:獲取檔案模式
返回值:檔案模式標誌
示例:
' filemode.bmx
' the following function converts the file mode to
' the standard unix permission bits string
Function Permissions$(mode)
local testbit,pos
local p$="rwxrwxrwx"
testbit=%100000000
pos=1
while (testbit)
if mode & testbit res$:+mid$(p$,pos,1) else res$:+"-"
testbit=testbit shr 1
pos:+1
wend
return res
End Function
print Permissions$(filemode("filemode.bmx"))
函式 SetFileMode( path$,mode )
描述:設定檔案模式
示例:
' setfilemode.bmx
' the following makes this source file readonly
writebits=%010010010
' read the file mode
mode=filemode("setfilemode.bmx")
'mask out the write bits to make readonly
mode=mode & ~writebits
'set the new file mode
setfilemode("setfilemode.bmx",mode)
函式 CreateFile( path$ )
描述:建立檔案
返回值:如果成功,則返回 True
示例:
' createfile.bmx
success=createfile("myfile")
if not success runtimeerror "error creating file"
函式 CreateDir( path$,recurse=False )
描述:建立目錄
返回值:如果成功,則返回 True
資訊:如果 recurse 為 true,則還會建立任何必需的子目錄。
示例:
' createdir.bmx
success=createdir("myfolder")
if not success runtimeerror "error creating directory"
函式 DeleteFile( path$ )
描述:刪除檔案
返回值:如果成功,則返回 True
示例:
' deletefile.bmx
success=deletefile("myfile")
if not success runtimeerror "error deleting file"
Function RenameFile( oldpath$,newpath$ )
描述:重新命名檔案
返回值:如果成功,則返回 True
Function CopyFile( src$,dst$ )
描述:複製檔案
返回值:如果成功,則返回 True
Function CopyDir( src$,dst$ )
描述:複製目錄
返回值:如果成功,則返回 True
Function DeleteDir( path$,recurse=False )
描述:刪除目錄
返回值:如果成功,則返回 True
資訊:將recurse設定為true以遞迴刪除所有子目錄和檔案 - 但請謹慎操作!
示例:
' deletedir.bmx
success=deletedir("myfolder")
if not success runtimeerror "error deleting directory"
Function ChangeDir( path$ )
描述:更改當前目錄
返回值:如果成功,則返回 True
示例:
' changedir.bmx print "CurrentDir()="+currentdir() ' change current folder to the parent folder changedir ".." ' print new CurrentDir() print "CurrentDir()="+currentdir()
Function ReadDir( path$ )
描述:開啟目錄
返回值:一個整數目錄控制代碼,如果目錄不存在則返回0
示例:
' readdir.bmx dir=ReadDir(CurrentDir()) If Not dir RuntimeError "failed to read current directory" Repeat t$=NextFile( dir ) If t="" Exit If t="." Or t=".." Continue Print t Forever CloseDir dir
Function NextFile$( dir )
描述:返回目錄中的下一個檔案
返回值:使用ReadDir開啟的目錄中下一個檔案的名稱,如果不再有檔案可讀則返回空字串。
Function CloseDir( dir )
描述:關閉目錄
Function LoadDir$[]( dir$,skip_dots=True )
描述:載入目錄
返回值:包含dir內容的字串陣列
資訊:如果skip_dots引數為真,則從返回的陣列中刪除“.”(當前)和“..”(父)目錄。
示例:
' loaddir.bmx ' declare a string array local files$[] files=loaddir(currentdir()) for t$=eachin files print t next
Function OpenFile:TStream( url:Object,readable=True,writeable=True )
描述:開啟一個檔案以進行輸入和/或輸出。
資訊:此命令類似於OpenStream命令,但會嘗試快取檔案的內容以確保諸如http:之類的序列流是可查詢的。使用CloseStream命令完成讀取和/或寫入OpenFile返回的流。
示例:
' openfile.bmx
' the following prints the contents of this source file
file=openfile("openfile.bmx")
if not file runtimeerror "could not open file openfile.bmx"
while not eof(file)
print readline(file)
wend
closestream file
Function ReadFile:TStream( url:Object )
描述:開啟一個檔案以進行輸入。
資訊:此命令類似於ReadStream命令,但會嘗試快取檔案的內容以確保諸如http:之類的序列流是可查詢的。使用CloseStream命令完成讀取和/或寫入OpenFile返回的流。
示例:
' readfile.bmx
' the following prints the contents of this source file
file=readfile("readfile.bmx")
if not file runtimeerror "could not open file openfile.bmx"
while not eof(file)
print readline(file)
wend
closestream file
Function WriteFile:TStream( url:Object )
描述:開啟一個檔案以進行輸出。
資訊:此命令與WriteStream命令相同。
示例:
' writefile.bmx
file=writefile("test.txt")
if not file runtimeerror "failed to open test.txt file"
writeline file,"hello world"
closestream file
Function CloseFile( stream:TStream )
描述:關閉檔案流。
資訊:在對開啟的檔案執行檔案操作後,請確保使用CloseFile或相同的CloseStream命令關閉檔案流。