Need some help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mattman5791
Posts: 4
Joined: 15 Apr 2011 12:01

Need some help

#1 Post by mattman5791 » 25 Sep 2011 18:07

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!

Voodooman
Posts: 12
Joined: 25 Sep 2011 07:45

Re: Need some help

#2 Post by Voodooman » 25 Sep 2011 19:06

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Need some help

#3 Post by Ed Dyreen » 26 Sep 2011 01:11

'

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 !

Voodooman
Posts: 12
Joined: 25 Sep 2011 07:45

Re: Need some help

#4 Post by Voodooman » 26 Sep 2011 08:53

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?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Need some help

#5 Post by Ed Dyreen » 26 Sep 2011 21:34

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

Voodooman
Posts: 12
Joined: 25 Sep 2011 07:45

Re: Need some help

#6 Post by Voodooman » 21 Jul 2012 03:30

so you just use ! instead of alphanumeric values?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need some help

#7 Post by foxidrive » 21 Jul 2012 03:37

Using %%! %%? etc just confuses the newbies.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Need some help

#8 Post by Ed Dyreen » 21 Jul 2012 06:49

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:

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Need some help

#9 Post by Squashman » 21 Jul 2012 08:30

Voodooman wrote:so you just use ! instead of alphanumeric values?

What made you come back to this thread 10 months later?
Last edited by Squashman on 21 Jul 2012 08:37, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Need some help

#10 Post by Squashman » 21 Jul 2012 08:36

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

Post Reply