Board logo

标题: [文本处理] [求助]如何在一堆Excel文档中找出内容含有关键词的文件并移动到指定文件夹。 [打印本页]

作者: Kira_1984    时间: 2019-10-17 00:37     标题: [求助]如何在一堆Excel文档中找出内容含有关键词的文件并移动到指定文件夹。

A文件夹中有数百个excel文档,需要在这些文档中找出内容含有指定关键词(例如:abc)的文件,并将这些文件移动到指定文件夹B中。
求大神帮忙写一段批处理代码
作者: Kira_1984    时间: 2019-11-1 11:56

求助一下,有没有大神在
作者: terse    时间: 2019-11-1 14:07

回复 2# Kira_1984
试一下 这个可以帮到不
  1. $Destination = '\B\'
  2. $SearchText = 'ABC'
  3. if (![System.IO.Directory]::Exists($Destination)) {$null = New-Item $Destination  -type directory}
  4. $Excel = New-Object -ComObject Excel.Application
  5. $Excel.Visible
  6. $Files = Get-ChildItem ".\A\*.xlsx" | Select -Expand FullName
  7. $f = ForEach($File in $Files){
  8.     $Workbook = $Excel.Workbooks.Open($File)
  9.     If($Workbook.Sheets.Item(1).UsedRange.Find($SearchText)){
  10.        $File
  11.     }
  12.     $workbook.close($false)
  13. }
  14. $f | Move-Item -Destination $Destination
  15. $Excel.Quit()
复制代码

作者: Kira_1984    时间: 2020-2-11 22:42

回复  Kira_1984
试一下 这个可以帮到不
terse 发表于 2019-11-1 14:07

试了下好像不行。Windows本身的搜索功能可以用来含关键字内容的文件,不知道有没有命令可以调用windows的查找功能把文件找出来。
作者: Batcher    时间: 2020-2-12 07:40

回复 4# Kira_1984


    没有。findstr命令可以查找txt文件,但是不能查找Excel这种非纯文本文件。
作者: Batcher    时间: 2020-2-12 07:41

回复 4# Kira_1984


    执行3楼的代码有什么报错信息?




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2