7thGen
7thGen

Reputation: 63

Exclude Windows folder in CMD Search

I am fairly new to Batch files but this is my Batch File for displaying path for Jpegs, Mp3, Mp4 etc.

@echo off
setlocal
cd /d C:\
Echo
echo Files Paths :
dir /b *.mp3 /s
dir /b *.mp4 /s
dir /b *.jpg /s
endlocal
pause

1.) Is there anyway that I can exclude Microsoft and Windows (wallpapers, icons, sounds, etc) folder from my search?

2.) How do I save the results in this output file (which is already created) C:\output.txt

Thanks!

Upvotes: 1

Views: 3288

Answers (1)

aschipfl
aschipfl

Reputation: 34909

This is quite an easy task for the findstr command:

dir /S /B /A:-D *.mp3 *.mp4 *.jpg | findstr /V /I /C:"\\Microsoft\\" /C:"\\Windows\\" > "C:\output.txt"

The \\ represent one literal \ in order to ensure that only directories become excluded whose full names match the predefined names. Since findstr uses the \ as an escape character \\ is necessary.

As you can see it is not necessary to use multiple dir commands. The filter option /A:-D excludes any directories to be returned even if they match one of the given patterns.

The returned data is written to a file using redirection. To append to the file rather than overwriting it replace > by >>.

Upvotes: 3

Related Questions