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

查看:604
本文介绍了为什么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 不会.我希望Wildard能够正常工作,因为它们对于 FileSystemObject : CopyFile MoveFolder DeleteFile

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脚本运行时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只是).此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将返回False.

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.

以上内容基于使用Process Monitor检查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天全站免登陆