简体   繁体   English

VBS 中的 CopyFile 权限被拒绝

[英]Permission denied on CopyFile in VBS

I'm trying to automate pushing a file into my users' home directories, but am stuck on a "Permission Denied" error — is thrown on line 6 here, with the CopyFile call.我正在尝试自动将文件推送到我的用户的主目录中,但我遇到了“权限被拒绝”错误 - 在这里的第 6 行,通过 CopyFile 调用抛出。

There are other parts of the script (not shown) that create and copy folder contents using the same source and destination directories, and they work perfectly.脚本的其他部分(未显示)使用相同的源目录和目标目录创建和复制文件夹内容,它们工作得很好。 It's only when I use CopyFile that it fails.只有当我使用 CopyFile 时它才会失败。

Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists("H:\Minecraft\.minecraft\options.txt") Then
    fso.CopyFile "C:\Minecraft\options.txt", "H:\Minecraft\.minecraft\"
End If

Set fso = Nothing

H: is a network home directory, to which the current user has full read/write privs. H:是网络主目录,当前用户对其具有完全的读/写权限。

I've tried adding/removing trailing slashes from the paths, adding "options.txt" to the destination path, removing the false argument... Not sure what else to try.我试过从路径中添加/删除尾部斜杠,将"options.txt"添加到目标路径,删除false参数......不知道还有什么可以尝试的。 Any thoughts?有什么想法吗?

FYI, this chunk of code, which comes immediately before the error-prone bit above, executes perfectly every time:仅供参考,这段代码紧接在上面容易出错的位之前,每次都能完美执行:

If Not fso.FolderExists("H:\Minecraft\.minecraft\bin\") Then
    If Not fso.FolderExists("H:\Minecraft\.minecraft\") Then
        fso.CreateFolder("H:\Minecraft\.minecraft\")
    End If
    fso.GetFolder("C:\Minecraft\bin\").Copy "H:\Minecraft\.minecraft\"
End If

I've only ever seen CopyFile fail with a "permission denied" error in one of these 3 scenarios:在以下 3 种情况之一中,我只见过CopyFile因“权限被拒绝”错误而失败:

  • An actual permission problem with either source or destination.源或目标的实际权限问题。
  • Destination path is a folder, but does not have a trailing backslash.目标路径是一个文件夹,但没有尾部反斜杠。
  • Source file is locked by an application.源文件被应用程序锁定。

for me adding / worked at the end of location of folder.对我来说,在文件夹位置的末尾添加/工作。 Hence, if you are copying into folder, don't forget to put /因此,如果您要复制到文件夹中,请不要忘记将/

Another thing to check is if any applications still have a hold on the file.要检查的另一件事是是否有任何应用程序仍保留该文件。

Had some issues with MoveFile. MoveFile 有一些问题。 Part of my permissions problem was that my script opens the file (in this case in Excel), makes a modification, closes it, then moves it to a "processed" folder.我的权限问题的一部分是我的脚本打开文件(在这种情况下在 Excel 中),进行修改,关闭它,然后将其移动到“已处理”文件夹。

In debugging a couple things, the script crashed a few times.在调试一些事情时,脚本崩溃了几次。 Digging into the permission denied error I found that I had 4 instances of Excel running in the background because the script was never able to properly terminate the application due to said crashes.深入研究权限被拒绝错误,我发现我有 4 个 Excel 实例在后台运行,因为由于上述崩溃,脚本永远无法正确终止应用程序。 Apparently one of them still had a hold on the file and, thusly, "permission denied."显然,其中一个人仍然持有该文件,因此“权限被拒绝”。

Based upon your source variable ( sourcePath = "C:\\Minecraft\\bin\\" ) I suspect your hard code is pointing at the wrong place根据您的源变量( sourcePath = "C:\\Minecraft\\bin\\" ),我怀疑您的硬代码指向错误的位置

fso.CopyFile "C:\Minecraft\options.txt", destinationPath, false

should be应该

fso.CopyFile "C:\Minecraft\bin\options.txt", destinationPath

or要么

fso.CopyFile sourcePath & "options.txt", destinationPath

I have read your problem, And i had the same problem.我已阅读您的问题,我遇到了同样的问题。 But af ter i changed some, my problem "Permission Denied" is solved.但是在我更改了一些之后,我的“权限被拒绝”问题就解决了。

Private Sub Addi_Click()
'On Error Resume Next
'call ds
browsers ("false")
Call makeAdir
ffgg = "C:\Users\Backups\user\" & User & "1\data\"
Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.Getfolder("c:\users\Backups\user\" & User & "1\data")
    f.Attributes = 0
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.Copyfile(filetarget, ffgg, True)

Look at ffgg = "C:\\Users\\Backups\\user\\" & User & "1\\data\\" , Before I changed it was ffgg = "C:\\Users\\Backups\\user\\" & User & "1\\data" When i add backslash after "\\data\\" , my problem is solved.看看ffgg = "C:\\Users\\Backups\\user\\" & User & "1\\data\\" ,在我改之前是ffgg = "C:\\Users\\Backups\\user\\" & User & "1\\data"当我在"\\data\\"之后添加反斜杠时,我的问题就解决了。 Try to add back slash.尝试添加反斜杠。 Maybe solved your problem.也许解决了你的问题。 Good luck.祝你好运。

You can do this:你可以这样做:

fso.CopyFile "C:\Minecraft\options.txt", "H:\Minecraft\.minecraft\options.txt"

Include the filename in the folder that you copy to.在您复制到的文件夹中包含文件名。

It's worth checking task manager for any stray wscript.exe tasks that are stuck.值得检查任务管理器是否有任何卡住的流浪 wscript.exe 任务。 It could be one of those that's blocking access to the file.它可能是阻止访问文件的那些之一。

It is possible that your Antivirus software may be preventing the activity of the script.您的防病毒软件可能会阻止脚本的活动。 I encountered this with AVG Antivirus running in silent mode (which means it does not alert you to every protection step it takes, so you get a permission error without realising that AVG is preventing the action).我在 AVG Antivirus 以静默模式运行时遇到了这个问题(这意味着它不会提醒您它采取的每个保护步骤,因此您会收到权限错误而没有意识到 AVG 正在阻止该操作)。 In my case, I invoked verbose mode (switched off silent mode), executed the script and AVG came up with an interception warning, allowing me to train AVG to permit this script to run.就我而言,我调用了详细模式(关闭了静默模式),执行了脚本,AVG 提出了一个拦截警告,允许我训练 AVG 以允许该脚本运行。

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

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