1. 首页 > 科技

VBS 遍历文件名后写入数组,再随机抽取数组中的文件名? vbs数组

VBS 遍历文件名后写入数组,再随机抽取数组中的文件名?vbs数组

如何用vbs获取指定路径下的文件名并输出到文本文件

把文本文件处理为数组,每行为一个数组元素,然后在每个元素中查找关键词,vbs可以直接使用instr函数来查找,也可以使用正则表达式查找。找到后把那个数组元素复制出来就可以了。第一种,使用instrc = split(createobject("scripting.filesystemobject").opentextfile("a.txt").readall,vbcrlf)for i = 0 to ubound(c)if instr(c(i),"nice") then msgbox c(i)next第二种,使用正则表达式c = split(createobject("scripting.filesystemobject").opentextfile("a.txt").readall,vbcrlf)for i = 0 to ubound(c)if rt("nice",c(i)) then msgbox c(i)nextFunction rt(patrn,str)set regex=new regexpregex.pattern = patrnregex.ignorecase = falsert = regex.test(str)End Function

用vbscript遍历数组

用记事本编辑*.vbs 写入以下代码,可双击文件运行。

dim arr(3)

arr(0)="a"

arr(1)="b"

arr(2)="c"

arr(3)="d"

for each i in arr

msgbox""&i&""

next

vbs 遍历一个文件夹并按原来的文件路径和文件名复制到新文件夹

Set fso=CreateObject("scripting.filesystemobject")

DestFolderPath="c:\dest" '搜索到的文件 复制的 新文件夹 路径

SearchFolderPath="c:\scr" ' 要搜索 遍历 的文件夹

SearchFileName="123" ' 要搜索的 文件名

dim files(),CopyFilePrefixPath

redim files(0)

call SearchFile(SearchFolderPath,SearchFileName,files,fso)

for i=1 to ubound(files)

CopyFilePrefixPath=Replace(LCase(files(i)),LCase(SearchFolderPath),"")

CopyFilePrefixPath=Replace(LCase(CopyFilePrefixPath),LCase(SearchFileName),"")

If Not CopyFilePrefixPath="" Or Not CopyFilePrefixPath="\" then

If Left(CopyFilePrefixPath,1)="\" Then

CopyFilePrefixPath=Mid(CopyFilePrefixPath,2)

End if

If right(CopyFilePrefixPath,1)="\" Then

CopyFilePrefixPath=Mid(CopyFilePrefixPath,1,Len(CopyFilePrefixPath)-1)

End If

Else

CopyFilePrefixPath=""

End If

call CopyFile(files(i),DestFolderPath,CopyFilePrefixPath,fso)

next

msgbox "已复制 " & ubound(files) & " 个文件!"

Sub CopyFile(SrcFilePath,byval DistFolder,AddCopyFileFrefixPath,fso)

Dim AddPaths,tmp

if not right(DistFolder,1)="\" then DistFolder=DistFolder & "\"

tmp=DistFolder

If AddCopyFileFrefixPath="" then

fso.copyfile SrcFilePath,DistFolder,true

Else

AddPaths=Split(AddCopyFileFrefixPath,"\")

For Each FolPath In AddPaths

tmp=tmp & FolPath & "\"

If not fso.FolderExists(tmp) Then fso.createfolder tmp

Next

fso.copyfile SrcFilePath,DistFolder & AddCopyFileFrefixPath & "\",true

End if

End Sub

Sub SearchFile(FolderPath,ByVal SearchFileName,arrs,fso)

dim FolderObject

SearchFileName=lcase(SearchFileName)

set FolderObject=fso.getfolder(FolderPath)

for each f in FolderObject.files

if SearchFileName=lcase(f.name) then

redim preserve arrs(ubound(arrs)+1)

arrs(ubound(arrs))=f.path

end if

next

for each fol in FolderObject.subfolders

call SearchFile(fol.path,SearchFileName,arrs,fso)

next

set FolderObject=nothing

end sub

vbs 遍历一个文件夹将指定文件复制到新的文件夹

'函数定义 Rpath:文件路径 Tpath目标路径 Ftype文件类型

Function CopyDemo (Rpath, Tpath, Ftype)

Set fso = CreateObject ("Scripting.FileSystemObject")

fso.CopyFile Rpath&filetype, tpath

End Function

'调用例子

CopyDemo "C:\", "D:\", "*.xml"

经验证可用