2008-12-15 141 views
3

在我的代码段,当我的脚本的文件名,它给了我一个权限上以下行否认 :的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域管理员权限。有任何想法吗?

在此先感谢

回答

11

我不认为这与文件权限本身有关。它具有与您创建使用该文件的事实做:

Set objFile = objFSO.CreateTextFile(strDirectory & strFile) 

创建该文件...并携带到该文件的引用(OBJFILE)

然后不关闭您之前的文件销毁参考

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

因此你摧毁了参考,但离开文本流开在内存从而锁定您的文件。

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

+0

好的...... – st0le 2011-12-12 12:57:33

0

balabaster是完全正确的。您需要在重新打开文件之前关闭该文件,然后再写入,或使用现有的打开的句柄。

1

什么它的价值...

我确信我有一个权限错误,因为这行:

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 

希望这可以帮助。

0

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

0

在我的具体情况之前存在的文件和所有我有要做的是给每个人的用户许可

相关问题