To list all files in a folder that contain a specific string, such as "abc", you can use the dir
function to get a list of all files in the directory and then use contains
function to check if the file names or contents contain the specific string.
Here's the code:
main.m645 chars21 lines
In this code, we first define the folder path (folder_path
). Then we use the dir
function to get a list of all files in the folder, which is stored in a structure array (file_list
).
The for
loop iterates through each file in the array. We first check if the file is a regular file (not a folder) using isdir
attribute. If the file is a regular file, we get the file name (file_name
) and check if it contains the string "abc" using the contains
function.
If the file name does not contain the string "abc", we also check the contents of the file using the fileread
function, which reads the contents of a file and returns the result as a string. If the contents of the file contain the string "abc", we display the file name using the disp
function.
gistlibby LogSnag