Page 1 of 1

Need some help

Posted: 25 Sep 2011 18:07
by mattman5791
Hey guys I need some help with a batch file I am creating. I can't figure out how to list these media files on my computer
.mp3, mpa, .asf, .avi, .flv, .mov,
.mp4, .mpg, .swf, .vob, .wmv

I know I need to use dir but I can't figure it out. Any help would be appreciated!

Thanks!

Re: Need some help

Posted: 25 Sep 2011 19:06
by Voodooman
create text file with extensions, one per line
like this
ext.txt

Code: Select all

mp3
mpa
asf
avi
flv
mov
mp4
mpg
swf
vob
wmv



Code: Select all

setlocal enabledelayedexpansion
for /f "tokens=* Delims=" %%a in (
ext.txt
) do (
set ext=%%a
dir *.!ext! /b /o:gn >>list.txt
)


This will list only filenames with extensions and store them in list.txt.

Or you can generate it from bat file:

Code: Select all

setlocal enabledelayedexpansion
echo off
echo mpa>>ext.txt
echo asf>>ext.txt
echo avi>>ext.txt
echo flv>>ext.txt
echo mov>>ext.txt
echo mp4>>ext.txt
echo mpg>>ext.txt
echo swf>>ext.txt
echo vob>>ext.txt
echo wmv>>ext.txt
for /f "tokens=* Delims=" %%a in (
ext.txt
) do (
   set ext=%%a
   dir *.!ext! /b /o:gn>>list.txt
)
del /q /f ext.txt
pause

Re: Need some help

Posted: 26 Sep 2011 01:11
by Ed Dyreen
'

Code: Select all

@echo off &SetLocal EnableDelayedExpansion

set "$Field=mpa asf avi flv mov mp4 mpg swf vob wmv"
for %%! in (

  !$Field!

) do for /f "usebackq tokens=*" %%! in (

  `dir "*.%%~!" /b /o:gn`

) do echo.%%!

pause
EndLocal &exit /b
Untested !

Re: Need some help

Posted: 26 Sep 2011 08:53
by Voodooman
modified it a little to remove "file not found" from llist if no file with extenstion exist

Code: Select all

@echo off
SetLocal EnableDelayedExpansion
set "$Field=mpa asf avi flv mov mp4 mpg swf vob wmv"
for %%! in (
   !$Field!
) do (
   for /f "tokens=*" %%! in (
         'if exist ^"*.%%~!^" dir ^"*.%%~!^" /b /o:gn'
   ) do (
      echo.%%!
      echo.%%!>>list.txt
   )
)
pause
exit /b


Btw, what is %%! ? why it act so different and makes sing word as variable instead of whole line?
I never seen documentation of this, if only i knew i could use it i would not read extenstions from separate list.
Can you explain a little %%! and %%? or %%$ which you use quite offten?

Re: Need some help

Posted: 26 Sep 2011 21:34
by Ed Dyreen
'
enter this in your console:

Code: Select all

for /?

I am not good in explaining things, you can find more help on for digging DOSTips.

Re: Need some help

Posted: 21 Jul 2012 03:30
by Voodooman
so you just use ! instead of alphanumeric values?

Re: Need some help

Posted: 21 Jul 2012 03:37
by foxidrive
Using %%! %%? etc just confuses the newbies.

Re: Need some help

Posted: 21 Jul 2012 06:49
by Ed Dyreen
Voodooman wrote:Btw, what is %%! ? why it act so different and makes sing word as variable instead of whole line?
That has nothing to do with %%! ?, but rather with the default delimeters, {TAB}, {space}, {COMMA}
Voodooman wrote:Can you explain a little %%! and %%? or %%$ which you use quite offten?
I try,

Generally it is considered good programming style not to use variables when you don't have to.
These 'short lifespan' values can be represented with tokens like %a, %b, %? or even %!.
In the example above I used %! which I now consider bad style cause the exclamation mark is special and normally needs to be escaped in delayed expansion. This is problematic when writing macros.

So that leaves me with %a, %b or %?.

I suck at maths, but I do know my alphabet so I promote using tokens %a to %Z only when your for loop collects multiple 'tokens=1,2*'. In all other case, I use non-alphabetical tokens like %? to stand out.
Voodooman wrote:modified it a little to remove "file not found" from llist if no file with extenstion exist
I modify it again, you know it's faster to open a file for writing only once :)

Code: Select all

setlocal enableDelayedExpansion

set "$field=mpa,asf,avi,flv,mov,mp4,mpg,swf,vob,wmv"

> "list.txt" (

       for %%? in (

              !$field!

       ) do   for /f "delims=" %%? in (

              '2^>nul dir /b /o:gn "*.%%~?"'

       ) do   (echo.%%?)
)

type "list.txt"
pause
exit /b
Note that I did not use file redirection in my original post to prove it wasn't necessary to solve the OP's problem.

See ya, :wink:

Re: Need some help

Posted: 21 Jul 2012 08:30
by Squashman
Voodooman wrote:so you just use ! instead of alphanumeric values?

What made you come back to this thread 10 months later?

Re: Need some help

Posted: 21 Jul 2012 08:36
by Squashman
I realize the nice thing about looping through each extension one at a time is that you have each type then grouped together. But you could just do this down and dirty.

Code: Select all

dir /a-d /b /s *.mpa *.asf *.avi *.flv *.mov *.mp4 *.mpg *.swf *.vob *.wmv >VideoFiles.txt