大家好〜我是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个数字并继续在文件夹中搜索对应文件的指南/教程。
如果有人可以引导我前进,那就太好了。预先谢谢你
答案 0 :(得分:0)
要查找子字符串的最后一次出现,可以使用
InStrRev(string1,string2[,start[,compare]])
这将执行InStr()
的操作,但从字符串的末尾开始。
然后,您可以找到所有包含“ DN”的文件,并在每次匹配时再次搜索项目以获取匹配的“ INV”和/或“ PO”文件。