Why does dir command find files not following pattern in Windows?
25 views (last 30 days)
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
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
return nothing in that they exclude the files with three characters after the .dat, but
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
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
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
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.*'));