Read files and automatically put in the desired line;

Asked

Viewed 137 times

-1

I have several files in one folder: 01.PNG, 02.PNG, 05.PNG, some of the files do not have (04.PNG, 03.PNG).

What would be the easiest way to write a text document where each file name would be written in the same line number:

Thus:

01.PNG
02.PNG


05.PNG

NOTE THAT THE MISSING FILES, THE LINES WERE JUST SKIPPED!

  • 1

    Have you tried anything? What was the difficulty?

  • I’m looking for the easiest language to do that. As I will do in excel, I started looking for there, but I accept a solution in python or any other tbm! Know if you can do it for windows bash?

2 answers

1

In C# I use the following code to read the files, ordered by creation date:

using System.IO;

string path=@"C:\ExampleArquivos"; \\caminho onde estão os arquivos

var files = new DirectoryInfo(pathexportacao).GetFiles().OrderBy(x => x.CreationTime).ToList();

After reading the files, you can perform a foreach to save the name in a txt.

string path=@"C:\Example\Example.txt";

foreach (var file in files)
        {  
            File.Create(path).Dispose();
            using(TextWriter tw = new StreamWriter(path))
            {
              tw.WriteLine(file.Name);
              tw.Close();
             }
        }
  • Vlw! Thank you very much

  • I only had the test today! Do you use visual studio to program in C#? As I am new I may have done something wrong but for what it seems to me is always overwriting in the text file and so when I open the file only has a line written.

  • Then try concatenating with n, like this: tw.Writeline(file. Name + " n");

0

As you said you will use excel, I recommend developing this in VBA. It will not evade the concept of your problem and is a language with very (very) support of people on the internet.

I do not program in VBA, but you understanding its basis will not have secret...

novo: arquivo
se > arquivo existe 
imprime: nome, quebra a linha
se > arquivo não existe
imprime: quebra linha, quebra linha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.