Question

I have loads of images in a folder called X:\myfolder\photos

How to extract all the names of the files in the above folder location and put it in a txt file or csv file. Can I do this in c# in a console application? or even just use cmd without creating an application?

Was it helpful?

Solution 2

Please see below code for your query. You can use this code in console application and use it.

    static void Main(string[] args)
    {
        GetFileNamesandWriteInTxt();
    }

    public static void GetFileNamesandWriteInTxt()
    {
        string path = @'X:\myfolder\photos';
        DirectoryInfo dr = new DirectoryInfo(path);
        FileInfo[] mFile = dr.GetFiles();
        StreamWriter writer = new StreamWriter("D:\\FileNames.txt", true);
        foreach (FileInfo fiTemp in mFile)
        {
            writer.WriteLine(fiTemp.Name);
            Console.WriteLine(fiTemp.Name);
        }
        Console.ReadLine();
      }

Hope this will work for you!

Thanks, Anshul

OTHER TIPS

You can use Directory.EnumerateFiles or GetFiles and File.WriteAllLines:

var allPaths = Directory.EnumerateFiles(@"X:\myfolder\photos");
File.WriteAllLines(pathToCsvFile, allPaths);

If you only want the name of the files and not the complete paths use Path.GetFileName:

File.WriteAllLines(pathToCsvFile, allPaths.Select(p => Path.GetFileName(p)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top