Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vbscript : fso.opentextfile permission denied

In my code segment, when I script the file name, it gives me a permission denied on the following line:

Set objTextFile = objFSO.OpenTextFile(strDirectory & strFile, ForAppending, True)

Here is the script

'output log info
Function OutputToLog (strToAdd)  
    Dim strDirectory,strFile,strText, objFile,objFolder,objTextFile,objFSO
    strDirectory = "c:\eNet"
    strFile = "\weeklydel.bat"
    'strText = "Book Another Holiday"
    strText = strToAdd

    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Check that the strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFolder = objFSO.CreateFolder(strDirectory)
       'WScript.Echo "Just created " & strDirectory
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
       'Wscript.Echo "Just created " & strDirectory & strFile
    End If

    set objFile = nothing
    set objFolder = nothing
    ' OpenTextFile Method needs a Const value
    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const ForAppending = 2

    Set objTextFile = objFSO.OpenTextFile(strDirectory & strFile, ForAppending, True)

    ' Writes strText every time you run this VBScript
    objTextFile.WriteLine(strText)
    objTextFile.Close
End Function

I have assigned the vbscript domain administrator permissions. Any ideas?

thanks in advance

like image 284
phill Avatar asked Dec 07 '22 08:12

phill


2 Answers

I don't think this has to do with File Permissions per se. It has to do with the fact that you've created the file using:

Set objFile = objFSO.CreateTextFile(strDirectory & strFile)

That creates the file...and carries a reference to that file (objFile)

Then you don't close the file before you destroy the reference

...
'Missing objFile.Close here
Set objFile = nothing
Set objFolder = nothing
...

Consequently you're destroying the reference but leaving the textstream open in memory thus locking your file.

You are then proceeding to attempt to re-open the file while the file is already "open". This is a little long winded, you've already got a reference after you've created the file - it would be easier just to write straight to that rather than destroy the reference before creating another one.

like image 115
BenAlabaster Avatar answered Feb 27 '23 06:02

BenAlabaster


for what its worth...

I was convinced I had a permission error because of this line:

Set LogFile = LogFSO.OpenTextFile(LogFileName, ForWriting, True)

Because that's the line that the 'permission denied' error pointed to. But in fact, my permission error was a few lines further down:

WshShell.AppActivate(ScreensToRemove(i))
WshShell.SendKeys ("~")
WScript.Sleep(1000)

There was no screen with such a caption, so the SendKeys is what did not have permission.

The solution, of course, was:

If WshShell.AppActivate(ScreensToRemove(i)) = True Then
   WshShell.SendKeys ("~")
   WScript.Sleep(1000)
End if

Hope that might help.

like image 40
Chris Avatar answered Feb 27 '23 07:02

Chris