The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
I am trying to create a batch file that creates a text file with a count of files. I have that code, but I would like to modify it to only count for files of a specific type (*.mp3, *.ogg, *.flac).
The code I have has so many options in here I don't know where and how to insert the filter for the file types. Can anybody help?
@echo off
setlocal enabledelayedexpansion
set folder=F:\112
for /f "tokens=* delims=" %%v in ('dir /a:d /s /b "%folder%"') do (
for /f "tokens=* delims=" %%i in ('dir /a:-d /b "%%v" 2^>^&1') do (
if /i %%i NEQ "File Not Found" call set /a count=%%count+1
)
echo %%~nv: !count! >> textfile.txt
set count=0
)
I didn't even know you could DO For loops in batch files... is that a new feature? I remember having to do complex setups with ONERRORLEVEL GOTOs or something like that. But I digress....
I think it's the IF that needs to be changed, as the Dir commands in the For Loops (which would be ideal to do the filtering at) can't seem to be given both a folder path with this subdirectory search and a filename mask as far as I can tell.
Working on it...this would be 1000% easier to do with a real programming language though. Is there a reason why you want it as a batch file particularly? As far as I can tell, there's no way with Batch Files to do a "Like" comparison, or to compare a portion of a string (the string, in this case, being a file path), or to see if a string contains a substring (such as ".mp3").
Yes you can do for loops in batch files, this is an NT thing.
I can't help you with your problem, but I'd recommend picking up VB or something just to make it easier on you. Managing stuff like that will be a nightmare in about 8 weeks from now.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
I didn't even know you could DO For loops in batch files... is that a new feature? I remember having to do complex setups with ONERRORLEVEL GOTOs or something like that. But I digress....
I think it's the IF that needs to be changed, as the Dir commands in the For Loops (which would be ideal to do the filtering at) can't seem to be given both a folder path with this subdirectory search and a filename mask as far as I can tell.
Working on it...
yeah do for loops in a batch file.... my coding skills are so out of date that I was like whaaat? also. The current code works fine so far, but counts all the files with album art and such. I've been trying to work on my ID3 tags recently with the program MP3 Tag, but the program bombs out when opening a specific folder with 27 files (*.mp3). With the massive data I'm opening I was like how the F! will I ever find out where my issue is, then I just started the big google train and bam found some code here and there and ended up with the batch file here.
I can't help you with your problem, but I'd recommend picking up VB or something just to make it easier on you. Managing stuff like that will be a nightmare in about 8 weeks from now.
hopefully 8 weeks from now I won't need this. Just a one time deal. When I find my problem folder with the 27 songs I'll just delete the damn thing and rip the CD again.
Snag VB.NET Express to do this, and you will thank yourself a million times over. 100% free, and you can use the System.IO library's methods to VERY easily iterate through a directory structure in a clear and concise way.
If you need help with VB.NET, let us know (in case you've not worked with it already), but even if you've never ever used it before it'll be easier to make and maintain than hacking together an extremely ugly batch file.
In total honesty, I don't even know if there's a way to do what you want with batch file programming. Someone somewhere has probably found some kind of a hackjob to search for substrings within other strings... but imagine the horror combining two batch file hackjobs together to try and make a working batch file application. The horror. The horror. Save yourself from that.
What you want to do in VB.NET is maybe 2 or 3 more lines than what you have and it's a lot easier to tell if somethings messed up. You could be looking at that batch file for months unless you're some super batch guru.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
And trust me, the only people who would compliment you on hacking together such a batch file would be geeks like us. But even we will only remember it for a moment. There's only - at best - fleeting glory in batch file programming. Save yourself from it. That is our Advice. Our Help will come if you decide to switch to something else (like VB.NET) to solve this problem, and you haven't worked with VB.NET before.
They should never, ever have added programming constructs to batch files. What were they thinking? That's like adding an erratic robotic arm to a television so that you can give it a bat and chuck baseballs at the screen while it flails the bat around.
@echo off
setlocal enabledelayedexpansion
set folder=E:\Temp\b
set extension=abc
set count=0
for /f "tokens=* delims=" %%v in ('dir /a:d /s /b "%folder%"') do (
for /f "tokens=* delims=" %%i in ('dir /a:-d /b "%%v\*.%extension%" 2^>^&1') do (
if /i "%%i" NEQ "File Not Found" call set /a count=%%count+1
)
echo %%~nv\*.%extension%: !count! >> textfile.txt
set count=0
)
Change extension to whatever extension you want, e.g. "mp3", "ogg", or whatever. Change to * if you want to count all files.
Also, fixed a bug for you on the if statement.
You wanted to double quote "%%i", otherwise the comparison against "File Not Found" won't work due to the spaces, I think.
Edit: Added indentation for the fellow programmers out there.
Edit2: Added extension information into the output so that you know what you were looking for.
What's this then, that appears at the end of the for loop statement you changed?
2^>^&1'
Just out of personal curiosity. That seems... insane to me. I cannot discern meaning from it, even after looking up modern batch programming syntax for... probably too long this evening.
Anyways, well done at finding the part that needed changing! Yikes.
What's this then, that appears at the end of the for loop statement you changed?
2^>^&1'
Just out of personal curiosity. That seems... insane to me. I cannot discern meaning from it, even after looking up modern batch programming syntax for... probably too long this evening.
Anyways, well done at finding the part that needed changing! Yikes.
Oh. If I don't miss my guess, that redirects standard error to standard output.
I imagine it's for redirecting "File Not Found" so that it can be compared against.
Although, personally, I would've just done a 2>NUL which redirects standard error to the bit bucket, and ignored anything from standard error, since... well, that's what the next statement does (or tries to, I imagine dir can spit out a lot more errors than "File Not Found", but for a simple implementation like this, I'll let it go).
@echo off
setlocal enabledelayedexpansion
set folder=E:\Temp\b
set extension=abc
set count=0
for /f "tokens=* delims=" %%v in ('dir /a:d /s /b "%folder%"') do (
for /f "tokens=* delims=" %%i in ('dir /a:-d /b "%%v\*.%extension%" 2^>^&1') do (
if /i "%%i" NEQ "File Not Found" call set /a count=%%count+1
)
echo %%~nv\*.%extension%: !count! >> textfile.txt
set count=0
)
Change extension to whatever extension you want, e.g. "mp3", "ogg", or whatever. Change to * if you want to count all files.
Also, fixed a bug for you on the if statement.
You wanted to double quote "%%i", otherwise the comparison against "File Not Found" won't work due to the spaces, I think.
Edit: Added indentation for the fellow programmers out there.
Edit2: Added extension information into the output so that you know what you were looking for.
thanks for the help. Had a busy weekend and just got back to this. Certainly does what I need it to do.
Posts
I think it's the IF that needs to be changed, as the Dir commands in the For Loops (which would be ideal to do the filtering at) can't seem to be given both a folder path with this subdirectory search and a filename mask as far as I can tell.
Working on it...this would be 1000% easier to do with a real programming language though. Is there a reason why you want it as a batch file particularly? As far as I can tell, there's no way with Batch Files to do a "Like" comparison, or to compare a portion of a string (the string, in this case, being a file path), or to see if a string contains a substring (such as ".mp3").
Yes you can do for loops in batch files, this is an NT thing.
I can't help you with your problem, but I'd recommend picking up VB or something just to make it easier on you. Managing stuff like that will be a nightmare in about 8 weeks from now.
yeah do for loops in a batch file.... my coding skills are so out of date that I was like whaaat? also. The current code works fine so far, but counts all the files with album art and such. I've been trying to work on my ID3 tags recently with the program MP3 Tag, but the program bombs out when opening a specific folder with 27 files (*.mp3). With the massive data I'm opening I was like how the F! will I ever find out where my issue is, then I just started the big google train and bam found some code here and there and ended up with the batch file here.
Steam
XBOX
hopefully 8 weeks from now I won't need this. Just a one time deal. When I find my problem folder with the 27 songs I'll just delete the damn thing and rip the CD again.
Steam
XBOX
If you need help with VB.NET, let us know (in case you've not worked with it already), but even if you've never ever used it before it'll be easier to make and maintain than hacking together an extremely ugly batch file.
In total honesty, I don't even know if there's a way to do what you want with batch file programming. Someone somewhere has probably found some kind of a hackjob to search for substrings within other strings... but imagine the horror combining two batch file hackjobs together to try and make a working batch file application. The horror. The horror. Save yourself from that.
What you want to do in VB.NET is maybe 2 or 3 more lines than what you have and it's a lot easier to tell if somethings messed up. You could be looking at that batch file for months unless you're some super batch guru.
They should never, ever have added programming constructs to batch files. What were they thinking? That's like adding an erratic robotic arm to a television so that you can give it a bat and chuck baseballs at the screen while it flails the bat around.
Change extension to whatever extension you want, e.g. "mp3", "ogg", or whatever. Change to * if you want to count all files.
Also, fixed a bug for you on the if statement.
You wanted to double quote "%%i", otherwise the comparison against "File Not Found" won't work due to the spaces, I think.
Edit: Added indentation for the fellow programmers out there.
Edit2: Added extension information into the output so that you know what you were looking for.
I don't know how anyone can parse the mess that is batch file programming. Well done. It looks like that would do it.
It's not that bad, actually. I think there's room for improvement in some of the variable names in this particular case, but that's just me.
2^>^&1'
Just out of personal curiosity. That seems... insane to me. I cannot discern meaning from it, even after looking up modern batch programming syntax for... probably too long this evening.
Anyways, well done at finding the part that needed changing! Yikes.
Oh. If I don't miss my guess, that redirects standard error to standard output.
I imagine it's for redirecting "File Not Found" so that it can be compared against.
Although, personally, I would've just done a 2>NUL which redirects standard error to the bit bucket, and ignored anything from standard error, since... well, that's what the next statement does (or tries to, I imagine dir can spit out a lot more errors than "File Not Found", but for a simple implementation like this, I'll let it go).
Yeah. You need that ampersand to differentiate between file descriptor 1 (standard output) and a file with the name "1".
e.g.
dir *.some_extension_that_does_not_exist 2>1
will create a file with the name "1" which has the contents
File Not Found
compared to:
dir *.some_extension_that_does_not_exist 2>&1
which just displays File Not Found on screen (but as part of standard output).
thanks for the help. Had a busy weekend and just got back to this. Certainly does what I need it to do.
Steam
XBOX