I created a few test files and tried it...
Code: Select all
D:\>dir d:\test\*.xml
Volume in drive D is Data
Volume Serial Number is C6F9-6F22
Directory of d:\test
29/08/2012 09:45 68 FILE_20120802_yoab_001.xml_bak
29/08/2012 09:46 75 FILE_20120801_apgh_005.Xml_BAK
29/08/2012 09:46 75 FILE_20120801_apgh_005.Xml
29/08/2012 09:45 43 FILE_20120801_stsi_001.XML
29/08/2012 09:45 68 FILE_20120802_yoab_001.xml
29/08/2012 09:47 170 FILE_20120810_cvia_167.Xml
6 File(s) 499 bytes
0 Dir(s) 2.169.847.808 bytes free
D:\>
...but it doesn't seem to work. The files with "xml_bak" extension still show.
dbenham wrote:Use FINDSTR regex to filter the results
Code: Select all
for /f "eol=: delims=" %F in ('dir /b /a-d *.xml ^| findstr /rix ".*\.xml"') do @echo %F
Dave's solution *does* work. Here's the output:
Code: Select all
D:\test>for /f "eol=: delims=" %F in ('dir /b /a-d *.xml ^| findstr /rix ".*\.xml"') do @echo %F
FILE_20120801_apgh_005.Xml
FILE_20120801_stsi_001.XML
FILE_20120802_yoab_001.xml
FILE_20120810_cvia_167.Xml
D:\test>
However, I want to avoid the "dir" command because the directory contains almost 100K files, while continuously new files are being added. I have a feeling that "dir" is going to be slow in this case. I definitely will keep the FINDSTR regex solution in the back of my mind. Maybe I can let the FOR loop (wrongly) spit out the "*.xml_bak" files and do the check afterwards in the processing part.
Liviu wrote:Actually, adding that extra IF would be my recommended solution.
I've started playing with the extra IF and it works. But as you mentioned, not in a general way. In fact in my script I created a variable (SET strFileFilter=*.XML) which I use in the FOR loop. This variable will need to change when I execute the script on another directory. Take "*.docx" for instance. The original problem would never occur here, so the extra IF is not necessary in this case. Mmm... creating generic scripts is tough
(About the ".XML" file, that was a typo. I was referring to files with a ".XML" extension.)
While trying to figure this all out, I noticed that when I'm a little more specific in the strFileFilter variable it works. It seems that if you can construct the filter in such a way that it probably never matches any 8.3 filename, the problem does not appear.
A filter of "FIL*.XML" apparently is enough. Being curious about why this is the case, I found the explanation in the Overview part of
http://en.wikipedia.org/wiki/8.3_filename.
My lesson learned: when filtering on files with an extension of 3 characters or less, chances are that you get in conflict with the 8.3 filenames so be as specific as possible in the filename part. If this is not possible, add an extra check genre IF or FINDSTR regex.
Thanks guys!
-=Wim=-
EDIT: It seems that a filter of "FIL*.XML" is not enough, at least not for the first 4 files. As mentioned in the above wikipedia article, the first 4 files have a short filename that contains the first 6 characters from the long filename, then a ~ and a digit, followed by a period and the first 3 characters of the extension. The filter should at least contain the first 7 characters of the long filename to avoid conflicts with the short filename. In this case I could use "FILE_????????_*.xml". Annoying problem....