简体   繁体   中英

A bat file to recursively traverse and find certain file type

I am trying to recursively traverse through a directory and find exe files in it.

This is the folder structure

C:\MyCave\iso\SDG\cmpdir

---------------\FolderA
------------------abc.txt
---------------\FolderB
------------------def.exe
---------------\FolderC
------------------ghi.dll

The below is my bat sinppet.

set f="C:\MyCave\iso\SDG\cmpdir\test-recur"
for /r %%f in ("*.exe") do if exist %%f echo %%f

Though it is working and listing out exe files in C:\MyCave\iso\SDG\cmpdir , but I want to list exe files present only in C:\MyCave\iso\SDG\cmpdir\test-recur directory.

This is my current output.

C:\MyCave\iso\SDG\cmpdir>exeRecur.bat

C:\MyCave\iso\SDG\cmpdir>set f="C:\MyCave\iso\SDG\cmpdir\test-recur"

C:\MyCave\iso\SDG\cmpdir>for /R %f in ("*.exe") do if exist %f echo %f

C:\MyCave\iso\SDG\cmpdir>if exist C:\MyCave\iso\SDG\cmpdir\cp.exe echo C:\MyCave\iso\SDG\cmpdir\cp.exe
C:\MyCave\iso\SDG\cmpdir\cp.exe

C:\MyCave\iso\SDG\cmpdir>if exist C:\MyCave\iso\SDG\cmpdir\dirB\Abcd.exe echo C:\MyCave\iso\SDG\cmpdir\dirB\Abcd.exe
C:\MyCave\iso\SDG\cmpdir\dirB\Abcd.exe

C:\MyCave\iso\SDG\cmpdir>if exist C:\MyCave\iso\SDG\cmpdir\test-recur\2\def.exe echo C:\MyCave\iso\SDG\cmpdir\test-recur\2\def.exe
C:\MyCave\iso\SDG\cmpdir\test-recur\2\def.exe

C:\MyCave\iso\SDG\cmpdir>if exist C:\MyCave\iso\SDG\cmpdir\wspace\defg.exe echo C:\MyCave\iso\SDG\cmpdir\wspace\defg.exe
C:\MyCave\iso\SDG\cmpdir\wspace\defg.exe

I know there is a small mistake somewhere. Requesting your help in solving it.


Based on Gerhard's and sst's answer I have modified my script like this.

set rootFolder=%1
set destFolder=%2
for /r %rootFolder% %%f in ("*.exe") do if exist %%f move %%f %destFolder%

I am passing command line arguments to bat like this...

exeRecur.bat "C:\MyCave\iso\SDG\cmpdir\test-recur" "C:\MyCave\iso\SDG\cmpdir\dirA"

Like this hard-coding of paths is avoided.

Regards

You forgot to specify the 'root path' argument of FOR /R , So it begins the enumeration from the current directory.

You should use a different name for your environment variable to avoid confusion with the FOR's parameter %%f .

set "RootFolder=C:\MyCave\iso\SDG\cmpdir\test-recur"
for /r "%RootFolder%" %%f in (*.exe) do echo %%f

You should also use the extended syntax of the set command: set "var=value" and use "%var%" wherever quoting is needed.

Since you are using the wildcard *.exe , There is no need for if exist in FOR /R as it will only enumerate the existing files.

Ok, so you are setting a variable:

set f="C:\MyCave\iso\SDG\cmpdir\test-recur"

Firstly, it is bad practice to set single char variables, but more importantly is the way you wrap it with double quotes. It should be from the beginning of the variable to the end of the value.

set "f=C:\MyCave\iso\SDG\cmpdir\test-recur"

The real problem though is that you never tell the for /r loop where to recursively do the search.

set "mypath=C:\MyCave\iso\SDG\cmpdir\test-recur"
for /r "%mypath%" %%f in (*.exe) do echo %%~f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM