MATLAB Answers

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

조회 수: 26(최근 30일)
Bob Thompson
Bob Thompson 2021년 4월 6일
댓글: dpb 2021년 4월 9일
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
Walter Roberson
Walter Roberson 2021년 4월 7일
Darn editor converted my wildcard to bold :(
But it is clear from what you posted that * does match multiple extensions

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2021년 4월 7일
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
dpb
dpb 2021년 4월 9일
.." 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...

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by