permissions - VBScript 可以创建文本文件但不能删除文件(权限被拒绝)?

标签 permissions delete-file createfile

我创建了一个 .vbs 文件(可以:创建文本文件 -> 打开 notepad.exe -> 删除上面的文件),如下所示:

' Create a new file
Dim objFS, objFile
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.CreateTextFile("D:\Folder\Textfile.txt")
objFile.WriteLine ("sample text")

' Run a program and wait
WScript.CreateObject("WScript.Shell").Run "notepad.exe", 1, true

' Delete file
objFS.Deletefile("D:\Folder\Textfile.txt")

但是当我运行它时,关闭记事本窗口后,它显示错误信息:

Line: 10
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error

不知道为什么这个.vbs不能删除文本文件?感谢您的帮助!

最佳答案

在尝试删除之前,您必须在脚本中关闭文件句柄。

' Create a new file
Dim objFS, objFile
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.CreateTextFile("D:\Folder\Textfile.txt")

'write to the file
objFile.WriteLine ("sample text")

'close the file
objFile.close
set objFile = Nothing

' open the file in notepad, and wait
WScript.CreateObject("WScript.Shell").Run "notepad.exe D:\Folder\Textfile.txt", 1, true

' Delete file
objFS.Deletefile("D:\Folder\Textfile.txt")

set objFS = Nothing

关于permissions - VBScript 可以创建文本文件但不能删除文件(权限被拒绝)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779952/

相关文章:

permissions - 使用Bower安装时出现权限错误

linux - CentOS 6.5 如何让新文件继承父文件夹所有权?

php - 检测文件从服务器中删除的原因

c++ - 在 C++ 中创建一个文件,就像按右键->新建->新建文本文档一样

macos - 我的sudo命令无法正常工作

php - 如何在保留权限的同时在Docker容器中共享Laravel应用代码库?

c# - 删除锁定的文件和文件夹

file - 我可以恢复从未提交的已删除文件吗?

c++ - 使用 CreateFile 访问驱动器分区

c - 窗口 c : failed to create mmap of a file