2016-09-22 12:13:51 +02:00
# file Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 21:19:03 +01:00
| 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
2021-10-20 12:24:09 +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
2021-10-20 12:24:09 +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
2021-10-20 12:24:09 +02:00
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 )
2021-10-20 12:24:09 +02:00
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.
2018-10-29 07:09:41 +01:00
!!! 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 )
2021-10-20 12:24:09 +02:00
## file.fsinfo()
Return size information for the file system, in bytes.
2016-09-22 12:13:51 +02:00
2018-10-29 07:09:41 +01: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
2021-10-20 12:24:09 +02:00
`file.list([mountpoint])`
2016-09-22 12:13:51 +02:00
#### Parameters
2021-10-20 12:24:09 +02:00
`mountpoint` to list files in other file systems, the mountpoint can be given.
2016-09-22 12:13:51 +02:00
#### Returns
2021-10-20 12:24:09 +02:00
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
```
2018-10-29 07:09:41 +01:00
## file.remove()
2016-09-22 12:13:51 +02:00
2018-10-29 07:09:41 +01:00
Remove a file from the file system. The file must not be currently open.
2021-04-27 22:30:46 +02:00
#### Syntax
2018-10-29 07:09:41 +01:00
`file.remove(filename)`
#### Parameters
`filename` file to remove
#### Returns
`nil`
#### Example
```lua
-- remove "foo.lua" from file system.
file.remove("foo.lua")
```
2021-10-20 12:24:09 +02:00
2018-10-29 07:09:41 +01:00
## file.rename()
2021-10-20 12:24:09 +02:00
Renames a file.
2016-09-22 12:13:51 +02:00
#### Syntax
2018-10-29 07:09:41 +01:00
`file.rename(oldname, newname)`
2016-09-22 12:13:51 +02:00
#### Parameters
2018-10-29 07:09:41 +01:00
- `oldname` old file name
- `newname` new file name
2016-09-22 12:13:51 +02:00
#### Returns
2018-10-29 07:09:41 +01:00
`true` on success, `false` on error.
2016-09-22 12:13:51 +02:00
#### Example
2018-10-29 07:09:41 +01:00
2016-09-22 12:13:51 +02:00
```lua
2018-10-29 07:09:41 +01:00
-- rename file 'temp.lua' to 'init.lua'.
file.rename("temp.lua","init.lua")
```