使用VBS脚本检查字符串是否包含特定字符 [英] Check if a string contains specific characters using VBS script

查看:1999
本文介绍了使用VBS脚本检查字符串是否包含特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本正在执行以下操作:


  1. 检索所有我选择的文件夹文件

  2. 按日期对它们进行分类(从最近到较旧)

  3. 在窗口中显示它们

这是我的VBS脚本(我在

解决方案

您正在使用 InStr 错误。您的代码:

  InStr(Tableau(1,i), average,vbTextCompare)

InStr 的签名为:

  InStr([start,] string1,string2 [,compare])

但是这里要注意的是它有两个可选参数,其中一个在最前面,具有特殊条件:


可选。指定每次搜索的起始位置。默认情况下,搜索从第一个字符位置(1)开始。 如果指定了compare,则需要此参数


因此,因为您使用的第四个参数的值 vbTextCompare ,您还需要在第一个参数中指定起点,该起点应为 1 (第一个字符)你的情况。因此,更正后的代码为:

  InStr(1,Tableau(1,i), average,vbTextCompare)

您看到的错误消息基本上是抱怨第一个参数应该是整数,但是您正在喂它



请参见 InStr文档


My script is doing the following point :

  1. Retrieve all my selected folder files
  2. Class them by date (From the recent one to the older)
  3. Show them in a window

Here is my VBS Script (I retrieve it here):

    Option Explicit

    Const PathMDB   = "C:\Users\C8461789\Desktop\test_script" 

    MsgBox TriRepertoire,,"Enumération " & PathMDB
    '---lister les fichiers du répertoire ---
    Function TriRepertoire()
    Dim fso, fichier, fileItem
    Dim i, imax, z, valeur, cible, liste
    Set fso = CreateObject("Scripting.FileSystemObject")

    imax = 0
    'début de l'énumération
    For Each fichier In fso.GetFolder(PathMDB).Files
    Set fileItem = fso.GetFile(fichier)

    imax = imax + 1
    ReDim Preserve Tableau(2, imax)
    Tableau(1, imax) = Fichier.Name
    Tableau(2, imax) = FileItem.DateLastModified

    '---trier les fichiers par ordre décroissant de création ---
    Do
    Valeur = 0
    For i = 1 To imax - 1
        If InStr(Tableau(1,i), "average", vbTextCompare) > 0 Then
            If CDate(Tableau(2, i)) < CDate(Tableau(2, i + 1)) Then
                For z = 1 To 2
                   Cible = Tableau(z, i)
                   Tableau(z, i) = Tableau(z, i + 1)
                   Tableau(z, i + 1) = Cible
                Next
                Valeur = 1
            End If
        End If
    Next 
    Loop While Valeur = 1
    Set fileItem = nothing
    Next

    'Affichage du résultat classé
    For i = 1 To imax
    'If IsNull(Tableau) Then
        liste = liste &vbTab& Tableau(1, i) &vbCr 
    'End If
    Next
    TriRepertoire = liste

    Set fso = nothing 
    End Function

In order to filter by name my retrieved files, I would like to add the following condition :

  • For each file name, if it contains "average", add the file name to the table
  • Else, do nothing

I tried to use

If InStr(Tableau(1,i), "average", vbTextCompare) > 0 Then

But it shows me this error :

解决方案

You are using InStr incorrectly. Your code:

InStr(Tableau(1,i), "average", vbTextCompare)

The signature for InStr is:

InStr([start,]string1,string2[,compare])

But the gotcha here is that it has two optional parameters, one of them being in the front, with a special condition:

Optional. Specifies the starting position for each search. The search begins at the first character position (1) by default. This parameter is required if compare is specified

So because you are using the fourth parameter with the value vbTextCompare, you need to specify the starting point in the first parameter as well, which would be 1 (first character) in your case. So, the corrected code is:

InStr(1, Tableau(1,i), "average", vbTextCompare)

The error message you see basically complains that the first parameter is expected to be an integer, but you are feeding it a string.

See InStr docs.

这篇关于使用VBS脚本检查字符串是否包含特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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