作者:朱金灿

来源:http://blog.csdn.net/clever101

最近编写一个JScript,在调用FileSystemObject.CopyFile发生没有权限的错误,具体如下图:

开始觉得这个错误挺诡异的,因为我是以管理员身份运行这个js的,怎么会没有权限呢?上网搜索了很多信息也没有找到答案。最后查了下MSDN,发现被错误信息误导了。MSDN是这样说的:

CopyFile Method

See Also

Copy Method| CopyFolderMethod | CreateTextFileMethod | DeleteFileMethod | MoveFileMethod
Applies To: FileSystemObjectObject

Language

  • JScript
  • VBScript
  • Show All

Copies one or more files from one location to another.

object.CopyFile ( source, destination[, overwrite] )

Arguments

object

Required. The objectis always the name of a FileSystemObject.

source

Required.Character string file specification, which can include wildcard characters, forone or more files to be copied.

destination

Required.Character string destination where the file or files from source are tobe copied. Wildcard characters are not allowed.

overwrite

Optional.Boolean value that indicates if existing files are to be overwritten. If true,files are overwritten; if false, they are not. The default is true.Note that CopyFile will fail if destination has the read-onlyattribute set, regardless of the value of overwrite.

Remarks

Wildcard characters can only be used in the last pathcomponent of the source argument. For example, you can use:

[JScript]
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFile ("c:\\mydocuments\\letters\\*.doc", "c:\\tempfolder\\")
[VBScript]
FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"

But you cannot use:

[JScript]
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFile ("c:\\mydocuments\\*\\R1???97.xls", "c:\\tempfolder")
[VBScript]
FileSystemObject.CopyFile "c:\mydocuments\*\R1???97.xls", "c:\tempfolder"

If source contains wildcard characters or destinationends with a path separator (\), it is assumed that destination is anexisting folder in which to copy matching files. Otherwise, destinationis assumed to be the name of a file to create. In either case, three things canhappen when an individual file is copied.

  • If destination does not exist, source gets copied. This is the usual case.
  • If destination is an existing file, an error occurs if overwrite is false. Otherwise, an attempt is made to copy source over the existing file.
  • If destination is a directory, an error occurs.

An error also occurs if a source using wildcardcharacters doesn't match any files. The CopyFile method stops on thefirst error it encounters. No attempt is made to roll back or undo any changesmade before an error occurs.

通过阅读上面的接口说明,我发现我对FileSystemObject.CopyFile的接口的理解的。我以为可以这样用:比如我想把C:\\src\\1.cpp拷贝到D:\\MyProject,代码是这样写:

FileSystemObject.CopyFile("C:\\src\\1.cpp","D:\\MyProject");

实际上这是错误的,只能写成这样:

FileSystemObject.CopyFile("C:\\src\\1.cpp","D:\\MyProject\\1.cpp");

如果你想把src目录下所有cpp文件拷贝到D:\\MyProject目录下,可以这样写:

FileSystemObject.CopyFile("C:\\src\\*.cpp","D:\\MyProject");

看来以后遇到这样的问题第一步应该先查MSDN,不过微软的“没有权限”的错误提示也太坑爹了!

参考文献:

1.  javascript 操作文件实现方法小结

最新文章

  1. 玩转spring boot——结合JPA事务
  2. python file模块 替换输入内容脚本
  3. 思考探索,如何才能高效访问我的这个DataTable?
  4. python 练习 7
  5. RHEL6中ulimit的nproc限制
  6. Magento模型和ORM基础
  7. C++ thread函数使用
  8. MPreview.js
  9. zlib库VS2015编译步骤
  10. 洛谷 P1111 修复公路
  11. Go基础系列:空接口
  12. Cocos Creator cc.Event点击触摸事件详解
  13. shiroWeb项目-认证及MD5认证信息在页面显示(十)
  14. VTK拾取网格模型上的可见点
  15. one-hot句子向量 对比度增强
  16. Centos7部署ntp服务器同步时间以及直接将本地时间同步为北京时间
  17. 深入理解.sync修饰符
  18. 一、java概述
  19. myeclipse单元测试
  20. Ztree学习(-)简单例子

热门文章

  1. Java文档上传问题设计
  2. Injector Job深入分析 分类: H3_NUTCH 2015-03-10 15:44 334人阅读 评论(0) 收藏
  3. HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
  4. mysql分区功能(三个文件储存一张表)(分区作用)(分区方式)
  5. php求二叉树的深度(1、二叉树就可以递归,因为结构和子结构太相似)(2、谋而后动,算法想清楚,很好过的)
  6. Android自定义组件系列【5】——进阶实践(1)
  7. ios开发事件处理之:五:事件的响应
  8. 批量解决 word/wps 中公式和文字不对齐的问题
  9. Qt 静态函数QMetaObject::connectSlotsByName(QObject * object)
  10. Undefined symbols for architecture i386: "_OBJC_CLASS_$_KKGridView", referenced from: