2

I need to make a script using CMD/Powershell that can find all files in a directory (and subdirectories) that end in extensions associated with media files. I have a Unix script that can do this:

 find /directory/ | egrep -e ".*.(jpg|tif|png|gif|wav|mp3|ogg|flac|wma|aac|m4a|flv|webm|ogv|gif|gifv|avi|wmv|mp4|mpg|3gp)"

(I'm sure there's a better way to do this, but it works, and has no problems for me.)

However, I need to do this for Windows. The file types are as follows:

  • jpg
  • tif
  • png
  • gif
  • wav
  • mp3
  • ogg
  • flac
  • wma
  • aac
  • m4a
  • flv
  • webm
  • ogv
  • gif
  • gifv
  • avi
  • wmv
  • mp4
  • mpg
  • 3gp

Edit: Even though I got the right answer, the possible duplicate is not correct. This is because I needed it to be outputted to a file. My script that I wrote using bgalea's answer is as follows:

@ECHO OFF
set $ext=*.jpg *.tif *.png *.gif *.wav *.mp3 *.ogg *.flac *.wma *.aac *.m4a *.flv *.webm *.ogv *.gif *.gifv *.avi *.wmv *.mp4 *.mpg *.3gp
dir /s/b c:\Users%$ext% > mediafiles.txt
echo The locations of the media files were copied to mediafiles.txt 
pause
3
  • Possible duplicate of How to properly -filter multiple strings in a PowerShell copy script Commented Feb 15, 2016 at 0:25
  • @user4317867 Edited in why that's not correct. Commented Feb 15, 2016 at 15:29
  • Then we'd use Get-ChildItem $originalPath\* -Include *.gif, *.jpg, *.xls*, *.doc*, *.pdf*, *.wav*, .ppt* to list the items, look up the FullName property and finally output that to a text file. (gci -Path C:\Scripts\* -Include *.csv, *.txt).fullname | out-file -FilePath c:\temp\test.txt;ii c:\temp\test.txt Commented Feb 15, 2016 at 22:00

2 Answers 2

3

Powershell FTW!

$extensions = @("*.jpg", "*.tif"...ect ect)

Get-ChildItem C:\temp -Include $extensions -Recurse
Sign up to request clarification or add additional context in comments.

Comments

1
 dir /s /b c:\*.3gp c:\*.mpg* c:\*.mp4

Etc

C:\ says to start at c:. /s does sub folders, /b gives only names.

3 Comments

You'll better make a list : set $ext=*.jpg *.tif *.png and then dir /s/b c:\%$List% +1
+1, But you have an unwanted * after c:\*.mpg. Also, you can save some typing by PUSHD C:\ before running the command, so that you don't need to include the path with every extension.
See pushd /? - also see Setting a UNC working directory from a shortcut at en.wikipedia.org/wiki/… - the last two paragraphs (written by me)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.