简体   繁体   English

vbscript:fso.opentextfile权限被拒绝

[英]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. 我已经分配了vbscript域管理员权限。 Any ideas? 有任何想法吗?

thanks in advance 提前致谢

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) 这会创建文件...并带有对该文件的引用(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. 这是一个有点长的啰嗦,你已经在创建文件后得到了一个引用 - 直接写入它会更容易,而不是在创建另一个之前销毁引用。

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. 没有带有这种标题的屏幕,因此SendKeys是没有权限的。

The solution, of course, was: 当然,解决方案是:

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

Hope that might help. 希望可能有所帮助。

此外,请确保您没有在Excel中打开该文件(我有一个.csv文件的问题)...

在我的特定情况下,之前存在的文件和我所要做的就是向Everyone用户授予权限

balabaster is exactly right. 巴拉巴斯特是完全正确的。 You either need to close the file before reopening it a second time for writing, or using the existing open handle. 您需要先关闭文件,然后再次重新打开文件,或者使用现有的打开句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM