nextfile is a small Lua library for generating the next file in a sequence of files. It was developed for use with tools that export sections of a video as images, and need to be able to name files sequentially without having generated all of the previous files themselves.
Go to file
The Magician 1500d7416e Add usage examples to README.md 2023-12-12 16:45:17 +00:00
src Fix nextfile expecting full path from prevfile 2023-11-20 18:59:37 +00:00
tests Fix nextfile expecting full path from prevfile 2023-11-20 18:59:37 +00:00
.busted Move spec/ to tests/, change default options 2023-11-20 11:54:25 +00:00
.gitignore .gitignore all .rock files 2023-11-20 19:03:57 +00:00
README.md Add usage examples to README.md 2023-12-12 16:45:17 +00:00
nextfile-0.0.2-1.rockspec Bump version number 2023-11-20 19:02:15 +00:00

README.md

nextfile

nextfile is a Lua library used to generate the "next" filename in a sequence.

Installation

nextfile is published on LuaRocks, use luarocks install nextfile to install it.

Usage

The nextfile library actually contains two exported modules, nextfile and prevfile. prevfile scans the files in a directory and returns the "most recent" filename according to a "human sort" algorithm. prevfile is also leveraged by nextfile, which then gives the "next file" according to the following rules:

  • If the previous filename is entirely numeric, then the next file is the next number. For example, if the previous file were "500.jpg", then the next file would be "501.jpg".
  • If the previous filename ends in a letter, then the next file will bump it to the next letter. For example, if the previous file were "200a.png", the next file would be "200b.png".

By default, nextfile will give the next file the same file extension as the previous file; there is a second optional parameter for specifying the file extension.

Examples

Create Test Files

user@box:~$ cd $(mktemp -d)
user@box:/tmp/tmp.g9nf7G4U1C $ touch {1,2,3}.txt

Use prevfile

local prevfile = require("prevfile")
local pfile = prevfile.prevfile("/tmp/tmp.g9nf7G4U1C")
print(pfile) -- 3.txt

Use nextfile

local nextfile = require("nextfile")
local nfile = nextfile.nextfile("/tmp/tmp.g9nf7G4U1C")
print(nfile) -- 4.txt

-- Specify an alternate file extension
nfile = nextfile.nextfile("/tmp/tmp.g9nf7G4U1C", "png")
print(nfile) -- 4.png

Create Alphabetical Test File

user@box:/tmp/tmp.g9nf7G4U1C $ touch 4a.txt
local nextfile = require("nextfile")
local nfile = nextfile.nextfile("/tmp/tmp.g9nf7G4U1C")
print(nfile) -- 4b.txt