writing file in %TEMP% fails silently
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
Comments
-
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 throughExpandEnvironmentStrings
toC:\Users\sa\AppData\Local\Temp\2
So g_strLogPath isC:\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 almost 12 yearsUser 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 almost 12 yearsThis 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 almost 12 years
Msgbox TempPath & "Sample.txt"
If you put this in theSub TmpPath()
what do you get? and then try to use it asOpen g_strLogPath For Append As #intFileNumber
What happens? -
Will Rickards almost 12 yearsA blank string is returned by TempPath
-
Siddharth Rout almost 12 yearsWhat does this give you?
MsgBox IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))
? -
Will Rickards almost 12 yearsC:\Users\sa\AppData\Local\Temp\2
-
Will Rickards almost 12 yearsGot the TempPath function to work. It outputs: C:\Users\sa\AppData\Local\Temp\2\
-
Siddharth Rout almost 12 yearsGr8. 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 almost 12 yearsI 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 almost 12 yearsCan 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 ofg_strLogPath
-
Will Rickards almost 12 yearsCouldn't step through but put msgbox right after. Found problem was with loglevel checking. Thanks
-
Deanna almost 12 yearsNormal practice is to have the logging levels on order of verbosity. You seem to have "everything" and "errors only" swapped.
-
Deanna almost 12 yearsAntivirus apps are well known to knobble writes to the temporary folder.
-
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 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 almost 12 yearsI 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.