How to add prefix in each file

0

I'm looking for the easiest way to add a prefix to every file I have.

The situation is that I have PDF files which are in several sub folders, which makes access difficult. But these sub folders belong to the same call reports.

What I'm looking to do is rename all files ending in .pdf and start with "". Each PDF file has unique names which are not easy to identify but if each one of them should start with this "".

I also noticed that there are several PDF files that start with "_" and others with a word.

I would need to add this prefix only in those that start from a-z or from 0-9.

How could I do this in powerShell, C # or some other method?

Thanks

    
asked by A arancibia 06.10.2017 в 15:14
source

1 answer

1

You could go through the files using

string[] files = Directory.GetFiles(@"c:\carpeta", "*.pdf", SearchOption.AllDirectories);

foreach(string file in files)
{
    string fileNameIn = Path.GetFileNameWithoutExtension(file);

    if(fileNameIn.StartsWith("_"))
        continue;

    string pathIn = Path.GetDirectoryName(file);
    string fileNameOut = string.Format("{0}_{1:yyyyMMdd}.pdf", fileNameIn, DateTime.Now);
    string pathOut = Path.Combine(pathIn, fileNameOut);

    File.Move(file, pathOut);
}

in this case, when you find the pdf files, you rename them by making a move to the same folder, only that the file name is added to the date

This can change it, it's just an example of how to rename and select the files

    
answered by 06.10.2017 / 15:51
source