vbscript:fso.opentextfile 权限被拒绝
在我的代码段中,当我编写文件名脚本时,它给了我一个权限被拒绝的信息 在以下行:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为这与文件权限本身无关。 它与您使用以下方法创建文件这一事实有关:
创建文件...并携带对该文件的引用 (objFile)
那么在销毁引用之前您不会关闭文件
因此您是销毁引用,但使文本流在内存中打开,从而锁定您的文件。
然后,当文件已经“打开”时,您将继续尝试重新打开该文件。 这有点啰嗦,创建文件后您已经获得了一个引用 - 直接写入该文件会比在创建另一个文件之前销毁该引用更容易。
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:
That creates the file...and carries a reference to that file (objFile)
Then you don't close the file before you destroy the reference
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.
其价值是什么...
我确信由于这一行而导致了权限错误:
因为这是“权限被拒绝”错误指向的行。 但事实上,我的权限错误是在下面几行:
没有带有这样标题的屏幕,因此 SendKeys 是没有权限的。
当然,解决方案是:
希望能有所帮助。
for what its worth...
I was convinced I had a permission error because of this line:
Because that's the line that the 'permission denied' error pointed to. But in fact, my permission error was a few lines further down:
There was no screen with such a caption, so the SendKeys is what did not have permission.
The solution, of course, was:
Hope that might help.
另外,请确保您没有在 Excel 中打开该文件(我在使用 .csv 文件时遇到了此问题)...
Also, make sure that you don't have the file open in Excel (I had this problem with a .csv file)...
在我的特殊情况下,之前存在的文件和我所要做的就是向Everyone用户授予权限
In my particular case the file which existed before and all I had to do was give permission to the Everyone user
巴拉巴斯特说得完全正确。 您需要在第二次重新打开文件进行写入之前关闭该文件,或者使用现有的打开句柄。
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.