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
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Dash
  • 23
  • 1
  • 5
  • Possible duplicate of [How to properly -filter multiple strings in a PowerShell copy script](http://stackoverflow.com/questions/18616581/how-to-properly-filter-multiple-strings-in-a-powershell-copy-script) – user4317867 Feb 15 '16 at 00:25
  • @user4317867 Edited in why that's not correct. – Dash Feb 15 '16 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` – user4317867 Feb 15 '16 at 22:00

2 Answers2

3

Powershell FTW!

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

Get-ChildItem C:\temp -Include $extensions -Recurse
aifarfa
  • 3,939
  • 2
  • 23
  • 35
Alexis Coles
  • 1,227
  • 14
  • 19
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.

  • You'll better make a list : `set $ext=*.jpg *.tif *.png` and then `dir /s/b c:\%$List%` +1 – SachaDee Feb 14 '16 at 23:19
  • +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. – dbenham Feb 15 '16 at 01:51
  • See `pushd /?` - also see Setting a UNC working directory from a shortcut at https://en.wikipedia.org/wiki/Batch_file#Setting_a_UNC_working_directory_from_a_shortcut - the last two paragraphs (written by me) –  Feb 15 '16 at 23:47