What does "no error" mean when writing to a file in Lua? - file

I am writing a script to add one line in a .txt per video while using MPV.
However, I am getting a weird error on line 68 with the for loop.
It merely tells me: no error. If I add an error parameter to file:write(message, error), it gives me another error message, stating: bad argument #2 to 'write' (string expected, got function). Any help would be appreciated.
function on_file_end(event)
if not paused then totaltime = totaltime + os.clock() - lasttime end
local message = (totaltime .. "s, " .. timeloaded .. ", " .. filename)
local lines = {}
local file = io.open(logpath, "r+")
if file_exists(logpath) then
for l in file:lines() do
if not l:find(message, 1, true) then
lines[#lines+1] = 1
file:write(message)
end
end
file:close()
end
end

bad argument #2 to 'write' (string expected, got function)
error is not an "error parameter" it is a global function that allows to raise your own errors in Lua.
See https://www.lua.org/manual/5.4/manual.html#pdf-error

Related

Lua Script error: bad argument #2 'to open' (invalid mode)

I am trying to create a file or read a already existing file but I keep getting the same error: bad argument #2 'to open' (invalid mode)
player_pos = player.pposition()
world_nam = server.worldName()
attribs = player.attributes()
xp = attribs.id(6).value
timesavename = world_nam .. player_pos .. xp
f = io.open(timesavename, ".txt", "r")
if f ~= nil then
io.input(f)
resultstart = f:read("*line")
resultstop = f:read("*line")
f:close()
end
Using , in a function will make it think its another argument, like in the comment you got you should concat the strings with ..

VB.net web service, Do I still need to Close/Dispose if I call Exit Function before Finally?

I have a VB.net web service that was written a number of years ago and I "think" might have some issues.
I am not a VB.net guy really and have limited knowledge of Close Dispose protocols. Reading got me the following but I am still unclear.
https://stackoverflow.com/questions/4439409/open-close-sqlconnection-or-keep-open
My main concern is the handling of the MS SQL database, potential lock-ups etc. Have seen a few.
The following code is part of one function (truncated code) but you will see there are a number of 'Exit Function' lines.
I assume then that the 'Finally' code will not get executed and therefor that no Close / Dispose etc. will be execute ? The web service processing returns to the calling app after that Exit Function.
Is that an issue, not processing that 'Finally' chunk of code (Close/Dispose) ?
and If so I guess removing the Exit Function lines will address that ?
Or .. will putting a CloseDbConnection() before the Exit Function do just as well.
thanks
ElseIf AppMode = "Update" Then
InPhoneGUID = db.updateInPhoneScanner(returnedUID, AppMode, New List(Of String)(New String() {tmpl1}))
If Not InPhoneGUID = "" Then
r.Message = "PhoneScanner Templates Updated"
' resultList.Add(r) ' Doubling up on the Returned info ?
r.GenUID = InPhoneGUID
resultList.Add(r)
Return GetResponseTextForMobile(resultList)
Exit Function
Else
r.Message = "error 1,PhoneScanner Update Failed"
resultList.Add(r)
Return GetResponseTextForMobile(resultList)
Exit Function
End If
_Logger.Info("=== Invalid Account Type for PHONESCANNER ===")
r.Message = "error 1,Account Addition Inavild Type"
resultList.Add(r)
Return GetResponseTextForMobile(resultList)
Exit Function
End If
End If ' End =========== MAINLINE ROUTINE
_Logger.Info("=== Invalid MODE ===")
r.Message = "error 1,Inavild Mode Sent"
resultList.Add(r)
Return GetResponseTextForMobile(resultList)
Exit Function
End If
End If
Catch ex As Exception
_Logger.Error(ex)
Finally
db.CloseDbConnection()
If fingerPrintHelper IsNot Nothing Then
fingerPrintHelper.Dispose()
End If
db = Nothing
End Try
The db.CloseConnection is as follows ;
Public Sub CloseDbConnection()
Try
mSqlconnection.Close()
mSqlconnection.Dispose()
mSqlconnection = Nothing
Catch ex As Exception
'Throw New Exception("CloseDbConnection : " & ex.Message)
End Try
End Sub

open(create) non existing file in lua

I'm trying to save a configtable(parsed to string) to a file wich is created just in time.
local cfg_string = table.tostring(cfg_table)
local file_name = ""
local cfg_file = ""
file_name = com:line(nil) -- reads a line of input from user via terminal
file_name = string.format("some_prefix-%s-some_suffix.lua",file_name)
-- file does NOT exist at this line
cfg_file = io.open("/dir/subdir/"..file_name,"w")
-- file now should exist
os.syslog(type(file_name)) -> string
os.syslog(type(cfg_file)) -> nil
os.syslog(type(cfg_string)) -> string
cfg_file:write(cfg_string)
cfg_file:write(cfg_string) throws "attempt to call "write" a nil value".
So hu,.. cfg_file is nil I know, but why? I also tried to io.open() with "a" flag, but this doesn't work too. The directory exists!
Thanks for your help.
Actually, my code is working. The error is thrown because missig write-permission to dir.
Everyone with such Errors should try
handle_name, err = io.open(file,"w")
print(err)

dbWriteTable in RMySQL error in name pasting

i have many data.frames() that i am trying to send to MySQL database via RMySQL().
# Sends data frame to database without a problem
dbWriteTable(con3, name="SPY", value=SPY , append=T)
# stock1 contains a character vector of stock names...
stock1 <- c("SPY.A")
But when I try to loop it:
i= 1
while(i <= length(stock1)){
# converts "SPY.A" into SPY
name <- print(paste0(str_sub(stock1, start = 1, end = -3))[i], quote=F)
# sends data.frame to database
dbWriteTable(con3,paste0(str_sub(stock1, start = 1, end = -3))[i], value=name, append=T)
i <- 1+i
}
The following warning is returned & nothing was sent to database
In addition: Warning message:
In file(fn, open = "r") :
cannot open file './SPY': No such file or directory
However, I believe that the problem is with pasting value onto dbWriteTable() since writing dbWriteTable(con3, "SPY", SPY, append=T) works but dbWriteTable(con3, "SPY", name, append=T) will not...
You are probably using a non-base package for str_sub and I'm guessing you get the same behavior with substr. Does this succeed?
dbWriteTable(con3, substr( stock1, 1,3) , get(stock1), append=T)

Groovy - create file issue: The filename, directory name or volume label syntax is incorrect

I'm running a script made in Groovy from Soap UI and the script needs to generate lots of files.
Those files have also in the name two numbers from a list (all the combinations in that list are different), and there are 1303 combinations
available and the script generates just 1235 files.
A part of the code is:
filename = groovyUtils.projectPath + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
targetFile.createNewFile();
where $file is actually that part of the file name which include those 2 combinations from that list:
file = "abc" + "-$firstNumer"+"_$secondNumber"
For those file which are not created is a message returned:"The filename, directory name or volume label syntax is incorrect".
I've tried puting another path:
filename = "D:\\rez\\" + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
targetFile.createNewFile();
and also:
File parentFolder = new File("D:\\rez\\");
File targetFile = new File(parentFolder, "$file"+"_OK.txt");
targetFile.createNewFile();
(which I've found here: What are possible reasons for java.io.IOException: "The filename, directory name, or volume label syntax is incorrect")
but nothing worked.
I have no ideea where the problem is. Is strange that 1235 files are created ok, and the rest of them, 68 aren't created at all.
Thanks,
My guess is that some of the files have illegal characters in their paths. Exactly which characters are illegal is platform specific, e.g. on Windows they are
\ / : * ? " < > |
Why don't you log the full path of the file before targetFile.createNewFile(); is called and also log whether this method succeeded or not, e.g.
filename = groovyUtils.projectPath + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
println "attempting to create file: $targetFile"
if (targetFile.createNewFile()) {
println "Successfully created file $targetFile"
} else {
println "Failed to create file $targetFile"
}
When the process is finished, check the logs and I suspect you'll see a common pattern in the ""Failed to create file...." messages
File.createNewFile() returns false when a file or directory with that name already exists. In all other failure cases (security, I/O) it throws an exception.
Evaluate createNewFile()'s return value or, additionally, use the File.exists() method:
File file = new File("foo")
// works the first time
createNewFile(file)
// prints an error message
createNewFile(file)
void createNewFile(File file) {
if (!file.createNewFile()) {
assert file.exists()
println file.getPath() + " already exists."
}
}

Resources