Sample Scripts: TLuaFileExample 1 --Script creates a new text file, writes a string to it and close it. file = TLuaFile() --Open a text file iRet = file:Open("c:\\test.txt",true) --Check if it could be created, it might exist and be write protected if iRet==0 then sErrString = "Failed to create file, error code:"..GetLastError().."\n" SetExitStatus(sErrString,false)
else
print("File created\n") --Write a string to the file sString = "Hello world!" file:Write(sString,string.len(sString)) --Close the file file:Close() SetExitStatus("Test ok",true) end
Example 2 --Script demonstrates: --File enumration --Directory enumration --Create and delete a directory --Construct a new file object file = TLuaFile(); --Scan the directory c:\temp for file using the wildcard *.* sResult = file:GetFileList("c:\\temp","*.*") print(sResult) --Scans the directory c:\temp for sub directories sResult = file:GetDirectoryList("c:\\temp") print(sResult) bResult = false
--Create a directory called "temp20" on the c: harddisk if file:CreateDirectory("c:\\temp20") then print("Created directory");
bResult = true
else
print("Failed to create directory") end
--Delete the directory we created above if file:DeleteDirectory("c:\\temp20") then bResult = true print("Deleted directory");
else
print("Failed to delete directory") end
if bResult then SetExitStatus("Test ok",true)
else
SetExitStatus("Test failed",false end
Example 3 --KNM Lua API example (C) 2006 Kaseya AB --Script creates a new text file, writes a string to it and close it. --Switch the context so that files are opened on the KNM host machine, not translating the paths file = TLuaFile(true) --Open a text file iRet = file:Open("c:\\test.txt",true) --Check if it could be created, it might exist and be write protected if iRet==0 then sErrString = "Failed to create file, error code:"..GetLastError().."\n"
SetExitStatus(sErrString,false) else
print("File created\n") --Write a string to the file sString = "Hello world!" file:Write(sString,string.len(sString)) --Close the file file:Close() SetExitStatus("Test ok",true) end
| |||
Topic 9726: Send Feedback. Download a PDF of this online book from the first topic in the table of contents. Print this topic. |