Lua wait for log file to be closed - lua

I have a script that is executed via a function LuaExportStop() that is called when a program is stopped.
The same function is used by other scripts. One of those other scripts writes and closes a log file when that function is called. I want to import that log file, parse the contents and write the results to another file or possibly to a MySql DB down the road. I have accomplished the code to do this with a static copy of the file in question.
I believe the problem I am having is that both scripts execute with the same function at the same time so an error is thrown when my script tries to access a file that is open and/or being written.
My question is can I have my script "wait" until the file has been closed to execute? How would this be implemented around this code?
luaExportStop()
local filePath = "h:\\somepath\\stats.log"
newFile = io.open(filePath, "w")
dofile("this is the log file")
newFile:write(pp(stats))
newFile:close()
end

Related

Moho, run script and get ScriptInterface instance

I need to run Moho lua script which created new document, import some files etc., by command line.
The problem is: to call this function requered ScriptInterface instance. It may called by mouse clicking, by menu selection in Moho ui (hand made calling) and sending ScriptingInterface instance as function parameter, like script:Run(moho). If I trying to call this function from commandline this instance not set as parameter.
So the question is - can I take ScriptInterface instance from some global vars or from somewhere else ??
You can prepare a special .moho file, which includes a layer with embed lua script. If you run command line opening Moho with this special file in it, it will execute the layer script (because it is executed on every new frame enter) and script will receive ScriptInterface instance as moho argument given to its LayerScript function.

Unable to change to previous io input

i am currently trying to make a small script that temporarily changes the io input to a file and then back to the previews input. i have tried searching for solutions and all i found was this from lua.org
local temp = io.input() -- save current file
io.input("newinput") -- open a new current file
... -- do something with new input
io.input():close() -- close current file
io.input(temp) -- restore previous current file
however when i attempt this i get this error
Bad Argument #1 (string or file expected, got table):
i am relatively new to lua so any help would be outstanding
Thanks.

Run script instructions from external source in Lua

Ok, I'm wanting to know if there's a way of running scripts from an external source with Lua. Be it another file in .txt format or from pastebin, what have you, and run the code and wait for said code to finish, and then continue on with the rest of the function. I'm not quite sure at all about how it'd work, but this is basically the idea I'm going by and isn't actual code.
function runStuff()
print("checking for stuff")
run.script(derp.txt)
wait for script
if run.script == finished
continue program
elseif nil
print("looks like this script sucks.")
end
end
runStuff()
And for example what "derp.txt" contains is:
function lookFile()
if herp.txt exists then
rename herp.txt to herp.lua
else
nil
end
end
lookFile()
I'm still new to Lua so I'm coding like a moron here, but you get my picture hopefully. I'm working on a project that'll act like an installer package for repositories based from pastebin or anywhere else that'll supply raw format outputs of lua scripts and I'm going to use that idea for it to call on scripts to run externally. So when I supply a "First Time Run" version of the program, it'll call out to a lua script that'll call to another lua script and install that script, then close.
This is for minecraft, mind you. ComputerCraft made me take interest in Lua, but anyway, hopefully you got the gist of what I'm trying to figure out. Hopefully that's doable and if not, I'll just have to figure something else out.
To load and execute a Lua code fragment you can use something like this:
local http = require("socket.http")
local response, err = http.request("url to some Lua code")
if not response then error(err) end
local f, err = (loadstring or load)(response)
if not f then error(err) end
print("done with "..response)
-- make sure you read on (in)security implications of running remote code
-- f() -- call the function based on the remote code
You probably need to use sandboxing.

Lua check if a file is open or not

I'm trying to script a lua file to check if a certain file is open. Then I want it to close that file if it is open. I know how to check if the file exist but I need to know how to check if the file is open, meaning the file is running.
Lua, like C, C++, and pretty much every other language, can only close files that it opens itself. You cannot close files open by other people (not with standard Lua calls); this would be incredibly rude.
So you can't test to see if a file is opened by someone else. Nor can you close their file. There may be system API calls you could make to do this, but you would have to give Lua scripts access to those APIs yourself. Lua's standard libraries can't do this.
Sounds like you want to check which if any programs have a given file open.
first thing that comes to mind is parsing the output of lsof on linux.
fd = io.popen("lsof path/to/my/file")
fileopened = (#fd:read("a*") > 0)
Kind of a hacky way to do it, but it works:
processname = "process_name_here.exe"
filedata = io.popen("tasklist /NH /FO CSV /FI \"IMAGENAME eq "..processname.."\"")
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 "..processname)
else
-- Program is not running
end
Just make sure to replace "process_name_here.exe" with the process name that shows up in task manager
Alternatively you can just use this to close it without checking if it was actually running:
os.execute("taskkill -im process_name_here.exe")

How to execute a .bat file from a C# windows form app?

What I need to do is have a C# 2005 GUI app call a .bat and several VBScript files at user's request. This is just a stop-gap solution until the end of the holidays and I can write it all in C#. I can get the VBScript files to execute with no problem but I am unable to execute the .bat file. When I "click" in the C# app to execute the .bat file a DOS window opens up and closes very fast and the test .bat file does not execute - "Windows does not recognize bat as an internal or external command" is the error returned in the DOS box. If I simply doubl-click the .bat file or manually run it from the command prompt it does execute. I also need the .bat file to execute silently unless a user interaction is required - this script copies 11k+ files to folders on a networked machine and occasionally Windows "forgets" if the destination is a file or directory and asks for the user to tell it what it is (this is a whole other issue not for discussion here...needless to say I am annoyed by it).
So far in my C# source I have this :
Process scriptProc = new Process();
if (File.Exists("c:\\scripts\\batchfile1.bat"))
{
scriptProc.StartInfo.FileName = #"cscript";
scriptProc.StartInfo.Arguments = ("cmd.exe", "/C C:\\scripts\\batchfile1.bat"); // Wacky psuedo code //
scriptProc.Start();
scriptProc.WaitForExit(1500000);
scriptProc.Close();
}
if (!File.Exists("c:\\scripts\\batchfile1.bat"))
{
}
I am aware that this code does not work - but it is essentially what I want it to do. What I am looking at is something like this for .bat files. I assume I have to tell the system to use cmd to run the .bat. I am at a loss as to how to do this. I have checked out this site which is for C# 2003. Not much help for me as I am very green with C#.
EDIT: Using Kevin's post I attempted it again. Same solution script from that post but modified for me since I do not need to redirect:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "C:\\scripts\\batchfile1.bat";
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
Here is what you are looking for:
Service hangs up at WaitForExit after calling batch file
It's about a question as to why a service can't execute a file, but it shows all the code necessary to do so.
For the problem you're having about the batch file asking the user if the destination is a folder or file, if you know the answer in advance, you can do as such:
If destination is a file:
echo f | [batch file path]
If folder:
echo d | [batch file path]
It will essentially just pipe the letter after "echo" to the input of the batch file.

Resources