可以搜索文件的.bat或.wsh脚本

时间:2022-12-19 22:37:58

I am looking for some examples of a .bat OR .wsh script that can do the following:

我正在寻找一些可以执行以下操作的.bat OR .wsh脚本示例:

  • Recursively read file names in a directory with a user-provided extension (.dll, .exe, etc)
  • 以用户提供的扩展名(.dll,.exe等)递归读取目录中的文件名

  • Search a user-provided directory for the above file names
  • 在用户提供的目录中搜索上述文件名

  • Generate a txt or xls report of the findings, like: x.txt was found in "C:\temp", "C:\blah"
  • 生成结果的txt或xls报告,例如:x.txt在“C:\ temp”,“C:\ blah”中找到

TIA.

EDIT:

Oops, I should clarify: there are two directories and two searches here.

哎呀,我应该澄清一下:这里有两个目录和两个搜索。

Search 1:

  • Search a user provided directory "Dir 1" for all *.dll's.
  • 搜索所有* .dll的用户提供的目录“Dir 1”。

Search 2:

  • Search a different user provided directory "Dir 2" for the file names generated in Search 1. This search also needs to be recursive.
  • 在搜索1中生成的文件名中搜索不同的用户提供的目录“目录2”。此搜索也需要递归。

So, if Search 1 finds foo.dll, foo2.dll and foo3.dll in Dir 1, Search 2 should look in Dir 2 for foo.dll, foo2.dll and foo3.dll, and provide a report (simple listing) of each found file.

因此,如果搜索1在目录1中找到foo.dll,foo2.dll和foo3.dll,则搜索2应该在目录2中查找foo.dll,foo2.dll和foo3.dll,并提供报告(简单列表)每个找到的文件。

3 个解决方案

#1


Put the following in a .bat file, say FindAll.bat:

将以下内容放在.bat文件中,比如FindAll.bat:

@echo OFF

for /f %%F in ('dir %2\%1 /s /b') do (
    <nul (set /p msg=%%~nxF )
    for /f %%G in ('dir %3\%%~nxF /s /b') do (
        if exist %%G (
            @echo found at %%G
        ) 
    )
)

%1 is the user provided file mask.

%1是用户提供的文件掩码。

%2 is the user provided directory to search first.

%2是用户首先搜索的目录。

%3 is the user provided directory to search second.

%3是用户提供的第二个搜索目录。

Call from the command line to generate a report:

从命令行调用以生成报告:

FindAll *.dll d:\dir1 d:\dir2 > dll_report.txt 2>&1

The <nul (set /p) trick will output text to the console without a new line (courtesy Pax from this thread: How to code a spinner for waiting processes in a Batch file?)

(set>

The 2>&1 added when calling the batch file is needed to capture all the output to the file (courtesy aphoria from this thread: Underused features of Windows batch files)

在调用批处理文件时需要添加2>&1来捕获文件的所有输出(来自此线程的礼节:Windows批处理文件的未充分利用的功能)

#2


Why not use dir?

为什么不用dir?

Search current directory and all subdirs for dlls

搜索当前目录和dll的所有子目录

dir /S *.dll

Search all of C for dlls

搜索所有C for dlls

dir /S C:\*.dll

Save a report

保存报告

dir /S C:\*.dll > report.txt

#3


I would study Robocopy to see if this could help (the /L flag is a clue).

我会研究Robocopy,看看这是否有帮助(/ L标志是一个线索)。

#1


Put the following in a .bat file, say FindAll.bat:

将以下内容放在.bat文件中,比如FindAll.bat:

@echo OFF

for /f %%F in ('dir %2\%1 /s /b') do (
    <nul (set /p msg=%%~nxF )
    for /f %%G in ('dir %3\%%~nxF /s /b') do (
        if exist %%G (
            @echo found at %%G
        ) 
    )
)

%1 is the user provided file mask.

%1是用户提供的文件掩码。

%2 is the user provided directory to search first.

%2是用户首先搜索的目录。

%3 is the user provided directory to search second.

%3是用户提供的第二个搜索目录。

Call from the command line to generate a report:

从命令行调用以生成报告:

FindAll *.dll d:\dir1 d:\dir2 > dll_report.txt 2>&1

The <nul (set /p) trick will output text to the console without a new line (courtesy Pax from this thread: How to code a spinner for waiting processes in a Batch file?)

(set>

The 2>&1 added when calling the batch file is needed to capture all the output to the file (courtesy aphoria from this thread: Underused features of Windows batch files)

在调用批处理文件时需要添加2>&1来捕获文件的所有输出(来自此线程的礼节:Windows批处理文件的未充分利用的功能)

#2


Why not use dir?

为什么不用dir?

Search current directory and all subdirs for dlls

搜索当前目录和dll的所有子目录

dir /S *.dll

Search all of C for dlls

搜索所有C for dlls

dir /S C:\*.dll

Save a report

保存报告

dir /S C:\*.dll > report.txt

#3


I would study Robocopy to see if this could help (the /L flag is a clue).

我会研究Robocopy,看看这是否有帮助(/ L标志是一个线索)。