首页 > 解决方案 > 使用 for 循环 VBS 中文件名的最后几个字母搜索文件夹的其余部分

问题描述

大家好~我是VBS的新手,我正在尝试查找代码来搜索文件夹中的其余文件,这些文件在for循环中的文件中包含特定数字。让我试着为你形象化它。

打印出我每天必须手动打印出的文档的当前代码:

'' To set the path to the current folder

set shApp = CreateObject("shell.application")


currentPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") 


set shFolder = shApp.NameSpace( currentPath )



'' To set the items in the current folder as "files"


set files = shFolder.Items()


'Open excel sheet to print the paper for shipping side to sign'

set objsheet = getobject(,"Excel.Application").activeworkbook.activesheet


''Start of code''

count = 1

for each files in files 
    ' If name contains "DN" '
    if inStr(files, "DN") then
        ' Print 1 time if SO'
        if inStr(files, "SO") then
            'print 1 time'
            files.InvokeVerbEx("Print")
            ' Pop up to show what has been printed '
            CreateObject("WScript.Shell").Popup "Printed "+ files , 3, "\(o.o)/    The Magic Script    \(o.o)/"
            objsheet.cells(count,1) = files
            count = count + 1
            'Pause in the script'
            WScript.Sleep 5000
        end if
        if inStr(files, "SM") then
            'print 4 time using loop function and "i" as a counter'
            'Do
            '   i = i + 1
            '   files.InvokeVerbEx("Print")
            'Loop until i>= 4
            'i = 0
            files.InvokeVerbEx("Print")
            files.InvokeVerbEx("Print")
            files.InvokeVerbEx("Print")
            files.InvokeVerbEx("Print")
            'Pop up to show what has been printed'
            CreateObject("WScript.Shell").Popup "Printed "+ files , 3, "\(o.o)/    The Magic Script    \(o.o)/"
            objsheet.cells(count,1) = files
            count = count + 1
            ' Pause in the script '
            WScript.Sleep 15000 
        end if
    end if
    ' if name contains "INV" '  
    if inStr(files, "INV") then
        ' Print 6 times using loop function and "j" as a counter '
        'Do
        '   j = j + 1
        '   files.InvokeVerbEx("Print")
        'Loop until j >= 6
        'j = 0
        files.InvokeVerbEx("Print")
        files.InvokeVerbEx("Print")
        files.InvokeVerbEx("Print")
        files.InvokeVerbEx("Print")
        files.InvokeVerbEx("Print")
        files.InvokeVerbEx("Print")
        ' Pop up to show what has been printed '
        CreateObject("WScript.Shell").Popup "Printed "+ files , 3, "\(o.o)/    The Magic Script    \(o.o)/"
        WScript.Sleep 18000 ' Prevent Lag '
    end if
    ' if name contains "PO" '
    if inStr(files, "PO") then
        'print out 2 times'
        'Do
        '   k = k + 1
        '   files.InvokeVerbEx("Print")
        'Loop until k >= 2
        'k = 0
        files.InvokeVerbEx("Print")
        files.InvokeVerbEx("Print")
        ' Pop up to show what has been printed '
        CreateObject("WScript.Shell").Popup "Printed "+ files , 3, "\(o.o)/    The Magic Script    \(o.o)/"
        WScript.Sleep 6000 'Prevent Lag'
    end if
    ' if name contains "OF" '
    if inStr(files, "OF") then
        ' print out 1 time '
        files.InvokeVerbEx("Print")
        objsheet.cells(count, 1) = files
        CreateObject("Wscript.Shell").Popup "Printed "+ files , 3, "\(o.o)/    The Magic Script    \(o.o)/"
        count = count + 1
        Wscript.Sleep 5000
    end if

next

objsheet.printout

CreateObject("WScript.Shell").Popup "Successfully Printed " , 10, "\(o.o)/    The Magic Script    \(o.o)/"

现在我试图通过将包含“DN”的文件与其包含“PO”或“INV”的相应文件匹配来改进代码,因此当它打印出来时,它会是有序的(因此更容易将它装订在一起)并且会保存在我的工作场所为人们腾出很多时间。

文件夹中已保存文件的示例如下:

DN_789456 SO 13371337

DN_963258 SO 12341234

INV_785412 SO 13371337

PO_632541 SO 12341234

如您所知,匹配它们的唯一方法是使用文件名中的最后 8 个数字。但是,我找不到可以用来提取这 8 个数字并继续在文件夹中搜索相应文件的指南/教程。

如果有人可以指导我,那就太好了。先感谢您

标签: vbscript

解决方案


要查找子字符串的最后一次出现,您可以使用

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

这将执行InStr()但从字符串末尾开始的操作。

然后,您可以找到所有包含“DN”的文件,并在每次匹配时再次搜索项目以获得匹配的“INV”和/或“PO”文件。


推荐阅读