* confusion: lfs, and dir path

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
Ron Melby
Megastar
Posts: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

confusion: lfs, and dir path

Post by Ron Melby »

I have a file (on my desktop) but it could be anywhere.

I am not sure, here, is lfs included in FH?

I am using the code snippet LoadCSV. it doesnt find the file, I guess it doesnt look thru MY whole computer path.

how do I find a file on my computer, and retrieve the path?
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 28410
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: confusion: lfs, and dir path

Post by tatewise »

To ensure lfs is loaded use:
require "lfs"

You are correct that code snippets like LoadCSV do NOT search for files, and need an explicit full file path & name.
But how does Plugin know the filename? Presumably it asks user? So reply with full path & name.

If you must search for files you have to use a loop that iterates within strDir and its subfolders:
for strFilename in DirTree(strDir) do
... do whatever with strFilename to check if your required file has been found.
end

That requires the DirTree iterator function:

Code: Select all

-- Return a Directory Tree entry & attributes on each iteration --
function DirTree(strDir,...)
	assert(strDir and strDir ~= "", "directory parameter is missing or empty")
	if strDir:sub(-1) == "/"
	or strDir:sub(-1) == "\\" then
		strDir = strDir:sub(1,-2)								-- Remove trailing "/" or "\"
	end
    
	local function doYieldTree(strDir)
		for strEntry in lfs.dir(strDir) do
			if strEntry ~= "." and strEntry ~= ".." then
				strEntry = strDir.."\\"..strEntry
				local tblAttr, strError = lfs.attributes(strEntry)
				if not tblAttr then tblAttr = { mode="attrfail", error=strError } end 
				coroutine.yield(strEntry,tblAttr)
				if tblAttr.mode == "directory" then
					local isOK = true
					for intOmit, strOmit in ipairs (arg) do
						if strEntry:matches(strOmit) then	-- Omit tree branch
							isOK = false
							break
						end
					end
					if isOK then doYieldTree(strEntry) end
				end
			end
		end
	end -- local function doYieldTree

	return coroutine.wrap(function() doYieldTree(strDir) end)
end -- function DirTree
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
ColeValleyGirl
Megastar
Posts: 5499
Joined: 28 Dec 2005 22:02
Family Historian: V7
Location: Cirencester, Gloucestershire
Contact:

Re: confusion: lfs, and dir path

Post by ColeValleyGirl »

Or use something from the Penlight library
User avatar
Ron Melby
Megastar
Posts: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: confusion: lfs, and dir path

Post by Ron Melby »

I have decided my present problem has elided from my original statement, while it is deferred that is outstanding as well.

if Opt == 'Display' then
local fp = rtvpath('*temp')
fp = fp .. pgnam .. '.txt'
SaveFile(table.concat(PRTF, '\n') .. '', fp)

pp = "C:\\Program Files\\Just Great Software\\EditPad Lite 8\\EditPadLite8.exe"
local bOK, iErr, Error = fhShellExecute(pp, '"'.. fp ..'"', '','', 3)
-- os.execute('start /wait /b EditPadLite8.exe ' .. '"'.. fp ..'"')
print( bok, ' ', iErr, ' ', Error)
print (pp)
print (fp)

return
end

the os.execute works without a fully qualified path. but there is a horrendously annoying command screen that cant be gotten rid of.

using powershell, I see no way to say (in my lingo) *LIBL/EditPadLite8.exe or equivalent %PATH%\\EditPadLite8.exe meaning the first one you see in the path.

good news is it does work.... but I try to avoid at all costs hardcoding a path....ideas?
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 28410
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: confusion: lfs, and dir path

Post by tatewise »

Try the following similar to plugins:code_snippets:launch_a_program_for_current_gedcom|> Launch a Program for Current Gedcom (code snippet) that says you should not need the path for installed programs.

pp = '"EditPadLite8.exe"'
local bOK, iErr, Error = fhShellExecute( pp, ...


BTW:
The directory iterator technique is from plugins:code_snippets:directory_tree|> Directory Tree (code snippet).
Alternatively ask user to select directory plugins:code_snippets:prompt_for_folder|> Prompt For Folder (code snippet).
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Ron Melby
Megastar
Posts: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

Re: confusion: lfs, and dir path

Post by Ron Melby »

windows 10 has strange settings.

I crashed when loading its a new windows 10 installation, had to add C:\Program Files (and I added 86 and Data as well) to the Path variable. works just fine.
FH V.6.2.7 Win 10 64 bit
Post Reply