For k,v in pairs help!

Heyo…


for k,v in pairs( file.Exists( "the-creator-logs/jobs/*.txt", "DATA" ) ) do
~~~~
end


I just wan’t to read ALL files in this directory, so then I can do this:


file.Read( "the-creator-logs/jobs/"..v, "DATA") 

But sadley, this doesn’t work. Please help! (The files are being created and work, just reading them all doesnt.

Aren’t you getting a lua error? (

file.Exists is supposed to return a boolean, not a table)

As NeatNit said, you’re trying to loop through a boolean here



if file.Exists( "the-creator-logs/jobs/*.txt", "DATA" ) then
     for k, v in pairs( file.Read( "the-creator-logs/jobs/"..v, "DATA") ) do
          --do stuff here
     end
end


file.Exists doesn’t work like that, also what the hell are you doing with v.

Uh… “Description: Returns a boolean of whether the file or directory exists or not.” and also I was copy+pasting what his original code was.

There’s nothing wrong with the code I posted?

Why dont you try and run it

Because I’m assuming * is indicative for whatever the hell the file is going to be?

No. That’s a wild card for file.Find

I did not know that.

With everything else posted, file.Read returns a string, not a table.

http://wiki.garrysmod.com/page/string/ToTable ?

Please stop, this isn’t helping the person who needs it.

Um, no one has really described my question at all. I only asked how do I read all files in my data folder. I just want to read all files in a directory in data folder.


file.Read( "the-creator-logs/jobs/*.txt", "DATA" )

You guys haven’t really helped. You’r all just fighting over who is right…

and I truly just want to print what EVERY sing file in my directory says… thats all.

[editline]17th November 2016[/editline]

Oh and, by looking at this you have no idea what lua is.


     for k, v in pairs( file.Read( "the-creator-logs/jobs/"..v, "DATA") ) do
          --do stuff here
     end

Problem here is firstly: v? Dude, v is created after the DO statement. For example: for k,v in pairs( player.GetAll() ) do. After do V and K is read, before it, 100% sure it will error because there is no such thing before the do statement. But nice try!

[editline]17th November 2016[/editline]

To give you guys some more information of what I am trying to do:


AddCSLuaFile()
include( "jobs.lua" )

local jobs = {
	examplejob = {
		name = "Example",
		salary = 250,
		category = "Citizens"
	},
	
	bobby = {
		name = "Bobby",
		salary = 100,
		category = "Gods"
	},
	
	mayor = {
		name = "Mayor",
		salary = 1000,
		category = "Presidential"
	}
}
local function PlayerInitialSpawn(ply)
	if not file.Exists( "data/the-creator-logs", "GAME" ) then
	file.CreateDir( "the-creator-logs" )
	file.CreateDir( "the-creator-logs/jobs" )
	file.Write( "the-creator-logs/read.txt", 'DO NOT DELETE THIS FILE, UNLESS YOU DO NOT WANT YOUR CUSTOM, SWEPS/WEAPONS, JOBS and MORE!' )
	end
	
	for k,v in pairs( jobs ) do
		if not file.Exists( "data/the-creator-logs/jobs/"..v.name, "GAME" ) then
			file.CreateDir( "the-creator-logs/jobs/"..v.name )
			file.Write( "the-creator-logs/jobs/"..v.name.."/salary.txt", v.salary )
			file.Write( "the-creator-logs/jobs/"..v.name.."/category.txt", v.category )
		end
	end
	
	-- PrintMessage( HUD_PRINTTALK, tostring( string.match( file.Read( "testing-shit/testing-data.txt" ), "^.*$" ) ) )
	-- PrintMessage( HUD_PRINTTALK, file.Read( "testing-shit/testing-data.txt" ) )
end
hook.Add( "PlayerInitialSpawn", "ply", PlayerInitialSpawn )

/\ INIT.lua, and this has the local jobs so I could test. It works, just I want to test from the data folders now.

In jobs.lua I am simply trying to figure out how to read ALL my data in my folder.


AddCSLuaFile()

local function PlayerInitialSpawn(ply)
	ply:ChatPrint( file.Read( "the-creator-logs/jobs/*.txt", "DATA" ) )
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn )

Oh and apparently in Patterned code this means ALL FILES: ^.*$ but it doesn’t work.

sick roast coming from the guy who needed help in the first place

Yes true, but it is not needed to post if you have no idea what lua is, on a lua forum…

Don’t forget he takes SF jobs and then proceeds to get everyone on FP to basically code it for him. He asks for help that many times.

Ok sorry, but I don’t do that anymore. I am just asking how to read every file I have in a directoy.
This is not for SF its just for my fun. I am just trying to make a simple Job Making script to upload to Gmod Workshop

Calm down man.

This isn’t tested, but:
-snip- code broken

file.Find should give you a list of files, but it won’t do a recursive check by itself - you’ll have to do that yourself (with its second return value which is all found folders).

This file does something similar: https://github.com/garrynewman/garrysmod/blob/master/garrysmod/gamemodes/sandbox/gamemode/cl_search_models.lua
But it’s a bit more complex, intended to not freeze the game while it’s working.

Edit: holy shit, 5x ninja’d