File Auto Numbering?

How would you auto number file? I want to create a file with the name as the date. How would I make it so when 2 files are created on the same day, it creates a file with a (2) or a (3) and so on at the end?

If I had:


It would create


and if that existed, it would create



I hope that’s enough detail.

If it exists, then create it with a (i);, and you can do a for loop to see if the numbers exist already.

I know how to use the for loop for basic needs, but I’m not sure how I would use it in file.Exists.

I helped someone create a system like this; one of the best ways is to save a simple file with the prefix of the file you’re saving with a number in it. The number represents the next file to save. After a file is saved, increment the number.

Here’s an idea

[lua]local function Write( sFile, sData, i )
i = i or 1

if ( file.Exists( sFile, "GAME" ) ) then
	local sExtension = string.GetExtensionFromFilename( sFile )
	return Write( string.StripExtension( sFile ) .. '(' .. i .. ')' .. sExtension, sData, i + 1 )

return file.Write( sFile, sData )


Looping through all files isn’t needed. Just store the key in a file.

say you save blah_blah_x.txt…

Make a function that takes the original name without the number like blah_blah, then read that .txt file or create if it doesn’t exist with 1 being the first number. save name_x.txt where x is the number that was read or the default number assigned. Save the file with x + 1. Done.

I’ll try to find the code later

Check if the file without the numbers exist. If it exists, for i = 2, math.huge check if file (i) exists. If it doesn’t, write file (i) and break.

function WriteFile(name, contents)
local rawName = string.StripExtension(name)
local found = #file.Find(rawName…"*", “DATA”)

if (found > 0) then
file.Write(rawName…" ("…found…").txt", contents)
file.Write(name, contents)

The issue with that, is that if a file is deleted, it’ll start overwriting others. Try this:

// Incrementally sequence files without overwriting issues ondelete - Josh 'Acecool' Moser
function file:WriteIncrementally( _filename, _data )
	// Define the folder to use -- Mine is: acecooldev/incrementally_saved/ in the garrysmod/data/ folder.
	local _folder = GAMEMODE.Name .. "/incrementally_saved/";

	// Ensure the folder exists, if not then create it.
	if ( !file.Exists( _folder, "DATA" ) ) then
		file.CreateDir( _folder );

	// Grab the current index or define it as 1
	local _index = tonumber( file.Read( _folder .. _filename .. ".txt", "DATA" ) || 1 );

	// Write the file in the desired format :: name(x).txt
	file.Write( _folder .. _filename .. "(" .. _index .. ").txt", _data, "DATA" );

	// Update the index
	file.Write( _folder .. _filename .. ".txt", _index + 1, "DATA" );

// Produces testing(x).txt, index saved as testing.txt
file:WriteIncrementally( "testing", "testing_data" );

I had to rewrite it, but it is very straight forward; this does what you need without having issues if you delete files.

Here it is:

You may go one step further and string.lower everything to avoid having issues with Linux, etc…; I’ll update the html to reflect that change; remove .html from the url to show the Lua file.

Is there any reason why you start all your variables with an _ ?

It obviously makes it more efficient and easy to read.

Just kidding.

It’s my style and people dislike that I have my own way of thinking, my own coding style, and that I don’t conform to the “social norm”… I believe I touch on why in there, but if not: Logic is universal and I code in many different languages. I write my code so that it is as close as possible to everything else I write to ensure an easy transition when I transcribe logic to different languages.

It just seems unpractical to prefix the most common variable type.

it’s fucking stupid; but reasoning with Acecool is one of the wonders of the universe.

Your _coding; _standards; do nothing to help the GMod / developer community. Please stop referencing that file every time we discuss your “style”. It’s impractical, ugly (and harder to read, as you’re using C operators in lua, and unless you have a specific module to Sublime Text / NP++, you won’t see them) and bad practice to use that sort of style in lua.

How your code looks in ST2:[/t]

How your code looks with my (the “social norm”) style:

Unless this is some sort of parallel universe, the code in the second image is easier to look at / read.

Especially for people who might just be starting off with GLua as their first language. Adding that in will just confuse the hell out of them and scare them off.

The reason I heavily comment on “tutorial” posts, and tutorial code is to help people wanting to learn, learn the ropes and what each thing means. I don’t comment that heavily in my typical code aside from the standard // triple line above functions / classes to briefly describe the function. So, don’t claim just by removing comments, or positioning them on the side which looks sloppy, does something, because they’re there to help people learn.

[editline]20th May 2014[/editline]

Not really; if they don’t understand variables can be named anything you like then it teaches them. How many times have people tried using “ply” without it being defined because they thought it was globally defined since they see it everywhere? I believe that causes more problems than individuals having their own coding standard.

Can we not have weekly discussions about Acecool’s coding style?

Thanks, I got it to work thanks to you. However, with the code you gave me, it created 2 files, 1 with the actual data I specified, and another with the index. I managed to fix it, and it’s working amazingly now. Thank you!

To quote Acecool, “I use an underscore ( _ ) before all local variables and in argument naming for functions. I use a double underscore ( __ ) for all data-storage / data which shouldn’t be manipulated such as seen within classes and their dictionaries. It’s meant to be for private local vars.” I for one actually think his coding style is extremely well structured.