Why does dir command find files not following pattern in Windows?
11 views (last 30 days)
Show older comments
I have series of files that I am trying to pick up with a dir command. I'm able to get the files, but I'm also picking up one extra, only while running the command in Windows, and I'm curious what syntax change I need to make to exclude the extra file.
flist = dir([parentdirectory,'\outputfiles.dat.*']);
Using the above command in a linux box I get only 'outputfiles.dat.###' files. Using the command in windows I get 'output.dat.###' and 'output.dat'. I would like to not get the 'output.dat' file.
Working in 2016b
4 Comments
Stephen23
on 7 Apr 2021
Edited: Stephen23
on 7 Apr 2021
"on NTFS and matlab would 3. match T=23.dat but not T=23.5.dat ? "
>> dir 3.
'3.' not found.
>> dir 23.
'23.' not found.
>> dir 23*
23.5.dat 23.dat
>> dir 23.*
23.5.dat 23.dat
"Does .* match multiple extensions? I suspect that it does."
I see no reason why it shouldn't, the DIR documentation does not place any restriction on what the wildcard can match:
>> dir A.*
A.B A.B.001 A.B.002 A.B.003
This example made me laugh:
>> dir *3.
A.B.003
>> dir *3.*
23.5.dat 23.dat
There it is in a nutshell, ladies and gentlemen.
Walter Roberson
on 7 Apr 2021
Edited: Walter Roberson
on 7 Apr 2021
Darn editor converted my wildcard to bold :(
But it is clear from what you posted that * does match multiple extensions
Accepted Answer
dpb
on 7 Apr 2021
That's the behavior of the OS dir command under Windows; '*' matches anything including nothing.
Unfortunately, in typical MS style, using "???" doesn't work either, it appears. If I try something like
file.dat.001
file.dat.002
...
dir file.dat.?
or
dir file.dat.??
return nothing in that they exclude the files with three characters after the .dat, but
dir file.dat.???
behaves the same as does the *
Under CMD.EXE, I think you'll be forced to either change the naming convention to be sure you have some character after the second dot that you can be sure will match -- for the above file pattern, either
dir file.dat.0*
dir file.dat.0??
work as desired. Of course, with three places, the leading 0 limits you to 100 files maximum so for more than that you would need another placeholder.
The JPSoftware CMD replacement TCC (TakeCommand Console) that I use has many extended facilities above and beyond CMD; one is the ability to write
dir file.dat.[0-9]*
which requires a match of a digit in the first position after the second dot which solves the problem neatly. Of course, you have to have the JPSoft command replacement in order to take advantage of such features (as would anybody else who tried to use any code taking advantage of the feature, of course). Hence, while it's neat it's probably not a solution you care about.
I think you'll simply have to check that the returned extension from
[d,f,e]=fileparts(filename);
is not empty.
NB: fileparts isn't terribly robust; it will return '.002' as the extension from the above, but will return 'file.dat' as the name; it only looks for the last dot/period found in the string.
As a stylistic point and for some help in coding, I'd suggest using fileparts to build the fully qualified name instead of explicit string catenation--it has the nicety of not requiring you to insert the dividers between filename sections and also automagically uses the system-dependent character.
flist = dir(fullfile(parentdirectory,'outputfiles.dat.*'));
25 Comments
Stephen23
on 9 Apr 2021
Edited: Stephen23
on 9 Apr 2021
@dpb: thank you for the explanation! I got the feeling that I could not see something obvious that everyone else could see: temporary forum-blindness, perhaps.
This is useful information, it could be applied to make the regular expression more targeted.
dpb
on 9 Apr 2021
.." temporary forum-blindness, ..."
Easy enough -- the interaction was pretty prolonged, albeit interesting and some constrasting viewpoints. :)
Wonder what OP finally ended up with for a solution...
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!