How to read a text file's name?

I have a folder named “C:\Desktop\Folder” and inside it, one file: “abc.txt” along with plenty of the files. I am using a for loop to read each file and if the file name abc.txt is found I want to read its content and store in another variable. I know I have to use the If condition here. How do I read the name of the file?

Hello. It might be easier to help if you showed a snippet of your workflow, like a screenshot.
But here are some tips that will help.

Let’s assuming you use .GetFiles() or .GetEnumerateFiles()
So, then, you can check the filename using Path.GetFilename or Path.GetFilenameWithoutExtension

It would look like this:

For each file In Directory.GetFiles(foldername, "*.txt")
    If activity: condition: Path.GetFilenameWithoutExtension(file).ToLower.Equals("abc")
         <perform actions>

Note: You can also use File.Exists() instead rather than getting all files.
System.IO.File.Exists( Path.Combine(foldername, "abc.txt" )

Regards.

1 Like

@swetha_pattabhi Here you go GetFilesFromDirectory.zip (11.5 KB)