writing file in %TEMP% fails silently

11,323

Solution 1

The problem was loglevel checking. It never actually got to the open or print statements.

I had three log levels defined 0 = no log 1 = everything 2 = errors only

It was set to 2 And LogError called LogMessage which checked if loglevel <> 1 and thus never ran.

Solution 2

Try this

Sub GetTmpPath()
    'This will give the Temp Path
    MsgBox IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))
End Sub

So you can try using it as

    Ret = IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))

    g_strLogPath = Ret & "\Sample.Log"

    Open g_strLogPath For Append As #intFileNumber
Share:
11,323
Will Rickards
Author by

Will Rickards

Software Developer

Updated on July 14, 2022

Comments

  • Will Rickards
    Will Rickards almost 2 years

    I have this function which writes to my log file. It is failing silently. In that there are no errors raised in this function, it just fails to create or write to the file. I'm trying to write to %TEMP%\myappname.log. It also fails in %USERPROFILE%\Desktop\myappname.log. Server is Windows Server 2008 R2 Standard. I've encountered this with writing to the application folder with other programs so moved to writing to the %TEMP% directory and this solved it. But this system won't even let me write to the %TEMP% directory. Yes I tried running as administrator too and it didn't help. %TEMP% in this case resolves through ExpandEnvironmentStrings to C:\Users\sa\AppData\Local\Temp\2 So g_strLogPath is C:\Users\sa\AppData\Local\Temp\2\myappname.log.

    Public Function LogMessage(ByVal strInput As String, Optional ByVal blnDate As Boolean = False) As Boolean
    
       Dim intFileNumber As Integer
    
       On Error GoTo ErrorHandler
    
       If g_lngLogLevel <> 1 Then
          Exit Function
       End If
    
       If Len(g_strLogPath) = 0 Then
          SetLogPath
       End If
    
       If blnDate Then
          strInput = Format(Now, cstrLogDateFormat) & " : " & strInput
       End If
    
       intFileNumber = FreeFile
       Open g_strLogPath For Append As #intFileNumber
       Print #intFileNumber, strInput
       Close #intFileNumber
    
       LogMessage = True
    
       Exit Function
    
    ErrorHandler:
    
       MsgBox _
          "Error: " & Err.Number & vbCrLf & _
          "Location: Module1.LogMessage" & vbCrLf & _
          "Line: " & Erl & vbCrLf & _
          Err.Description, vbExclamation + vbOKOnly
    
    End Function
    
  • Will Rickards
    Will Rickards almost 12 years
    User is local to the machine (not a network account). User is member of local administrators group. It should have full permissions. I can install programs just fine.
  • Will Rickards
    Will Rickards almost 12 years
    This function seems to return a blank string. I use ExpandEnvironemntStrings with %TEMP% to get the temp path. It works fine. Problem isn't getting the temp location it is writing to it. The app can't write to it, user can write to it.
  • Siddharth Rout
    Siddharth Rout almost 12 years
    Msgbox TempPath & "Sample.txt" If you put this in the Sub TmpPath() what do you get? and then try to use it as Open g_strLogPath For Append As #intFileNumber What happens?
  • Will Rickards
    Will Rickards almost 12 years
    A blank string is returned by TempPath
  • Siddharth Rout
    Siddharth Rout almost 12 years
    What does this give you? MsgBox IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))?
  • Will Rickards
    Will Rickards almost 12 years
    C:\Users\sa\AppData\Local\Temp\2
  • Will Rickards
    Will Rickards almost 12 years
    Got the TempPath function to work. It outputs: C:\Users\sa\AppData\Local\Temp\2\
  • Siddharth Rout
    Siddharth Rout almost 12 years
    Gr8. You can use either. Just ensure that you check what the last character is. If it is "\" then you don't add the "\" to the file name.
  • Will Rickards
    Will Rickards almost 12 years
    I think you're missing the point. I have no trouble getting the temp path to write to. All your methods just give me the same path I was getting before. But actually writing the file is failing, without any error. I suspect some policy on the machine is causing it.
  • Siddharth Rout
    Siddharth Rout almost 12 years
    Can you step through the code? and the moment it passes the line Open g_strLogPath For Append As #intFileNumber open that temp folder and check if the file is created? Also check what is the value of g_strLogPath
  • Will Rickards
    Will Rickards almost 12 years
    Couldn't step through but put msgbox right after. Found problem was with loglevel checking. Thanks
  • Deanna
    Deanna almost 12 years
    Normal practice is to have the logging levels on order of verbosity. You seem to have "everything" and "errors only" swapped.
  • Deanna
    Deanna almost 12 years
    Antivirus apps are well known to knobble writes to the temporary folder.
  • wqw
    wqw almost 12 years
    @Deanna: Doubt real-time protection treats TEMP folder differently. OP is using remote desktop (that's why he gets 1, 2 and so on at the end of TEMP) and probably has some wrong settings on RDS, group policy or file permissions.
  • Deanna
    Deanna almost 12 years
    @wqw: McAffee at least had the option to disable executables in the %TEMP% folder, and that's far fromt he worst I've seen.
  • Will Rickards
    Will Rickards almost 12 years
    I never want to turn logging of errors off so I just switched it to a boolean verbose logging flag. And only output the stack trace type stuff when verbose but output the errors always.