Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


read files [help]
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Lynxz Gaming
Advanced Cheater
Reputation: 1

Joined: 01 Jul 2017
Posts: 67

PostPosted: Sun Feb 25, 2018 4:12 am    Post subject: read files [help] Reply with quote

how to make read file in current folder??
without a long path thanks Very Happy

_________________
Hello Hitler !
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 619

PostPosted: Sun Feb 25, 2018 9:12 am    Post subject: Reply with quote

1st script :
Code:
local open = io.open
local function read_file(path,file)
    local file = open(file, "rb")
    if not file then return nil end
    local content = file:read "*a"
    file:close()
    return content
end

path = TrainerOrigin or getMainForm().OpenDialog1.InitialDir
path = Path..'\\'
local fileContent = read_file(path, 'Lynxz Gaming.txt')   --- change to filename you want to open and read
print(fileContent);


NOTE : put on same directory / path you want to open and read.


2nd script :
Code:
function GetFileName(f)
 local str = f
 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 getfile()
 load_dialog = createOpenDialog(self)
 load_dialog.InitalDir = os.getenv('%USERPROFILE%')
 load_dialog.execute()
 file = load_dialog.FileName
 fl = GetFileName(file)
 if fl == nil then
 return file
 else
 local open = io.open
 local file2read = open(fl, "rb")
 local content = file2read:read "*a"
 file2read:close()
 print(content)
 return content
 end
end

getfile()


It's up to you which one you want t use.... LOL... LOL... Very Happy Very Happy Very Happy

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater
Reputation: 32

Joined: 09 Aug 2013
Posts: 769

PostPosted: Sun Feb 25, 2018 11:28 am    Post subject: Reply with quote

I feel like GetFileName could be replaced with
Code:
function GetFileName(f) return f:match('.*\\(.*)%.') end


actually, it can't because your GetFileName works exclusively with '/' not '\', also it includes the extension but that's a trivial change.
Code:
function GetFileName(f) return f:match('.*[/\\](.*%..*)') end


Though for the same functionality I think this is slightly simpler (can't guarantee it's any more efficient but it seems like it should be)
Code:
function SkipSlash(f)
  local last = 0
  local slash = last
  repeat
    slash = last
    last = file:find('/', last+1)
  until not last
  return file:sub(slash+1)
end
the actual shortest would probably be
Code:
function SkipSlash(f)
  local r = f:reverse()
  local n = r:find('/') or 0
  return r:sub(0,n-1):reverse()
end
but it's going to duplicate the string to reverse it and then the filename twice (once reversed once unreversed)... actually now that I think about it I'm not sure if lua can reuse substrings... it's possible since they're immutable, I'm just not sure if it does.

The 2nd would still be longer than the first but then it is doing a bit more.

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
Aylin
Expert Cheater
Reputation: 2

Joined: 16 Feb 2017
Posts: 154

PostPosted: Sun Feb 25, 2018 11:36 am    Post subject: Reply with quote

@Corroder Master, do you code this file open locally?
in Trainer (AddFile) with a file open button. Wink
But can this file be an .exe?
Files are hosted in Trainer and opened with a command.
Can I ask for a coding?

_________________
See Hitler ! This Trainer is for you!
http://forum.cheatengine.org/viewtopic.php?t=606786
Enthusiastic people: Always one step ahead!
Do not underestimate me Master: You were a beginner in the past!
Back to top
View user's profile Send private message MSN Messenger
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 619

PostPosted: Sun Feb 25, 2018 6:39 pm    Post subject: Reply with quote

@freeER, yes..function getFileName(f) ; has contain code to manipulating escape character sequences, while this '/' or '\' considered as invalid characters by lua.

Actually, as the topic wish to read a file without need to write any long path name, then this script also work without getFileName(f) function.

Code:
load_dialog = createOpenDialog(self)
load_dialog.InitalDir = os.getenv('%USERPROFILE%')
load_dialog.execute()
file = load_dialog.FileName

local file2read = io.open(file, "rb")
local content = file2read:read "*a"
file2read:close()
print(content)
return content


So, @Aylin...yes you can add the code to a button 'Open File' and execute that file if that file(s) is an application (exe, bat, msi, etc...)

Code:
function executeFile(f_app)
 load_dialog = createOpenDialog(self)
 load_dialog.InitalDir = os.getenv('%USERPROFILE%')
 load_dialog.Filter = 'Executable files|*.EXE;*.BAT;*.CMD;*.sh|Bat files (*.bat)|*.BAT|Exe files (*.exe)|*.EXE|All files (*.*)|*'
 load_dialog.execute()
 file = load_dialog.FileName
 f_app = file:match('.*\\(.*)%.')   --- use freeER code
 shellExecute(f_app)
end

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 14

Joined: 24 Feb 2017
Posts: 441
Location: 'Merica

PostPosted: Sun Feb 25, 2018 9:03 pm    Post subject: This post has 1 review(s) Reply with quote

Here's how I get the path separator.
Code:
local pathseparator = package.config:sub(1,1)


And I just use it with this.
Code:
function getPath( ... )
   local pathseparator = package.config:sub(1,1)
   local elements = { ... }
   return table.concat(elements, pathseparator)
end

_________________
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 619

PostPosted: Mon Feb 26, 2018 12:38 am    Post subject: Reply with quote

Ah...thank @tim..
But in case eliminating \ concatenating path name, then path name must be write with '\\' or '/' to make lua accept the path name.

Your script :
Code:
function getPath( ... )
   local pathseparator = package.config:sub(1,1)
   local elements = { ... }
   return table.concat(elements, pathseparator)
end

a= 'E:\tester.txt'
print(getPath(a))    ---> result : E:       ester.txt'

a = 'E:\\tester.txt '
print(getPath(a))    ---> result : E:\tester.txt'

a = 'E:'/tester.txt '
print(getPath(a))    ---> result : E:/tester.txt'

--- note : by default in windows, path name should display as '[dir name] ..\.. [sub dir name]....\..[file name]'



However with your code also will give same result. So, this is another optional to get a file from a path (and execute it).

Code:
function getPath( ... )
   local pathseparator = package.config:sub(1,1)
   local elements = { ... }
   return table.concat(elements, pathseparator)
end

function executeFile(f_app)
 load_dialog = createOpenDialog(self)
 load_dialog.InitalDir = os.getenv('%USERPROFILE%')
 load_dialog.Filter = 'Executable files|*.EXE;*.BAT;*.CMD;*.sh|Bat files (*.bat)|*.BAT|Exe files (*.exe)|*.EXE|All files (*.*)|*'
 load_dialog.execute()
 file = load_dialog.FileName
-- f_app = file:match('.*\\(.*)%.')   --- use freeER code
 f_app = getPath(file)  --use TheyCallMeTim13 code
 shellExecute(f_app)
end

executeFile(f_app)

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 14

Joined: 24 Feb 2017
Posts: 441
Location: 'Merica

PostPosted: Mon Feb 26, 2018 2:08 am    Post subject: Reply with quote

Corroder wrote:
...


the getPath is meant for concatenation of a file path, so:
Code:
getPath('e:', 'someFolder', 'somefile.txt')

_________________
Back to top
View user's profile Send private message
Aylin
Expert Cheater
Reputation: 2

Joined: 16 Feb 2017
Posts: 154

PostPosted: Mon Feb 26, 2018 7:35 am    Post subject: Reply with quote

Corroder wrote:

So, @Aylin...yes you can add the code to a button 'Open File' and execute that file if that file(s) is an application (exe, bat, msi, etc...)

Code:
function executeFile(f_app)
 load_dialog = createOpenDialog(self)
 load_dialog.InitalDir = os.getenv('%USERPROFILE%')
 load_dialog.Filter = 'Executable files|*.EXE;*.BAT;*.CMD;*.sh|Bat files (*.bat)|*.BAT|Exe files (*.exe)|*.EXE|All files (*.*)|*'
 load_dialog.execute()
 file = load_dialog.FileName
 f_app = file:match('.*\\(.*)%.')   --- use freeER code
 shellExecute(f_app)
end


I uploaded a simple file (CT)
With 'addFile' I put the file I want to open into. Smile
I want to open this file with a Button.
The file will not be dropped on the table, it will stay in the trainer.
It will be activated as Local.
Also Corrod: I request an exception. Sad
Look into CT, please. You have such a file (1by1) and
We have processed a forum with 40 posts.
You should have suggested 1by1. Maybe it will get better results. Wink

https://www.dropbox.com/s/k2teynwb8qt1pet/OpenLocalFile.CT?dl=0

_________________
See Hitler ! This Trainer is for you!
http://forum.cheatengine.org/viewtopic.php?t=606786
Enthusiastic people: Always one step ahead!
Do not underestimate me Master: You were a beginner in the past!
Back to top
View user's profile Send private message MSN Messenger
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 619

PostPosted: Mon Feb 26, 2018 7:16 pm    Post subject: Reply with quote

Aylin
Quote:
With 'addFile' I put the file I want to open into. Smile
I want to open this file with a Button.
The file will not be dropped on the table, it will stay in the trainer.


I don't think CE can open and execute an exe file has store via add file to CE Table or an exe file as a stream file.
Even we change that exe file as sting binary file. Windows not allow to execute a bin file easy without some parameters needed.

But we still do a trick to execute exe file has store as stream file, in this case is a pure exe file without any lib files, or other files etc.

Here is the trick :

Code:
function CEButton1Click(sender)     ---- CEButton1 event to auto execute player exe
 path = TrainerOrigin or getMainForm()
 player_1by1 = findTableFile('player.exe').Stream
 player_path = path..'\\player.exe'
 player_1by1.saveToFile(player_path)
 player_1by1.destroy()
 os.execute('player.exe')
end

function remove_exit()    --- remove player exe  and player.ini from path when CE close
 player_path = TrainerOrigin or getMainForm()
 player_file1 = player_path..'\\player.exe'
 player_file2 = player_path..'\\player.ini'
 os.remove(player_file1)
 os.remove(player_file2)
 closeCE()
 return caFree
end

MainForm.OnClose = remove_exit
form_show(UDF1)


Note : I am not make a function to check if file player.exe exist on path or not, i think it doesn't need. And player.exe must be close first before close CE.

EDIT : you can use os.execute('taskkill /im player.exe /f') to force kill player.exe process if it still running

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
Aylin
Expert Cheater
Reputation: 2

Joined: 16 Feb 2017
Posts: 154

PostPosted: Mon Feb 26, 2018 9:19 pm    Post subject: Reply with quote

Coding opens with a way cmd.exe.
Is this call in the encoding? Rolling Eyes
also it needs to establish a OnTimer before closing the player.
Without the player.ini the file does not close.
I've arranged a try.
Thanks also for coding.
If you are aware, this encoding will close the MP3 Player project. Wink

Sorry.. Smile
EDIT: the following code is functioning properly.
But at Player opening,
It opens with cmd.exe and cmd.exe remains. Rolling Eyes

Code:
function CEButton1Click(sender)     ---- CEButton1 event to auto execute player exe
 path = TrainerOrigin or getMainForm()
 player_1by1 = findTableFile('player.exe').Stream
 player_path = path..'\\player.exe'
 player_1by1.saveToFile(player_path)
 player_1by1.destroy()
 os.execute('player.exe')
end

function CEButton2Click(sender)
UDF1.CETimer1.Enabled = true
end
function CETimer1Timer(sender)
os.execute('taskkill /im player.exe /f')   --- remove player exe  and player.ini from path when CE close
UDF1.CETimer2.Enabled = true
end
function CETimer2Timer(sender)
UDF1.CETimer1.Enabled = false
 player_path = TrainerOrigin or getMainForm()
 player_file1 = player_path..'\\player.exe'
 player_file2 = player_path..'\\player.ini'
 player_file3 = player_path..'\\1by1rss.XML'
 os.remove(player_file1)
 os.remove(player_file2)
 os.remove(player_file3)
 closeCE()
 return caFree
end

UDF1.OnClose = remove_exit
form_show(UDF1)
UDF1.CETimer1.Enabled = false
UDF1.CETimer2.Enabled = false



https://www.dropbox.com/s/qsw0ldmv1ak5ucm/OpenLocalFile-11.CT?dl=0

_________________
See Hitler ! This Trainer is for you!
http://forum.cheatengine.org/viewtopic.php?t=606786
Enthusiastic people: Always one step ahead!
Do not underestimate me Master: You were a beginner in the past!
Back to top
View user's profile Send private message MSN Messenger
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 619

PostPosted: Mon Feb 26, 2018 11:10 pm    Post subject: Reply with quote

I don't know what are you trying to do with add CETimer on your form.
We need not to make something simple to be complex.
However, this script below trying to meet what you need.

Code:
function CEButton1Click(sender)
 player_path = TrainerOrigin or getMainForm()
 player_1by1 = findTableFile('player.exe').Stream
 player_path = player_path..'\\player.exe'
------ check if player.exe already ran or not
------ then if user click 'open' button again when player exe already ran, it will not give error access violation
 filedata = io.popen("tasklist /NH /FO CSV /FI \"IMAGENAME eq "..player_path.."\"")
 output = filedata:read()
 filedata:close()
 if output ~= "INFO: No tasks are running which match the specified criteria." then
------ Program is running. Close the program
    os.execute("taskkill -im"..player_path)
 else
--- -- Program is not running
 player_1by1.saveToFile(player_path)
 --player_1by1.destroy()    --- this no need when use shellExecute
 shellExecute(player_path)  ---
 os.execute('Exit')  --- close windows CMD pop-up
 end
end

function CEButton2Click(sender)
 player_path = TrainerOrigin or getMainForm()
 player_file1 = player_path..'\\player.exe'
------ check if player.exe already ran or not
------ in case, if player.exe still running, then we can not delete it
 filedata = io.popen("tasklist /NH /FO CSV /FI \"IMAGENAME eq "..player_file1.."\"")
 output = filedata:read()
 filedata:close()
 if output ~= "INFO: No tasks are running which match the specified criteria." then
------ Program is running. Close the program
    os.execute("taskkill -im"..player_file1)
 else
------ Program is not running
  player_file2 = player_path..'\\player.ini'
  player_file3 = player_path..'\\1by1rss.xml'
  os.remove(player_file1)
  os.remove(player_file2)
  os.remove(player_file3)
  closeCE()
  return caFree
 end
end

---MainForm.OnClose = remove_exit    ---- this is no need since you gave option to close CE via CLOSE button
form_show(UDF1)

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
Aylin
Expert Cheater
Reputation: 2

Joined: 16 Feb 2017
Posts: 154

PostPosted: Tue Feb 27, 2018 7:07 am    Post subject: Reply with quote

Code:
function CEButton1Click(sender)
 player_path = TrainerOrigin or getMainForm()
 player_1by1 = findTableFile('player.exe').Stream
 player_path = player_path..'\\player.exe'
------ check if player.exe already ran or not
------ then if user click 'open' button again when player exe already ran, it will not give error access violation
 filedata = io.popen("tasklist /NH /FO CSV /FI \"IMAGENAME eq "..player_path.."\"")
 output = filedata:read()
 filedata:close()
 if output ~= "INFO: No tasks are running which match the specified criteria." then
------ Program is running. Close the program
   os.execute("taskkill -im"..player_path)
-- (Timer Start?)
-- else
end --
end --
function CEButton2Click(sender) -- Timer1???
 player_path = TrainerOrigin or getMainForm() --
 player_1by1 = findTableFile('player.exe').Stream --
 player_path = player_path..'\\player.exe'
--- -- Program is not running
 player_1by1.saveToFile(player_path)
--player_1by1.destroy()    --- this no need when use shellExecute
 shellExecute(player_path)  ---
--os.execute('Exit')  --- close windows CMD pop-up
 end
--end

function CEButton3Click(sender)
 player_path = TrainerOrigin or getMainForm()
 player_file1 = player_path..'\\player.exe'
------ check if player.exe already ran or not
------ in case, if player.exe still running, then we can not delete it
 filedata = io.popen("tasklist /NH /FO CSV /FI \"IMAGENAME eq "..player_file1.."\"")
 output = filedata:read()
 filedata:close()
 if output ~= "INFO: No tasks are running which match the specified criteria." then
------ Program is running. Close the program
--    os.execute("taskkill -im"..player_file1)
    os.execute('taskkill /im player.exe /f')
-- else
------ Program is not running
  player_file2 = player_path..'\\player.ini'
  player_file3 = player_path..'\\1by1rss.xml'
  os.remove(player_file1)
  os.remove(player_file2)
  os.remove(player_file3)
  closeCE()
  return caFree
 end
end

---MainForm.OnClose = remove_exit    ---- this is no need since you gave option to close CE via CLOSE button
form_show(UDF1)


Buttons did not detect the end of "else".
the current encoding is working,
but I added one more button.
button 2 must be; opening and closing.
Your last code is: closing CMD and open program,
but it did not record the program and did not open it.
I edited it assuming that the flow was prevented with "else".

or a Timer for openPlayer. 2 Buttons, 1 Timer?
you are angry with the use of the Timer, Smile but it may be good for the flow. Wink

we are reaching the result. Thanks for your help and patience master @Corroder

_________________
See Hitler ! This Trainer is for you!
http://forum.cheatengine.org/viewtopic.php?t=606786
Enthusiastic people: Always one step ahead!
Do not underestimate me Master: You were a beginner in the past!
Back to top
View user's profile Send private message MSN Messenger
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 619

PostPosted: Tue Feb 27, 2018 10:26 am    Post subject: Reply with quote

Quote:
Buttons did not detect the end of "else".


Yes, this... 'else'
....
Code:
    os.execute('taskkill /im player.exe /f')
-- else 
------ Program is not running
  player_file2 = player_path..'\\player.ini'
....


must be mistyping while wrote code to this post, because on my CT it's 'else' not '-- else'.

Quote:
Your last code is: closing CMD and open program,
but it did not record the program and did not open it.


Not really understood what mean of ' it did not record the program and did not open it'.

Quote:
you are angry with the use of the Timer, Smile but it may be good for the flow. Wink


Why I must get angry. for what reasons ?.
I said 'We need not to make something simple to be complex', it's mean in connection to programming style) :

1. Try to get and use the easiest way to reach the goal following by some aspects.
example : in CE

Code:
control_setCaption.UDF1.CELabel1('Hi I am use cheat engine')   --- this is writing on classic CE style.
-- this same to :
UDF1.CELabel1.Caption = 'Hi I am use cheat engine'
-- or
lbl1 = UDF1.CELabel1
-- then simply we can use in entire script
lb1.Caption = 'Hi I am use cheat engine'
lb1.Color = '0xFFFFFF'
-- etc...etc


Note this : Every characters / letters used on scripts will counting as 1 byte. So, consider this, how much CT file size will grow up if not typing script code in efficient way ?. And also there are many reasons to be considering such as add stream files, images, etc to a CT file. Who want get or download a CT file with file size 50 MB's or over ?

2. In case make a CE Trainer, such as other programming languages, if you plan to publish your work, then try to place yourself as user side who shall use your work, except you will use it personally then doesn't matter what you do.

Look to my last script above, there are several notes there. That notes present what I thought as a user.
The logic is 'What if'. example in your case : 'What if user click Open Button again while user already clicked it for first time ?.' Also, I gave you option about event when close your form (via button click or what ever). The option is to delete player.exe, player.ini, etc from path, which created when click open button. Did you think this case ?. That is example called with 'What if'. And other more examples that you can think by yourself.

3. Make a CE Trainer, such as other programming languages, I believe and use only property required by the project. For example :
I will not add a Timer for open or close a external program/app if not important or for some reasons. I will do just 'closeCE + parameters' and will not make user waiting without clearly reasons. I more like use 'showMessage('message', mbOK) to give user choise sure to close or not....
The first thing that I'm thinking when open/close an app is : Is this safe ?. or Is this close properly without left any unnecessary files on my machine ?. Am I sure no process still running on the background ?.

Many other thing if want to say but it will make a long talk and I don't need it. So finally, what I've trying to is to tell you a good. But if then you think I was angry then I am not. And if my critic for your job is hard to accept by you, then leave it. Because at the last it's your decision.

Cheers...

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
Aylin
Expert Cheater
Reputation: 2

Joined: 16 Feb 2017
Posts: 154

PostPosted: Tue Feb 27, 2018 12:06 pm    Post subject: Reply with quote

People do not mind the online advertising pages that are opened with Timer for hours.
or a trainer of 30 game titles is 6 MB.
For them; Impact and result are important.
For me; It's a little more fun and visual.
Both sides are satisfied with the results.
Last Trainer 3500 lines and 6 MB
Nobody is looking at it. On the contrary, entertainment is multiplying.
Perhaps; We are thinking too much about people who use
It's driving us away from the fun. Rolling Eyes
Instead of pausing for 2 more years, I'd rather have fun and continue

The "-else" topic:
Every 2 Buttons did not read codes after "else", did not apply.
However, if you disabled the "else" character, the entire code worked.
"-" reminder was in this direction.

My purpose is not to test your patience. Translation errors makes me talkative.
You and @FreeER, you have an effort on me.
If I have a fault with your, forgive me. Rolling Eyes

_________________
See Hitler ! This Trainer is for you!
http://forum.cheatengine.org/viewtopic.php?t=606786
Enthusiastic people: Always one step ahead!
Do not underestimate me Master: You were a beginner in the past!
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites