C# Create Directory and move file to it.

  • Thread starter Thread starter Malortje
  • Start date Start date
M

Malortje

Guest
Hi All,

I'm using email parser to save email attachments (PDF and XML). In Email parser we can use c# code to rename and move the files to a destination.

For every email that I receive with a PDF file he needs to create a Folder with the Subject_message name that is in the code.

So for example he needs to make :

-> Folder1 (Subject message) name

-> Subfolder (PDF)

-> .pdf file

the code below does create the PDF folder but the File.move doens't move the .pdf file under the sub folder.
Could you guys please assist ?

using System;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Collections.Specialized;
using EmailParserBackend.ScriptingInterface;

public class MyAction : ScriptBasedAction
{
public override bool Run(MailMessage email, NameValueCollection fields)
{
String message_subject = fields["Subject"];
message_subject = Between(message_subject, "(", ")").Trim();

DirectoryInfo d = new DirectoryInfo(@"D:\HARDDISK\INTFILES\AEB2TRANSLIMA\Temp Files\PDF\Temp");//PDF Temp folder

FileInfo[] infos = d.GetFiles();

string CurrentFolder = @"D:\HARDDISK\INTFILES\AEB2TRANSLIMA\Attachments\";
string DestinationFolder = @"D:\HARDDISK\INTFILES\AEB2TRANSLIMA\Attachments\" + message_subject;
System.IO.Directory.CreateDirectory(DestinationFolder);
System.IO.Directory.CreateDirectory(Path.Combine(DestinationFolder, @"PDF"));
string PDFFolder = DestinationFolder + "\\" + @"PDF";

int Index = 0;

foreach (FileInfo f in infos)
{
Index++;
File.Move(f.FullName, DestinationFolder + Number2String(Index, true) + ".pdf");//Attachment Folder
}

return(true);
}

public string Between(string value, string a, string b)
{
int posA = value.IndexOf(a);
int posB = value.LastIndexOf(b);

if (posA == -1)
{
return "";
}

if (posB == -1)
{
return "";
}

int adjustedPosA = posA + a.Length;

if (adjustedPosA >= posB)
{
return "";
}

return value.Substring(adjustedPosA, posB - adjustedPosA);
}

public string Number2String(int number, bool isCaps)
{
Char c = (Char)((isCaps ? 65 : 97) + (number - 1));

return c.ToString();
}
}



Thanks in advance.

Continue reading...
 
Back
Top