nodemcu-firmware/docs/modules/file.md

160 lines
3.1 KiB
Markdown
Raw Normal View History

2016-09-22 12:13:51 +02:00
# file Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
| 2014-12-22 | [Zeroday](https://github.com/funshine) | [Zeroday](https://github.com/funshine) | [file.c](../../components/modules/file.c)|
2016-09-22 12:13:51 +02:00
Historically the file module provided both file meta information/manipulation
as well as file contents manipulation. These days file content manipulation
is handled via the standard Lua `io` module, and only meta information
is handled by the file module.
2016-09-22 12:13:51 +02:00
The file module operates on the virtual file system provided by the
Espressif VFS component, with a couple of exceptions. Specifically, the
[`fsinfo()`](#filefsinfo) and [`format()`](#fileformat) functions
operate only on the default SPIFFS file system, as configured in the
platform settings in Kconfig.
2016-09-22 12:13:51 +02:00
## file.exists()
Determines whether the specified file exists.
#### Syntax
`file.exists(filename)`
#### Parameters
- `filename` file to check
#### Returns
true if the file exists (even if 0 bytes in size), and false if it does not exist
2016-09-22 12:13:51 +02:00
#### Example
```lua
files = file.list()
if files["device.config"] then
print("Config file exists")
end
if file.exists("device.config") then
print("Config file exists")
end
```
#### See also
[`file.list()`](#filelist)
2016-09-22 12:13:51 +02:00
## file.format()
Format the file system. Completely erases any existing file system and writes a new one. Depending on the size of the flash chip in the ESP, this may take several seconds.
!!! note
Function is not supported for SD cards.
2016-09-22 12:13:51 +02:00
#### Syntax
`file.format()`
#### Parameters
none
#### Returns
`nil`
#### See also
[`file.remove()`](#fileremove)
## file.fsinfo()
Return size information for the file system, in bytes.
2016-09-22 12:13:51 +02:00
!!! note
Function is not supported for SD cards.
2016-09-22 12:13:51 +02:00
#### Syntax
`file.fsinfo()`
#### Parameters
none
#### Returns
- `remaining` (number)
- `used` (number)
- `total` (number)
#### Example
```lua
-- get file system info
remaining, used, total=file.fsinfo()
print("\nFile system info:\nTotal : "..total.." (k)Bytes\nUsed : "..used.." (k)Bytes\nRemain: "..remaining.." (k)Bytes\n")
```
## file.list()
Lists all files in the file system.
#### Syntax
`file.list([mountpoint])`
2016-09-22 12:13:51 +02:00
#### Parameters
`mountpoint` to list files in other file systems, the mountpoint can be given.
2016-09-22 12:13:51 +02:00
#### Returns
A lua table which contains the {file name: file size} pairs. For SPIFFS
file systems the size is returned in bytes, whereas for FAT file systems
the size is given in kilobytes.
2016-09-22 12:13:51 +02:00
#### Example
```lua
l = file.list();
for k,v in pairs(l) do
print("name:"..k..", size:"..v)
end
```
## file.remove()
2016-09-22 12:13:51 +02:00
Remove a file from the file system. The file must not be currently open.
2021-04-27 22:30:46 +02:00
#### Syntax
`file.remove(filename)`
#### Parameters
`filename` file to remove
#### Returns
`nil`
#### Example
```lua
-- remove "foo.lua" from file system.
file.remove("foo.lua")
```
## file.rename()
Renames a file.
2016-09-22 12:13:51 +02:00
#### Syntax
`file.rename(oldname, newname)`
2016-09-22 12:13:51 +02:00
#### Parameters
- `oldname` old file name
- `newname` new file name
2016-09-22 12:13:51 +02:00
#### Returns
`true` on success, `false` on error.
2016-09-22 12:13:51 +02:00
#### Example
2016-09-22 12:13:51 +02:00
```lua
-- rename file 'temp.lua' to 'init.lua'.
file.rename("temp.lua","init.lua")
```