Friday, September 21, 2012

Batch search for files - With sub-directories

Well took a while...

Why on earth would I need such a thing? 
Actually I was looking for a solution to map all the *.c & *.cpp in these folders and to add them all to the file, I'm trying to integrate aubio native library, and there are tones of files...

Why the Batch?
Sometimes you get carried away with your initial thought, and that's what happened, I started off trying to use the dir command to accomplish this, but it is impotent, then got carried away in Batch-Land...

Since searching into sub-directories is a recursive action, I have implemented this in two batch files, a runner and the search itself.

So here is the runner:

@echo off

set keyPhrase=%1
set yourDir=%~dp0
set outputFileName=output
echo echo output for search > %outputFileName%.bat

call search-in-subdirectories.bat %yourDir% %keyPhrase%
cls call %outputFileName%.bat pause

And here is the search itself (search-in-subdirectories.bat):

echo Dir: %1

for %%a in ("%1\%2") do echo echo %%~fa >> %outputFileName%.bat

for /d %%a in ("%1\*") do call %0 %%~fa %2

Simply locate both runner and search batches in some folder, and call the runner with the search parameter e.g.:

runner.bat *.c

Share your thoughts...

I could always make this better, but it works... and I don't have the time to abstract this further...

No comments:

Post a Comment