VBscript to check for the existance of a file (with the ability to use a wildcard) within a certain time frame

后端 未结 3 1369
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 12:34

Good morning all, I have been trying to pull together a VBscript that takes a file path and a file name (that may have a wildcard in it) from the user when the script is

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 13:17

    For your example, the easiest way to do this is to use the inStr (In String)function. I find it works in 99% of my wild card tasks. So, in your example, instead of using:

    If file.Name = fileName Then
    

    use:

    If inStr(file.Name, filename) Then
    

    This doesn't actually allow for wildcards(*) as it won't find a match(with the asterisk in the argument), so you would need to strip the wildcard from the string and replace it with nothing (or just train the user to not use wildcards):

    Replace(filename,"*", "")
    

    However, the inStr function does allow for partial or full matches which makes it suitable for most wildcard tasks. Therefore, if your file name is pic.jpg, whether the user searches for:

    pic or jpg or p or c or pi etc.

    It will return a match. Keep in mind though, that the instr function returns a number where the match shows up in the string. So, if it doesn't create a match, the result will be 0. I've run into examples where NOT doesn't work or I've needed to use the full syntax which in this case would be:

    If inStr(file.Name, filename)<>0 Then
    

提交回复
热议问题