Introduction to Lua
1.Lua introduction
Lua is a lightweight and compact scripting language written in standard C and open in source code. It is designed to be embedded in applications, thereby providing flexible extension and customization capabilities for applications. It is designed to be embedded in the application, thereby providing flexible extension and customization capabilities for the application.
2.Lua characteristics
Lightweight
- It is written in standard C language and open in source code. After compilation, it is only more than 100 K, which can be easily embedded in other programs.
Scalable
- lua provides very easy-to-use extension interfaces and mechanisms: these features are provided by the host language (usually C or C++), and lua can use them just like an already built-in feature.
Other Features
- 1. Support process-oriented programming and functional programming
- 2. Automatic memory management; only provides a common type of table (table), which can implement arrays, hash tables, collections, and objects;
- 3. Language built-in pattern matching; closure; function can also be regarded as a value; provide multi-threading (co-process, threads not supported by the operating system);
- 4. Closures and tables can easily support some key mechanisms required for object-oriented programming, such as data abstraction, virtual functions, inheritance and overloading.
III. Application scenarios
- Game development
- Standalone application scripts
- Web application scripts
- Extensions and database plug-ins such as: MySQL Proxy and MySQL WorkBench
- Security systems, such as intrusion detection systems
lua Gets all folders under the specified path
1. Function acquisition
is a function in Lua that allows you to execute an external command and process the output of the command as a stream. If you want to pass in Luaimplement
dir
Command (linux command is ls) to obtain all files and their paths in the specified folder. You can construct a suitable Windows environmentdir
Command and passExecute this command.
-- exist Lua Called in dir Command and get output local handle = ('dir /b /s "C:\\path\\to\\your\\folder\\"') local files = {} for file in handle:lines() do files[#files + 1] = file end handle:close() -- 现exist 'files' The array contains the complete path to all files in the folder
Here/b
The parameters represent a concise format (only display the file name), and/s
Indicates the contents that include all subdirectories. Please note that you need to"C:\\path\\to\\your\\folder\\"
Replace with the actual folder path you are looking for.
If you are on a non-Windows system, such as Unix/Linux system, then you should usels
orfind
Command insteaddir
:
-- exist Unix/Linux Get all files under folders and subfolders in the system local handle = ('find "path/to/your/folder/" -type f') -- ... The same processing logic ...
In this example,find
The command is used to find all ordinary files in the specified directory and its subdirectories (-type f
)。
2. Use the lfs (LuaFileSystem) library to obtain
local lfs = require "lfs" function listFolders(path) local folders = {} local attr = (path) if == "directory" then (folders, path) for file in (path) do if file ~= "." and file ~= ".." then local f = path..'/'..file local attr = (f) if == "directory" then listFolders(f) end end end end return folders end -- Example of usage local path = "/your/specified/path" local folders = listFolders(path) for _, folder in ipairs(folders) do print(folder) end
Make sure you have installed itlfs
If the library is not installed, you can install it through LuaRocks
luarocks install luafilesystem
This code prints out all folder paths under the specified path. If you just want to get the direct subfolder, you canlistFolders
Remove the right from the functionrecursive call.
This is the article about lua getting all folders under the specified path. For more related lua to get folder content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!