I am sure I am missing something simple and obvious, but I need another set of eyes to tell me what it is…
I have a Windows console app that seems to have problems getting the current folder. I get a NilObjectException.
Here is the code:
[code]ArchiveDirFound = False #If DebugBuild then
CurrentFolder = GetFolderItem(“C:\Users\cboyd\Documents\code\tcgfiles\test”) #Else
CurrentFolder = GetFolderItem("") #Endif
WorkName = “file_manager_” + CurrDate.SQLDateTime + “.log”
LogfileName = WorkName.ReplaceAll(":","_")
If CurrentFolder <> Nil AND CurrentFolder.Exists then
Logfile = CurrentFolder.Child(LogfileName)
Else
print(“could not get current folder”)
quit
End if
Try
WriteStream = TextOutputStream.Create(Logfile)
Catch err as IOException
strError = err.Message
End Try[/code]
IF any portion of that path does not exist CurrentFolder will be nil
CurrentFolder = GetFolderItem(“C:\Users\cboyd\Documents\code\tcgfiles\test”)
GetFolderitem should also have its second parameter used like
CurrentFolder = GetFolderItem(“C:\Users\cboyd\Documents\code\tcgfiles\test”, Folderitem.PathTypeNative)
dont use GetFolderItem("")
GetFodleritem( name ) is the same as GetFolderitem("").Child( name )
you use Child on something that MAY NOT have children (if test is not a dir)
maybe something like
ArchiveDirFound = False
WorkName = "file_manager_" + CurrDate.SQLDateTime + ".log"
LogfileName = WorkName.ReplaceAll(":","_")
#If DebugBuild then
CurrentFolder = GetFolderItem("C:\\Users\\cboyd\\Documents\\code\\tcgfiles\\test", FolderItem.PathTypeNative)
If CurrentFolder <> Nil AND CurrentFolder.Exists and CurrentFolder.Directory then
Logfile = CurrentFolder.Child(LogfileName)
end if
#Else
Logfile = GetFolderitem(LogfileName)
#Endif
if logfile = nil or logfile.exists = false or logfile.directory then
print("could not get log file")
quit
End if
Try
WriteStream = TextOutputStream.Create(Logfile)
Catch err as IOException
strError = err.Message
End Try
oh and never trust “forum code”
that seems about right but I am just writing it in here so its untested
Yes, that is intentional on my part to avoid the whole “now you see me, now you don’t” debug folder. That way the log files persist past the debug session and I can see a) that they are working and b) what they captured.
Yes, I am sure the log file should be with the executable.
So I do have a follow up question(for whomever): Norman explicitly stated : [quote]3) dont use GetFolderItem("")[/quote]
What then should I do if I want the current folder that the exe resides in?
In your original code, GetFolderItem("") is perfectly acceptable. I think what you got was a knee-j erk reaction based on the myriad examples where it is completely unnecessary. I can’t count how many times I have seen code like
I don’t think that’s always the same result. On a Mac, isn’t GetFolderItem("") the root of the package, whereas ExecutableFile.Parent is somewhere inside the package?
Edit: yeah, I guess that is a little pedantic when posted on a Windows thread. Sorry.
Yes, that is intentional on my part to avoid the whole “now you see me, now you don’t” debug folder. That way the log files persist past the debug session and I can see a) that they are working and b) what they captured.
Yes, I am sure the log file should be with the executable.
So I do have a follow up question(for whomever): Norman explicitly stated :
What then should I do if I want the current folder that the exe resides in?
Thanks.[/quote]
Well the way you’re using it ends up being GetFolderItem("").Child(filename)
So just do GetFolderItem(filename)
[quote=272846:@Tim Hare]Do not trust all forum recommendations, either.
In your original code, GetFolderItem("") is perfectly acceptable. I think what you got was a knee-j erk reaction based on the myriad examples where it is completely unnecessary. I can’t count how many times I have seen code like
[/quote]
No - read the code for the NOT debug build case
It basically does GetFolderitem("").Child()
#If DebugBuild then
CurrentFolder = GetFolderItem("C:\\Users\\cboyd\\Documents\\code\\tcgfiles\\test")
#Else
CurrentFolder = GetFolderItem("") // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#Endif
WorkName = "file_manager_" + CurrDate.SQLDateTime + ".log"
LogfileName = WorkName.ReplaceAll(":","_")
If CurrentFolder <> Nil AND CurrentFolder.Exists then
Logfile = CurrentFolder.Child(LogfileName) // <<<<<<<<< == GetFolderItem("") .Child(LogfileName)
Else
print("could not get current folder")
quit
End if