Vbscript: fso.opentextfile exception allowed

In my code segment, when I script the file name, it gives me permission rejected 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 vbscript domain administrator permissions. Any ideas?

early

+3
source share
5 answers

I do not think this is due to file permissions per se. This is because you created the file using:

 Set objFile = objFSO.CreateTextFile(strDirectory & strFile) 

This creates a file ... and contains a link to this file (objFile)

Then you do not close the file before destroying the link

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

Therefore, you destroy the link, but leave the text stream open in memory, thereby blocking the file.

Then you try to reopen the file while the file is already β€œopen”. This is a little longer, you already received the link after creating the file - it would be easier to just write directly, and not destroy the link before creating another.

+11
source

for being worth it ...

I was convinced that I had a permission error due to this line:

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

Because the line that the permission error points to is indicated. But actually my permission error was a few lines below:

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

There was no screen with that title, so SendKeys is something that did not have permission.

The solution, of course, was:

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

Hope this helps.

+2
source

Also, make sure you don't have an open file in Excel (I had this problem with a CSV file) ...

+1
source

Balabaster is exactly right. You need to close the file before re-opening it a second time for writing or using an existing open descriptor.

0
source

In my specific case, the file that existed before and all I had to do was give permission to Everyone

0
source

All Articles