|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Wed Jan 18, 2017 9:09 pm Post subject: Open file using Open Dialog (inspirated by Lua File System) |
|
|
Hi,
I try to make a file browser / opener using CE open dialog :
Code: | file_array = {
{fExt=".txt",fAccess="notepad.exe"},
{fExt=".jpg",fAccess="MSPaint.exe"},
{fExt=".png",fAccess="MSPaint.exe"},
{fExt=".doc",fAccess="winword.exe"},
{fExt=".xls",fAccess="excel.exe"}
}
function CEButton1Click(sender)
dialog = createOpenDialog()
dialog.Filename = 'file name'
dialog.DefaultExt = ".ct"
dialog.Filter = "file name (*.ct)|*.ct"
dialog.FilterIndex = 1
dialog.Options = '[ofEnableSizing]'
if dialog.execute() then
file=io.open(dialog.Filename, "r")
file:close()
file_ext = -------------------- get file extension
index = file_array[index]
if index == nil or file_ext ~= index.fExt then ------ look up from table array
showMessage("Can't recognize file type")
shellExecute(index.fAccess/Filename)
openProcess(index.fAccess/Filename)
end
end |
How to improve script above to open file according file extension and process by correct windows application when open it ?
Need this because not want using "require" module such as LFS.
Thanks and regards
|
|
Back to top |
|
|
Zanzer I post too much Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Thu Jan 19, 2017 7:41 am Post subject: |
|
|
Code: | shellExecute("cmd", [[/c start "" "E:/zanzer.txt"]]) |
|
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Thu Jan 19, 2017 9:29 pm Post subject: |
|
|
thanks Zanzer,
Code: | shellExecute("cmd", [[/c start "" "E:/zanzer.txt"]]) |
I can't find the way to get return filename when doing dialog.execute() to substitute this "E:/zanzer.txt".
So, maybe is better add textbox to give user input their path/filename and then get it as a variable to use with shellExecute.
Then the openFileDialog() function just use for user to preview or locating where is their file on the computer.
Regards
|
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Fri Jan 20, 2017 7:49 am Post subject: |
|
|
How to use lookup from table array ?
Code: | apps = {
{ext="txt", app="notepad.exe"},
{ext="xls", app="EXCEL.exe"},
{ext="doc", app="MSWORD.exe"}
}
index = "txt"
index = apps[index]
if index == nil then
print("Not in the table")
else
print(tostring(index.app))
end |
--- result always return "Not in the table"
Thanks
|
|
Back to top |
|
|
atom0s Moderator Reputation: 198
Joined: 25 Jan 2006 Posts: 8516 Location: 127.0.0.1
|
Posted: Fri Jan 20, 2017 1:56 pm Post subject: |
|
|
You are not using a numeric index in your table, so you need to loop through the k/v pair and locate it that way.
Code: |
apps = {
{ext="txt", app="notepad.exe"},
{ext="xls", app="EXCEL.exe"},
{ext="doc", app="MSWORD.exe"}
}
for k, v in pairs(apps) do
if (v.ext == "doc") then
print(v.app)
end
end
|
_________________
- Retired. |
|
Back to top |
|
|
mgr.inz.Player I post too much Reputation: 218
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Fri Jan 20, 2017 3:59 pm Post subject: |
|
|
@Corroder, Actually, this table is indexed. You are using table with "index/value" pairs.
And this:
Code: | apps = {
{ext="txt", app="notepad.exe"},
{ext="xls", app="EXCEL.exe"},
{ext="doc", app="MSWORD.exe"}
} |
is the same as this:
Code: | apps = {
[1]={ext="txt", app="notepad.exe"},
[2]={ext="xls", app="EXCEL.exe"},
[3]={ext="doc", app="MSWORD.exe"}
} |
To search something in this table, you will need "for loop". And to keep the order when iterating, you should use ipair.
If you don't want to lookup through whole table, use table with "key/value" pairs.
Code: | apps = {
["txt"]="notepad.exe",
["xls"]="EXCEL.exe",
["doc"]="MSWORD.exe"
}
print( apps.doc )
>MSWORD.exe
print( apps["doc"] )
>MSWORD.exe |
Read whole http://lua-users.org/wiki/TablesTutorial
_________________
|
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Fri Jan 20, 2017 7:42 pm Post subject: |
|
|
Thanks atom0s,
Code: | for k, v in pairs(apps) do
if (v.ext == "doc") then
print(v.app)
end
end
|
it work and i have put it to my script :
Code: | function GetFileExtension(path)
local str = path
local temp = ""
local result = "."
for i = str:len(), 1, -1 do
if str:sub(i,i) ~= "." then
temp = temp..str:sub(i,i)
else
break
end
end
for j = temp:len(), 1, -1 do
result = result..temp:sub(j,j)
end
return result
end
function file_check(file_name)
local file_found=io.open(file_name, "r")
if file_found==nil then
file_found=showMessage(file_name .. " ... Error - File Not Found")
else
file_found=showMessage(file_name .. " ... File Found")
end
return file_found
end
apps = {
{ext=".txt", app="notepad.exe"},
{ext=".xlsx", app="EXCEL.exe"},
{ext=".doc", app="WINWORD.exe"}
}
function extLookup(ext)
for k, v in pairs(apps) do
if (v.ext == ext) then
print(v.app)
shellExecute(v.app, path);end
end
end
function DoOpenFile()
path = UDF1.CEEdit1.Text
if path == "" then
showMessage("Enter full path and file name you want to open. If you not sure, use explorer to check it.")
return
end
file_check(path)
print(path)
ext = GetFileExtension(path)
print(ext)
extLookup(ext)
end
x = 0
function DoExplorer() --- Make Button2 as ON/OFF
if(x == 0)then
shellExecute'explorer.exe'
openProcess'explorer.exe'
x = 1
else
shellExecute("taskkill", "/IM explorer.exe /F")
x = 0
end
end
function closeTrainer()
closeCE()
return caFree
end
UDF1.CEButton1.onClick = DoOpenFile
UDF1.CEButton2.onClick = DoExplorer
UDF1.CEButton3.onClick = closeTrainer
form_onClose(UDF1, closeTrainer)
UDF1:show() |
thanks also mgr.inz.Player
i have read : http://lua-users.org/wiki/TablesTutorial
and also search for some references from other site, but not found something simple like your explained. With your explain, it's more easier for me to understand.
I did worked with table array before, but i used it for look up a variable choose via a Combobox and I am not facing problem while to do it because it using a numeric index in the table, like atom0s said.
Got everything good and growing my knowledge about lua that can use in future, even I am learn everything by self-taught.
Thanks and regards
|
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 941
|
Posted: Fri Jan 20, 2017 8:58 pm Post subject: |
|
|
Let's make mgr.inz.Player's [key=extension] idea more complicated (add template on formatting command and param to be executed by shellExecute)
Code: |
-- sample template format: extension -> app cmd, app param
defaultExt2App = {
txt = {'notepad', '"^F"',true}, -- 3rd parameter 'true' tell the directory has to be exist
bat = {'cmd','/c "^F"'},
cmd = {'cmd','/c "^F"'},
[''] = {'explorer','/select,"^F"'}, -- handle if extension is empty string
}
function pathSplit(p) -- thanks from Zanzer's hint http://forum.cheatengine.org/viewtopic.php?p=5714297#5714297
local dir,name,ext
if p:find"%.[^:/\\]-$" then
dir,name,ext = p:match"^(.-)([^:/\\]-)%.+([^%.:/\\]-)$"
else
dir,name,ext = p:match"^(.-)([^:/\\]*)$"
end
ext = ext or ''
if ext~='' and name=='' then name,ext = '.'..ext,'' end
return dir,name,ext
end
function dirExist(dir, testWritable)
dir = dir:match"^(.-)[/\\]*$"..'/'
if not testWritable then
return nil~=getDirectoryList(dir)
else
local testNUL = io.open(dir..'nul','wb')
return testNUL and testNUL:close() or false
end
end
function file2RunApp(file, Ext2app)
local ext2app = {}
for k,v in pairs(Ext2app or {}) do if not ext2app[k] then ext2app[k]=v end end
for k,v in pairs(defaultExt2App) do if not ext2app[k] then ext2app[k]=v end end
local dir,name,ext = pathSplit(file)
local appParam = ext2app[ext:lower()]
if not appParam then return nil,'Application Not known: '..tostring(file)end
local cmd,param,reqDirExist = appParam[1],appParam[2],appParam[3]==true
if reqDirExist and not getDirectoryList(dir) then
error("require directory not exist.",2)
end
local ctx = cmd and {D = dir, N = name, E = ext, F = file }
cmd = cmd and cmd:gsub("%^([DNEF])",ctx)
param = param and param:gsub("%^([DNEF])",ctx) or nil
return cmd and pcall(shellExecute, cmd, param)
end
-- test
local target = "lsdir.cmd"
local testcmd = io.open(target,'wb')
if testcmd then
testcmd:write[[
@echo off
dir *.* /w/s
pause
]]
testcmd:close()
end
if os.rename(target,target) then--file exist?
print('try excuting :'..target)
print('ok ? '..tostring( file2RunApp(target) ))
end
|
usage:
Code: |
-- support functions
fileGetExt (f) -- get the file extension
fileGetName (f) -- get the file base name excluding extension
fileSplit (f, updir) -- splite the file into diretory, name, ext, and return itself concat with a possible parent directory when f is relative
-- main function
file2RunApp(f, Ext2App)
- f : the input file with diretory as string
- Ext2App : a table to specify how to format command and parameter accoding to file extension, use defaultExt2App if omitted
- return true if an corresponding shellExecute can be run
-- template to format command and parameters:
In the template, use ^F = full file name, ^D = diretory part, ^N = base name, ^E = extension, to substitute. See above defaultExt2App.
The extension as key has to make lower case.
|
_________________
- Retarded.
Last edited by panraven on Sun Jan 22, 2017 7:16 pm; edited 8 times in total |
|
Back to top |
|
|
Zanzer I post too much Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Fri Jan 20, 2017 9:41 pm Post subject: |
|
|
Code: | local dialog = createOpenDialog(nil)
dialog.options = "[ofAllowMultiSelect]"
local result = dialog.execute()
if result then
local files = dialog.files
for i = 0, files.count - 1 do
shellExecute("cmd", [[/c start "" "]]..files[i]..[["]])
end
end
dialog.destroy() |
|
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 941
|
Posted: Fri Jan 20, 2017 10:17 pm Post subject: |
|
|
Zanzer wrote: | Code: | local dialog = createOpenDialog(nil)
dialog.options = "[ofAllowMultiSelect]"
local result = dialog.execute()
if result then
local files = dialog.files
for i = 0, files.count - 1 do
shellExecute("cmd", [[/c start "" "]]..files[i]..[["]])
end
end
dialog.destroy() |
|
Zanzer's 'start' solution is simple and elegant, but I got "Not enough storage is available to process this command" on running *.cmd or *.bat which prevent the console to close itself.
Not known if it only my machine setting.
bye~
Description: |
|
Filesize: |
37.44 KB |
Viewed: |
16286 Time(s) |
|
_________________
- Retarded. |
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Sat Jan 21, 2017 4:53 am Post subject: |
|
|
Zanzer
Code: | local dialog = createOpenDialog(nil)
dialog.options = "[ofAllowMultiSelect]"
local result = dialog.execute()
if result then
local files = dialog.files
for i = 0, files.count - 1 do
shellExecute("cmd", [[/c start "" "]]..files[i]..[["]])
end
end
dialog.destroy() |
Tested today Jan 21. 2017 :
Work good as same as using explorer [Windows 7 Ultimate - 32bit]
Code: | shellExecute("cmd", [[/c start "" "E:/filename.txt"]])
--- also tested with doc, xlsx, jpg, etc
path = UDF1.CEEdit1.Text --- eq. "E:/filename.txt
shellExecute("cmd", [[/c start "" ", path]])
|
Tested on Jan 19. 2017 :
File not open and got something windows message about powerShell.
Don't know what is the problem.
But when testing again today Jan 21, with shellExecute("cmd", [[/c start "" "E:/filename.txt"]]), it work with no issue.
Panraven,
I still try to learn and try to understand about your script logic and trying to implement that script.
Thanks to all of you
|
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 941
|
Posted: Sat Jan 21, 2017 1:45 pm Post subject: |
|
|
No problem, at least you said you have tried.
I'll try stick to what I learnt from the past too.
bye~
_________________
- Retarded. |
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Sat Jan 21, 2017 10:43 pm Post subject: |
|
|
Panraven bro,
Test your functions above :
Code: | file = "E:test.txt"
print(fileGetExt (file)) -- get the file extension
print(fileGetName (file)) -- get the file base name excluding extension
print(fileSplit (file, updir))
file2RunApp(file,Ext2app)
-- result [[
txt E:test
E:test
E:test txt E:test.txt
-- Notepad open correctly while use : file2RunApp(file,Ext2app)
]]
-- again :
file = "E:\test.txt" -- add \ at directory name
print(fileGetExt (file)) ----> txt E: est
print(fileGetName (file)) ----> E: est
print(fileSplit (file, updir)) ----> E: est txt E: est.txt
file2RunApp(file,Ext2app) ---> filename, dir name, volume label incorrect / notepad open without open the file given
-- again :
file = "E:\corroder\test.txt"
give error "script Error:[string "-- sample template format: extension -> app c..."] .... invalid escape sequence near '"E:\c' "
|
|
|
Back to top |
|
|
Zanzer I post too much Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Sat Jan 21, 2017 11:00 pm Post subject: |
|
|
Code: | local path = [[c:\temp\test\myfile.txt]]
local dir, name, ext = string.match(path, "(.-)([^\\]-([^%.]+))$")
print(dir)
print(name)
print(ext) |
|
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Sat Jan 21, 2017 11:43 pm Post subject: |
|
|
Work good, thank Zanzer,
Code: | local path = [[c:\temp\test\myfile.txt]]
local dir, name, ext = string.match(path, "(.-)([^\\]-([^%.]+))$")
print(dir) --> c:\temp\test\
print(name) --> myfile.txt
print(ext) --> txt
--- Put on a function
file = [[c:\temp\test\myfile.txt]]
function SplitFilename(strFilename)
return string.match(strFilename, "(.-)([^\\]-([^\\%.]+))$")
end
path,file,extension = SplitFilename(file)
print(path,file,extension) --> c:\temp\test\ myfile.txt txt
print(path) --> c:\temp\test\
print(file) --> myfile.txt
print(extension) --> txt
|
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|