Windows console app can't get current folder

Hello all,

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]

Any ideas where I am going awry?

On what line is the exception raised?

  1. IF any portion of that path does not exist CurrentFolder will be nil
    CurrentFolder = GetFolderItem(“C:\Users\cboyd\Documents\code\tcgfiles\test”)

  2. GetFolderitem should also have its second parameter used like
    CurrentFolder = GetFolderItem(“C:\Users\cboyd\Documents\code\tcgfiles\test”, Folderitem.PathTypeNative)

  3. dont use GetFolderItem("")
    GetFodleritem( name ) is the same as GetFolderitem("").Child( name )

  4. 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

Hi Andrew, sorry for the delayed response.
From what I can tell the exception is raised on this line:
Logfile = CurrentFolder.Child(LogfileName)

Hi Norman ~ thanks for the reply and encouragement not to trust forum code. :slight_smile:

As a clarifying point the debug run works w/o issue.
It is when I compile it and run it that I see problems.

I will play this and let you guys know how it goes.

Again, thanks,

I see that in debug mode you point to a fixed place not next to the console program. That probably explains why it always works.

Are you sure the log file is where it is supposed to be in built mode ? Is it really just next to the Console build in the same folder ?

Hi Michel,

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?

Thanks.

Do not trust all forum recommendations, either. :slight_smile:

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

f = GetFolderItem("")
f = f.Child("xyz")

Hi Tim,

Thanks for clearing that up!

Thanks to everyone’s help and Norman’s real/pseudo code :wink: the compiled object is not throwing out NilObjectExceptions anymore.

[quote=272840:@Craig Boyd]3) dont use GetFolderItem("")
What then should I do if I want the current folder that the exe resides in?[/quote]

No idea why Norman said that. Anyway, it has always worked just fine here.

Alternatively, you can use

f = App.ExecutableFile.Parent

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.

Indeed on Mac that would not work, as executablefile points to the Unix executable in Contents/MacOS within the bundle.

But on Windows as well as Linux, it is the same as GetFolderItem("").

[quote=272840:@Craig Boyd]Hi Michel,

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. :slight_smile:

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