MATLAB Answers

Why does dir command find files not following pattern in Windows?

25 views (last 30 days)
Bob Thompson
Bob Thompson on 6 Apr 2021
Commented: dpb on 9 Apr 2021
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
Walter Roberson
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

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!