Thread: [C#] Getting the full path of a file, when you only have a part of the f-name.

Results 1 to 4 of 4
  1. #1 [C#] Getting the full path of a file, when you only have a part of the f-name. 
    Registered Member

    Join Date
    Nov 2013
    Posts
    746
    Thanks given
    187
    Thanks received
    459
    Rep Power
    5000
    F-name = filename

    Hello guys,

    I know that most of you are familiar with Java, however C# look like it.
    I've stored in a "String []", the ID of the files that I need to move to another folder. All Files has different Prefix, and there are hundreds of files.

    For exemple :

    Attached image

    What I want to do is get the Full Path of the files containing the "ID" in order to be able to move it in another folder.


    What's the problem ?


    I'm not able to get the Full Path of the file with only a piece of the name files. Any kind of help, would be much appreciated.

    Code :

    Code:
    
    
                string ID = File.ReadAllText(@"C:\Users\Desktop\Script\zig.txt");
                Console.WriteLine(ID);
                Console.ReadLine();
               
    
    
                String root = @"C:\Users\Desktop\Script\tiptop\";
                String[] lines = System.IO.File.ReadAllLines(@"C:\Users\Desktop\Script\zig.txt");
    
    
                for (int i = 0; i <= lines.Length; i++)
                {
                    string fullPath = System.IO.Path.Combine(root,(lines[i-1]));
    
    
                    if (System.IO.File.Exists(fullPath))
                    {
                        Console.WriteLine("" + fullPath);
                    }
    
    
                }
    
    
                for (int i = 0; i <= lines.Length;i++)
                {
                    //Console.WriteLine("" + lines[i]);
                    string fullPath = Path.GetFullPath(@"C:\Users\Desktop\Script\tiptop\");
               
                    Console.WriteLine(System.IO.Directory.EnumerateFiles(@"C:\Users\Desktop\Script\tiptop\", lines[i]));
    
    
    Finally, I was able to fix it.
    The code isn't clear at all, but it works.

    That's how I've done :

    Code:
    /**
     * @author Naïm Gallouj
     * 2/04/2019
     * */
    using System;
    using System.IO;
    using System.Text;
    using System.Resources;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    
    namespace Netty
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Utilisation d'un autre programme pour savoir si l'ID apparait 4 fois
                // Stockage des ID qui aparaissent 4 fois dans un Tableau de chaîne de caractère.
                string ID = File.ReadAllText(@"C:\Users\ngallouj\Desktop\Script\zig.txt");
                
                //Console.WriteLine(ID);
                //Console.ReadLine();
    
    
                //Declaration de toutes les Préfixes de BEXT
                string prefix1 = "exp_vente_ent_";
                string prefix2 = "ctl_";
                string prefix3 = "exp_vente_lig_";
                string prefix4 = "mvt_";
                string prefix5 = "rcp_achat_ent_";
                string prefix6 = "rcp_achat_lig_";
                string prefix7 = "rec_vente_lig_";
                string prefix8 = "rec_vente_ent_";
    
    
                //Declaration extenstion fichiers possible
                string a = ".txt";
                string b = ".top";
    
    
                String root = @"C:\Users\ngallouj\Desktop\Script\tiptop\";
                String[] lines = System.IO.File.ReadAllLines(@"C:\Users\ngallouj\Desktop\Script\zig.txt");
    
    
                for (int i = 0; i <= lines.Length; i++)
                {
                    //string fullPath = System.IO.Path.Combine(root,prefix1,lines[i-1]);
                    //=========================================
                    //DECLARATION DES CHEMINS D'ACCEES PÖSSIBLE
                    //=========================================
                    //concaténation et lecture dans le tableau des ID pour obtenir des chemins d'accées Plausible.
    
    
                    string fullPath0 = System.IO.Path.Combine(root, prefix1 + lines[i] + a);
                    string fullPath0a = System.IO.Path.Combine(root, prefix1 + lines[i] + b);
    
    
                    string fullPath1 = System.IO.Path.Combine(root, prefix2 + lines[i]) + a;
                    string fullPath1a = System.IO.Path.Combine(root, prefix2 + lines[i] + b);
    
    
                    string fullPath2 = System.IO.Path.Combine(root, prefix3 + lines[i] + a);
                    string fullPath2a = System.IO.Path.Combine(root, prefix3 + lines[i] + b);
    
    
                    string fullPath3 = System.IO.Path.Combine(root, prefix4 + lines[i] + a);
                    string fullPath3a = System.IO.Path.Combine(root, prefix4 + lines[i] + b);
    
    
                    string fullPath4 = System.IO.Path.Combine(root, prefix5 + lines[i] + a);
                    string fullPath4a = System.IO.Path.Combine(root, prefix5 + lines[i] + b);
    
    
                    string fullPath5 = System.IO.Path.Combine(root, prefix6 + lines[i] + a);
                    string fullPath5a = System.IO.Path.Combine(root, prefix6 + lines[i] + b);
    
    
                    string fullPath6 = System.IO.Path.Combine(root, prefix7 + lines[i] + a);
                    string fullPath6a = System.IO.Path.Combine(root, prefix7 + lines[i] + b);
    
    
                    string fullPath7 = System.IO.Path.Combine(root, prefix8 + lines[i] + a);
                    string fullPath7a = System.IO.Path.Combine(root, prefix8 + lines[i] + b);
    
    
                    string name0 = System.IO.Path.Combine(root, prefix1 + lines[i] + a);
                    string name0a = System.IO.Path.Combine(root, prefix1 + lines[i] + b);
    
    
                    string name1 = System.IO.Path.Combine(root, prefix2 + lines[i]) + a;
                    string name1a = System.IO.Path.Combine(root, prefix2 + lines[i] + b);
    
    
                    string name2 = System.IO.Path.Combine(root, prefix3 + lines[i] + a);
                    string name2a = System.IO.Path.Combine(root, prefix3 + lines[i] + b);
    
    
                    string name3 = System.IO.Path.Combine(root, prefix4 + lines[i] + a);
                    string name3a = System.IO.Path.Combine(root, prefix4 + lines[i] + b);
    
    
                    string name4 = System.IO.Path.Combine(root, prefix5 + lines[i] + a);
                    string name4a = System.IO.Path.Combine(prefix5 + lines[i] + b);
    
    
                    string name5 = System.IO.Path.Combine(prefix6 + lines[i] + a);
                    string name5a = System.IO.Path.Combine(prefix6 + lines[i] + b);
    
    
                    string name6 = System.IO.Path.Combine(prefix7 + lines[i] + a);
                    string name6a = System.IO.Path.Combine(prefix7 + lines[i] + b);
    
    
                    string name7 = System.IO.Path.Combine(prefix8 + lines[i] + a);
                    string name7a = System.IO.Path.Combine(prefix8 + lines[i] + b);
    
    
    
    
                    //Cette partie sert à vérifier si le chemin d'accées existe pour ensuite bouger tout les fichiers sur un serveur tierce.
    
    
                    //Console.WriteLine(fullPath1);
                    //Console.WriteLine(fullPath1a);
                    //Console.WriteLine(fullPath2);
                    //Console.WriteLine(fullPath2);
                    //File.Move(fullPath2, @"C:\Users\ngallouj\Desktop\Script\ark");
    
    
    
    
                    //File.Move(fullPath1, @"C:\Users\ngallouj\Desktop\Script\ark\");
                    //File.Move(fullPath1a, @"C:\Users\ngallouj\Desktop\Script\ark\");
                    if (File.Exists(fullPath0)) {
                        
                        File.Copy(fullPath0, @"C:\Users\ngallouj\Desktop\Script\ark\"+ prefix1 + lines[i] + a);
    
    
                    }
    
    
                        if (File.Exists(fullPath0a))
    
    
                    {
                       
    
    
                        File.Copy(fullPath0a, @"C:\Users\ngallouj\Desktop\Script\ark\"+ prefix2 + lines[i] + a);
    
    
                        }
    
    
                    if (File.Exists(fullPath1))
                        {
    
    
    
    
                            File.Copy(fullPath1, @"C:\Users\ngallouj\Desktop\Script\ark\"+ prefix3 + lines[i] + a);
    
    
                        }
    
    
                        if (File.Exists(fullPath1a))
                       {
    
    
    
    
                        File.Copy(fullPath1a, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix4 + lines[i] + a);
    
    
    
    
                         }
                        if (File.Exists(fullPath2))
                    {
                        File.Copy(fullPath2, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix5 + lines[i] + a);
                    }
                        if (File.Exists(fullPath2a))
                    {
                        File.Copy(fullPath2a, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix6 + lines[i] + a);
                    }
                        if (File.Exists(fullPath3))
                    {
                        File.Copy(fullPath3, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix7 + lines[i] + a);
                    }
                        if (File.Exists(fullPath3a))
                    {
                        File.Copy(fullPath3a, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix8 + lines[i] + a);
                    }
                        if (File.Exists(fullPath4))
                    {
                        File.Copy(fullPath4a, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix1 + lines[i] + a);
                    }
                        if (File.Exists(fullPath4a))
                    {
                        File.Copy(fullPath4a, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix2 + lines[i] + a);
                    }
                        if (File.Exists(fullPath5))
                    {
                        File.Copy(fullPath5, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix3 + lines[i] + a);
                    }
                        if (File.Exists(fullPath5a))
                    {
                        File.Copy(fullPath5a, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix4 + lines[i] + a);
                    }
                        if (File.Exists(fullPath6))
                    {
                        File.Copy(fullPath6, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix5 + lines[i] + a);
                    }
                        if (File.Exists(fullPath6a))
                    {
                        File.Copy(fullPath6a, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix6 + lines[i] + a);
                    }
                        if (File.Exists(fullPath7))
                    {
                        File.Copy(fullPath7, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix7 + lines[i] + a);
                    }
                        if (File.Exists(fullPath7a))
                    {
                        File.Copy(fullPath7a, @"C:\Users\ngallouj\Desktop\Script\ark\" + prefix8 + lines[i] + a);
                    }
    
    
    
    
                    }
                    
                    
                    //File.Move(fullPath2, @"C:\Users\ngallouj\Desktop\Script\ark");
    
    
    
    
                }
     
                     
              
    
    
                    //for (int i = 0; i <= lines.Length;i++)
                    //{
                    //Console.WriteLine("" + lines[i]);
                    //  string fullPath = Path.GetFullPath(@"C:\Users\Desktop\Script\tiptop\");
    
    
                    //Console.WriteLine(System.IO.Directory.EnumerateFiles(@"C:\Users\Desktop\Script\tiptop\", lines[i]));
    
    
    
    
                    //}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
                }
            }

    Attached image


    Attached image

    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Oct 2014
    Posts
    192
    Thanks given
    31
    Thanks received
    15
    Rep Power
    81
    Reply With Quote  
     

  3. #3  
    Registered Member

    Join Date
    Nov 2013
    Posts
    746
    Thanks given
    187
    Thanks received
    459
    Rep Power
    5000
    Plan's was just to make my class working and it's. The code isn't optimized yet xx.

    Attached image


    Attached image

    Reply With Quote  
     

  4. #4  
    Registered Member
    Project's Avatar
    Join Date
    Dec 2010
    Posts
    2,669
    Thanks given
    1,043
    Thanks received
    820
    Rep Power
    1101
    Here's some logic for you to use. You could grab the list of files in the directory, flag all of the files that contain your prefix in each string, and then move those that are flagged.

    I.E

    get files example:

    Code:
    DirectoryInfo d = new DirectoryInfo(@"D:\Test");//Assuming Test is your Folder
    FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files or whatever your file extension is
    string str = "";
    foreach(FileInfo file in Files )
    {
      str = str + ", " + file.Name;
    }
    Use the function String.Contains() for the substring, then add to its own array

    Use new array of successful String.Contains() to move into a new folder

    edit: misunderstood your question. oops.
    This may help you: https://stackoverflow.com/questions/...subdirectories
    Reply With Quote  
     

  5. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 0
    Last Post: 04-23-2015, 10:00 PM
  2. Replies: 5
    Last Post: 10-02-2011, 12:29 AM
  3. Replies: 4
    Last Post: 07-08-2011, 12:08 AM
  4. Replies: 0
    Last Post: 06-06-2010, 03:24 PM
  5. Replies: 2
    Last Post: 03-21-2010, 10:14 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •