vbscript:fso.opentextfile 权限被拒绝

发布于 2024-07-10 03:37:30 字数 1555 浏览 8 评论 0原文

在我的代码段中,当我编写文件名脚本时,它给了我一个权限被拒绝的信息 在以下行:

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

脚本

'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

这是我已分配 vbscript 域管理员权限的 。 有任何想法吗?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小瓶盖 2024-07-17 03:37:30

我认为这与文件权限本身无关。 它与您使用以下方法创建文件这一事实有关:

Set objFile = objFSO.CreateTextFile(strDirectory & strFile)

创建文件...并携带对该文件的引用 (objFile)

那么在销毁引用之前您不会关闭文件

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

因此您是销毁引用,但使文本流在内存中打开,从而锁定您的文件。

然后,当文件已经“打开”时,您将继续尝试重新打开该文件。 这有点啰嗦,创建文件后您已经获得了一个引用 - 直接写入该文件会比在创建另一个文件之前销毁该引用更容易。

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.

北方。的韩爷 2024-07-17 03:37:30

其价值是什么...

我确信由于这一行而导致了权限错误:

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

因为这是“权限被拒绝”错误指向的行。 但事实上,我的权限错误是在下面几行:

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

没有带有这样标题的屏幕,因此 SendKeys 是没有权限的。

当然,解决方案是:

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

希望能有所帮助。

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.

生生漫 2024-07-17 03:37:30

另外,请确保您没有在 Excel 中打开该文件(我在使用 .csv 文件时遇到了此问题)...

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

绝不放开 2024-07-17 03:37:30

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

In my particular case the file which existed before and all I had to do was give permission to the Everyone user

末が日狂欢 2024-07-17 03:37:30

巴拉巴斯特说得完全正确。 您需要在第二次重新打开文件进行写入之前关闭该文件,或者使用现有的打开句柄。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文