Fastest recursive search for files

42 ビュー (過去 30 日間)
KAE
KAE 2020 年 7 月 10 日
コメント済み: Adam Danz 2025 年 4 月 21 日
From this answer, I learned that dir can search recursively for files in subdirectories, and it is about 3X faster than what I had been using earlier, getfilenames. However now I am searching a remote directory containing many subdirectories with thousands of files, and the dir command takes 30+ minutes to execute. Is there a faster way? I am happy to call something outside of Matlab, like find in the accepted answer here.

採用された回答

Adam Danz
Adam Danz 2024 年 1 月 28 日
編集済み: Adam Danz 2024 年 1 月 28 日
Here's an Easter egg. I rarely answer non-MATLAB questions here but since I had to solve this today, I thought I'd share.
This batch file finds all .fig files in a directory and its subfolders (recursive search). Instructions:
  1. Create a new file with the .cmd or .bat extension.
  2. Copy-paste the script below into the file, adjusting the variables as needed.
  3. Update this line to specify the search directory: set "SEARCH_DIR=\\abc\def\"
  4. Update this line to specify the output file name, saved to the same directory as the cmd file: set "OUTPUT_FILE=FigFilesList.txt"
  5. Update the search term: (*.fig)
  6. Double-click on the file to execute the batch script.
Upon execution, the script will...
  • ...open the terminal command window
  • Search through the specified directory and its subdirectories for files with the .fig extension.
  • Output the paths of these files to a text file named FigFilesList.txt.
  • Every 200 fig files found, a message will be displayed in the terminal to indicate progress.
  • A message will appear at the end to indicate that the seaerch is complete.
@echo off
setlocal EnableDelayedExpansion
set "SEARCH_DIR=\\abc\def\"
set "OUTPUT_FILE=FigFilesList.txt"
echo Listing all .fig files in %SEARCH_DIR% and subfolders:
echo File list generated on %DATE% at %TIME% > "%OUTPUT_FILE%"
set /a FILE_COUNT=0
set /a MODULUS=200
for /R "%SEARCH_DIR%" %%i in (*.fig) do (
echo %%i >> "%OUTPUT_FILE%"
set /a FILE_COUNT+=1
set /a RESULT=!FILE_COUNT! %% !MODULUS!
if !RESULT! equ 0 (
echo Number of .fig files found so far: !FILE_COUNT!
)
)
echo List of .fig files saved to %OUTPUT_FILE%
echo **********Search complete**********
echo PROCESS COMPLETE >> "%OUTPUT_FILE%"
endlocal
  1 件のコメント
KAE
KAE 2024 年 1 月 31 日
Thanks for working on this!

サインインしてコメントする。

その他の回答 (1 件)

Austin Fite
Austin Fite 2025 年 4 月 21 日
編集済み: Austin Fite 2025 年 4 月 21 日
This is an old thread at this point but I have a file exchange utility "fsfind" that is purpose-built for this application.
The inputs support regular expressions (see documentation for "regexp") and only subfolders that match the pattern will be searched. I use it to efficiently search very deep directory structures (10+ levels).
  1 件のコメント
Adam Danz
Adam Danz 2025 年 4 月 21 日
+1 thanks for sharing

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by