1

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!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
7thGen
  • 63
  • 1
  • 10
  • 1
    Possible duplicate of [Print directory tree but exclude a folder on windows cmd](https://stackoverflow.com/questions/43810090/print-directory-tree-but-exclude-a-folder-on-windows-cmd) – Vlam Mar 04 '19 at 07:54
  • 1
    As the previously mentioned duplicate, is for an non requested tag/scripting language, the simplest non `PowerShell` method would be to use the `/V` option with `Find` or `FindStr`, _but doing so may not be the quickest_. Perhaps you'd be better off with filtering the directories to be searched at the outset, by nesting a `For` loop inside another, with the outer one filtering the directories and the inner recursively searching what it returns. – Compo Mar 04 '19 at 08:38
  • 1
    Simplest would be to do dir with findstr. `dir /b /s *.mp3 | findstr /vi "microsoft" | findstr /vi "windows` – Gerhard Mar 04 '19 at 12:35

1 Answers1

3

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 >>.

aschipfl
  • 33,626
  • 12
  • 54
  • 99