vbs搜索指定的文件

vbs搜索指定的文件,第1张

'纯vbs的话要递归遍历所有文件夹,比较麻烦,可以和cmd结合起来用

targetfile="test.jpg"'改成你要找的文件名

Set WshShell = WScript.CreateObject("Wscript.Shell")

Set fso = WScript.CreateObject("Scripting.Filesystemobject")

tempfile=WScript.ScriptName&"_temp.txt"

wshshell.Run "cmd /c dir d:\"&targetfile&"/b /s>"&tempfile,1,True

Set f=fso.GetFile(tempfile)

If f.Size>0 Then'dir找到目标文件时才会向tempfile中写入数据,没找到时只会生成一个空文件

Set ft=fso.OpenTextFile(tempfile,1)

Do Until ft.AtEndOfStream'可能会找到多个文件,故需要读每一行的数据

line=ft.ReadLine'读入一行

MsgBox line'仅供演示

Loop

ft.Close

End If

fso.DeleteFile(tempfile)'删除生成的临时文件

这是一个示例代码,实际使用中需要根据自己的需求进行调整。

' 定义变量

Dim fso, folder, file, excelFile, sheet, findText

' 初始化变量

Set fso = CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder("C:\myfolder")

findText = "hello"

' 查找最新时间的EXCEL文件

For Each file In folder.Files

If file.Type = "Microsoft Excel Worksheet" And file.DateLastModified >excelFile.DateLastModified Then

Set excelFile = file

End If

Next

' 打开EXCEL文件并查找文字

Set excel = excelFile.OpenAsTextStream(1, -2)

Set sheet = excel.Worksheets(1)

Set findRange = sheet.Range("A1:Z1000")

Set findResult = findRange.Find(findText)

' 如果找到了,d出对话框提示

If Not findResult Is Nothing Then

MsgBox "Found the text in the Excel file."

End If

@echo off

set src=1.txt

set dst=2.txt

for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (

  if exist %%a:\nul (

     for /f "delims=" %%i in ('dir /s/a/b/a-d "%%a:\%src%" 2^>nul') do (

         copy "%%i" "%%~dpi%dst%"

     )

  )

)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11406445.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存