C# Create Directory and move file to it. RRS feed

  • Question

  • 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(Path.Combine(DestinationFolder, @"PDF"));
            string PDFFolder = DestinationFolder + "\\" +  @"PDF";
            int Index = 0;
            foreach (FileInfo f in infos)
                File.Move(f.FullName, DestinationFolder + Number2String(Index, true) + ".pdf");//Attachment Folder
        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.

    Monday, October 14, 2019 11:15 AM

All replies

  • Look at your File.Move:

    File.Move(f.FullName, DestinationFolder + Number2String(Index, true) + ".pdf");

    If, for instance, Destination folder is "c:\Something" and number2string returns "1234" then the result of this is going to be "c:\Something1234.pdf".

    Do you see the problem already? You are missing a "\" in between the folder and the filename.

    To avoid this kind of problems in the future, I suggest that you avoid concatenating files and folders. Instead, you can use System.IO.Path.Combine, which has the "intelligence" to add the backslash if it is missing, and not add it if it's already there.

    File.Move(f.FullName, Path.Combine(DestinationFolder,  Number2String(Index, true) + ".pdf"));

    Edit: By the way, you probably meant to use "PDFFolder" instead of "DestinationFolder", otherwise it doesn't make much sense to prepare PDFFolder.

    Monday, October 14, 2019 1:59 PM
  • Hi Malortje,

    Thank you for posting here.

    According to your description, you want to move a file from a folder to another folder.

    In your code, you use:

    DestinationFolder + Number2String(Index, true) + ".pdf";

    to create a path.

    It’s the path:

    But in fact, the following path is the right one.

    There are two ways to get this path.


      String destFileName = DestinationFolder + "\\"+ Number2String(Index, true) + ".pdf";
     String destFileName= Path.Combine(DestinationFolder, Number2String(Index, true) + ".pdf");
    Hope my solution could be helpful.

    Best regards,

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 15, 2019 6:48 AM