The files are named as follows: 5000023_abc_2000045.pdf, 5000023_def_2000045.pdf.
All files are in the same directory.
I want to write all these files sorted into an array and then put them to a 3rd party program to merge them. It is about 60000 files.
I tried with getfiles, but that didn't work. sorry, i'm a total beginner.
many thanks in advance
Heres my Code
using System.Diagnostics;
using System.IO;
using bin_vMergePdfNeu.Properties;
namespace System.Configuration
{
class Program
{
static void Main(string[] args)
{
Console.Title = "bin_vMergePDF";
// Ordner aus dem die PDF gezogen werden
string altDirIn = Settings.Default.AlternativeInputDir;
// Ordner in dem die zusammengeführte PDF abgelegt werden soll
string altDirOut = Settings.Default.AlternativeOutputDir;
////////////////////////////////////////////////////////////////////
//var paths = new Collections.Generic.List<string>();
//paths.Add(altDirIn);
//var cmd = String.Join(" ", altDirIn) + " cat output " + altDirOut;
////////////////////////////////////////////////////////////////////
string[] rchg = Directory.GetFiles(altDirIn, "*.pdf".Split('_')[0]);
string cmd = string.Join(" ", rchg) + " cat output " + altDirOut;
Process p = new Process();
p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo.FileName = "pdftk.exe";
p.StartInfo.Arguments = cmd;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
Console.WriteLine(p.StandardError.ReadToEnd());
Console.WriteLine();
Console.ReadKey();
p.WaitForExit();
}
}
}
This would do the job:
That assumes that every file starts with a number and an underscore.
It's also worth noting that, if you want to sort file names the same way File Explorer does, you can use the same Windows API that File Explorer uses. You can create a comparer that incorporates that API:
You can then use that class to sort your file paths:
It's also worth noting that that code, which is based on what you wrote yourself, is probably going to fail if the folder path or any file name has a space in it. You could quote each path to handle that: