Clear Filters
Clear Filters

why I cannot list specify files with function dir

5 views (last 30 days)
I want to list all special files with specified extensions, such as all *.jpg files. I cd the matlab to the current folder, and call the dir function as following:
dir *.jpg
it does not return all file names but a struct with name attribute and other fields, like the following picture showing
If I call dir function as following:
dir
it will return all files
I just want to list all jpg files, but not including the root directory, how to use dir function, please?
  9 Comments
Guillaume
Guillaume on 9 May 2018
I wanted to see the end of the list of files. From the screenshot you show in KSSV answer, it looks like somehow you have a file actually called '*.jpg' in that directory. Something I didn't think was possible on Windows.
What is the output of
s = dir('*.jpg');
double(s.name)
li yan
li yan on 9 May 2018
It's really odd. I copy 3 pictures to another folder, and test like this:
s = dir('*.jpg');
it works well, it means that the variable s really stores 3 picture information as a struct. But if I copy all pictures to the folder, and re-test like above, it does not work well.
I'm sure that the folder does not contain a file named *.jpg.
last, I test dir in the current folder contains all files ( there is no file named *.jpg, I'm sure) using your code:
s = dir('*.jpg');
double(s.name)
the result is 42 46 106 112 103

Sign in to comment.

Accepted Answer

Jan
Jan on 9 May 2018
Edited: Jan on 9 May 2018
Guillaume is right: You have created a file called '*.jpg'. This is not possible under Windows with standard tools, but you can do this under Linux or with using evil tricks. I suggest to start your machine from a Linux live CD or USB stick and delete the file '*.jpg'.
You can install the Linux subsystem: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc771470(v=ws.11) (here for Windows 7) and delete the file from there.
This works for a trailing dot in the file name, which is illegal also:
system('del \\?\D:\Temp\asd.')
I cannot test this with a * in the name, because I'm not able to create such a file...
A chkdsk might fix the file name also.
  2 Comments
Guillaume
Guillaume on 9 May 2018
This is why I wanted a screenshot of the end of the file listing. Everything points to a file called *.jpg in the current directory.
How you would create such a file in Windows is a mystery. The WIN32 API won't let you. I suggest copying all the valid files into a new directory then deleting the current directory with the invalid *.jpg file (if the OS actually lets you).
li yan
li yan on 10 May 2018
Edited: li yan on 10 May 2018
Thank you for your help very much.
I do not know why there is a *.jpg file in the folder, even though I cannt see that file from explorer.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 9 May 2018
images = dir('*.jpg') ;
iwant = {images(:).name}' ;
  1 Comment
li yan
li yan on 9 May 2018
it does not work correctly.
images = dir('*.jpg') ;
this just return a struct with one element, like this:

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!