为什么 FileExists 不支持通配符? [英] Why doesn't FileExists support wildcards?

查看:55
本文介绍了为什么 FileExists 不支持通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个示例 VBScript 片段:

Consider this example VBScript fragment:

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists("D:\Folder\File*.ext") Then ' Finds nothing!
  fs.CopyFile "D:\Folder\File*.ext", "D:\OtherFolder\"
  fs.Deletefile "D:\Folder\File*.ext"
End If

FileExists 方法不支持通配符(*?).FolderExists 不是.我希望通配符能正常工作,因为它们适用于 FileSystemObject:CopyFileCopyFolderMoveFileMoveFolder, DeleteFile, DeleteFolderGet* 文件名处理方法,如 GetAbsolutePathName.

The FileExists method turns out not to support wildcards (* and ?). Not does FolderExists. I expected wildards to just work because they work fine for all similar methods in the FileSystemObject: CopyFile, CopyFolder, MoveFile, MoveFolder, DeleteFile, DeleteFolder and the Get* filename handling methods like GetAbsolutePathName.

当然有办法解决这个问题,比如 GetFolder 并迭代其文件.但是 FileExists 会更具可读性、方便性、自然性和一致性.

Of course there are ways to work around this, like GetFolder and iterating over its files. But FileExists would have been much more readable, convenient, natural and consistent.

fs.FileExists 不一致感觉像是 API 设计问题.可能是什么原因?背后有什么想法吗?

The fs.FileExists inconsistency feels like an API design problem. What could be the reason? Is there some idea behind it?

推荐答案

只有设计 Microsoft Scripting Runtime API (scrrun.dll)(这些函数是其中的一部分)的团队中的某个人才能肯定地回答这个问题.

Only someone from the team that designed the Microsoft Scripting Runtime API (scrrun.dll), which these functions are a part of, can answer this question for sure.

但我的猜测是 FileExists 只不过是 CreateFile Windows API 函数dwCreationDisposition 参数设置为 OPEN_EXISTING(仅当文件或设备存在时才打开它.").此 Windows API 函数不支持通配符,因此 FileExists 也不支持.

But my guess is that FileExists is nothing but a wrapper for the CreateFile Windows API function with the dwCreationDisposition parameter set to OPEN_EXISTING ("Opens a file or device only if it exists."). This Windows API function does not support wildcards, so FileExists can't, either.

当文件不存在时,系统将响应错误 2(系统找不到指定的文件.")并且 FileExists 将返回 错误.

When the file does not exist, the system will respond with error 2 ("The system cannot find the file specified.") and FileExists will return False.

以上基于使用进程监视器检查 FileExists 调用的行为.

The above is based on using Process Monitor to inspect the behavior of a FileExists call.

讨论这是否是 API 设计疏忽以及是否应该有所不同是没有实际意义的.

It would be moot to discuss whether this is an API design oversight and whether it should be any different.

话虽如此,没有理由在您显示的代码中进行存在"检查.

That being said, there is no reason for an "exists" check in the code you show.

如果您想将文件从位置 A 移动到位置 B,就这样做.

If you want to move files from location A to location B, just do that.

如果有东西要移动,它就会被移动.如果没有任何东西可以移动,就会出现错误,您可以检查.存在"检查不提供任何额外信息.

If there is something to move, it will be moved. If there is nothing to move, there will be an error you can inspect. The "exists" check provides no extra information whatsoever.

Dim fs, source
Set fs = CreateObject("Scripting.FileSystemObject")

On Error Resume Next

fs.MoveFile "File*.ext", "D:\OtherFolder\"

If Err.Number = 0 Then
  MsgBox "Done"
ElseIf Err.Number = 53 Then ' File not found
  MsgBox "Nothing to do"
ElseIf Err.Number = 76 Then ' Path not found
  MsgBox "Target path not found"
Else
  MsgBox "Unexpected Error " & Err.Number & " - " & Err.Description
End If

On Error Goto 0

为方便起见,我将其包装在 Sub 中,以便我可以重复使用它,并且 On Error Resume Next 不会泄漏到我的其余代码中.

For convenience I would wrap that in a Sub so that I can re-use it and the On Error Resume Next won't leak into the rest of my code.

还值得注意的是,在相同的卷中,MoveFile 将比复制和删除方式更快.

It's also worth noting that within the same volume, MoveFile will be way faster than copy-and-delete.

这篇关于为什么 FileExists 不支持通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆