Search code examples
cmdfindstr

findstr exclude some subdirectories


I am using findstr /s to find what I am looking for. How can I exclude some sub directories? For example, Main Direc -> Sub folder 1, Sub folder 2, Sub folder 3, Sub folder 4

findstr /s /i /C:"SWG_DD_STANDARD_DATE" *.* > E:\Private\ORA_APPS_DEV\WO_WIP\ONE_OFF\NARAIN_ALL\Search\search.txt

How can I exclude sub folder 2 from my search?


Solution

  • You can iterate the list of folders and exclude the non required one

    (for /d %%a in (*) do if /i not "%%a"=="subfolder2" (
        findstr /s /i /C:"SWG_DD_STANDARD_DATE" "%%a\*"
    )) > E:\Private\ORA_APPS_DEV\WO_WIP\ONE_OFF\NARAIN_ALL\Search\search.txt
    

    Or, if the only need is to exclude the folder from the output, you can filter it

    findstr /s /i /C:"SWG_DD_STANDARD_DATE" * | findstr /v /b /i /c:"subfolder2" >  E:\Private\ORA_APPS_DEV\WO_WIP\ONE_OFF\NARAIN_ALL\Search\search.txt