views:

53

answers:

2

Probably far too easy question, but how do I match a file extension such as .jpg while not matching jpg~ (i.e. a jpg that a program has made a local copy of?) My current line is:

for /f %%a in ('dir /b *.jpg') do echo %%~na

But if any program has a copy of one of the files open (and thus has made a .jpg~ file) this regexp will match those too. I found a reference to $ being the 'end of line', but doing this doesn't work at all:

for /f %%a in ('dir /b *.jpg$') do echo %%~na
+1  A: 

I think, the problem arises from the short-name representation. (Use dir /X and you can see that xxx.jpg and xxx.jpg~ both have a 8.3 file-name that ends with .jpg.)

Javaguru
Ah, that's interesting. So despite the fact that dir is using the /b option, it still matches on the 8.3 filenames? Oh, Windows...
Stephen
@Stephen: Why should `/b` change how file names are matched? If just controls how the results are output.
Joey
Well, it entirely depends on how they implemented the matching, but I would have thought that if one knew the user wanted the output in full non-8.3 format, they'd match on the full name. Then again, I don't know how much, if any, the language changed after non-8.3 names came out.
Stephen
+3  A: 

I don't think it is possible to filter this with just a FOR command (unless you pipe the output of dir to findstr) but in this case, adding a simple if test is all that is needed:

for %%A IN (*.jpg) DO if "%%~xA"==".jpg" @echo %%~A
Anders
Excellent, thanks!
Stephen
This IMHO retarded behavior is confirmed by http://support.microsoft.com/?kbid=164351
Anders