Search code examples
batch-filecmddirfindstr

Want to exclude specific files with dir findstr


I want to find all *.csv files within a folder-structure except 2 files

This is my code

for /f "tokens=*" %%i IN ('dir /b /s *.csv | findstr /v /i  "\combinedold.csv" | findstr /v /i "\combined.csv"')

The 2 files are "combined.csv" and "combinedold.csv".


Solution

  • The basic command should be something like

    dir /s /b /a-d *.csv | findstr /v /i /e /c:"\\combinedold.csv" /c:"\\combined.csv"
    

    Now, to include it inside a for /f command, it is necessary to escape non quoted special characters (the pipe in this case), so it becomes

    for /f "delims=" %%i in ('
        dir /s /b /a-d *.csv ^| findstr /v /i /e /c:"\\combinedold.csv" /c:"\\combined.csv"
    ') do echo %%i