A .bat or .wsh script that can search for files

后端 未结 3 877
野的像风
野的像风 2020-12-18 04:21

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

  • Recursively read file names in a directory with a user-provided extension (.
相关标签:
3条回答
  • 2020-12-18 04:40

    Why not use dir?

    Search current directory and all subdirs for dlls

    dir /S *.dll
    

    Search all of C for dlls

    dir /S C:\*.dll
    

    Save a report

    dir /S C:\*.dll > report.txt
    
    0 讨论(0)
  • 2020-12-18 04:52

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

    0 讨论(0)
  • 2020-12-18 05:00

    Put the following in a .bat file, say 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.

    %2 is the user provided directory to search first.

    %3 is the user provided directory to search second.

    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?)

    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)

    0 讨论(0)
提交回复
热议问题