Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Simple File Sorter

[Image: Gmqtwwg.png]


So I was thinking the other day.. I want to code something but I dont know what..
Then I got bored and started sorting files on my desktop and I realized I had ALOT of files and I didnt feel like sorting them all because it would be a boring process of creating folders and adding them all to each and every one.
Then it hit me.. I can create a simple file sorter and share it with other people who might just suffer from the same issue (Cludder Desk)

So here it is.. You simply drag and drop all the files in your desktop and it sorts them into folders i.e .png & .jpg go to Images, .exe files to into Executables etc.

I also commented out the code if anyone wants to use this as a learning project.
Happy coding!
Thanks to Malum & F0RC3FI3LD For helping me understand how to make the code look better!



I looked at him coding this:)
Its a really nice program to learn about files and also just for general use!
Good job:)
(Dec 25, 2016 03:19 PM)ForceJew Wrote:  I looked at him coding this:)
Its a really nice program to learn about files and also just for general use!
Good job:)

Thank you! Had a blast doing this :D
things can be done much simple >> shorter << extention
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
My $0.02:

- The solution name is IMBA 👍
- Why this?
Code
if (fileExtension == ".txt")
{
    do something here
    if  (fileExtension == ".txt")
    {
        Why again the SAME If sentence 'nested' in the SAME condition? do.Ob
    }
}

- You should use a reusable 'void', class, helper or other some shit for your folder stuff: The code is a C&P and only change the file extension, so you basically have 20 lines for txt files, the 'same' 20 lines for exe files, other 'same' 20 lines for jpg, same for rars,...
Why isn't anyone using a Dictionary here? <File Extension, Filepath>. This is the most concrete way of doing this IMO. Keys are already unique.


Code
var filemap = Directory.GetFiles(@"...").GroupBy(Path.GetExtension).ToDictionary(x => x.Key, x => x.ToList());

^ Dictionary< string, List<string> >

Code
foreach (var kv in filemap)
{
    var ext = kv.Key;
    /* create folder here (check existence in case?) */
    Console.WriteLine(ext);
    foreach (var fp in kv.Value)
    {
        /* move file to designated folder */
        Console.WriteLine($"\t- {fp}");
    }
}
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Dec 25, 2016 07:35 PM)0xDEAD10CC Wrote:  Why isn't anyone using a Dictionary here? <File Extension, Filepath>. This is the most concrete way of doing this IMO. Keys are already unique.


Code
var filemap = Directory.GetFiles(@"...").GroupBy(Path.GetExtension).ToDictionary(x => x.Key, x => x.ToList());

^ Dictionary< string, List<string> >

Code
foreach (var kv in filemap)
{
    var ext = kv.Key;
    /* create folder here (check existence in case?) */
    Console.WriteLine(ext);
    foreach (var fp in kv.Value)
    {
        /* move file to designated folder */
        Console.WriteLine($"\t- {fp}");
    }
}

+1


Duplicated code is a code smell. You need to trigger yourself when you write duplicated code and think of a better solution.

[Image: D12KBT6.png]
(Dec 25, 2016 07:35 PM)0xDEAD10CC Wrote:  
Code
foreach (var kv in filemap)
        Console.WriteLine($"\t- {fp}");

Didn't know about $ until now. That's extremely useful! Thanks for the info.
(Dec 26, 2016 07:59 AM)Malum™ Wrote:  Didn't know about $ until now. That's extremely useful! Thanks for the info.

It's called String Interpolation and came in C# 6.0 if I am not mistaken.
(Dec 26, 2016 09:33 AM)Ninja Wrote:  It's called String Interpolation and came in C# 6.0 if I am not mistaken.

Yea, I had to Google it. Got me going on all the new features and a couple ones I've been missing out on.
Code updated.
Hope you all enjoy!
My 2nd $0.02:

- lBox ListBox is useless. You could remove it from your frmMain Form.
- Why you call a class/function 'Sort' if this class/function do not perform any kind of sorting? 😧
- 2nd 'Sort' function in FileSorter class should be private, not public.
- if you throw an exception you should propagate it, if don't do this way your try is useless:
Code
In frmMain:
try
    Foo();
catch ex
    //ex handling

In Helper:
int Foo()
try
    Foo2();
catch ex
    throw new exception(ex.message);

...
int Foo2()
try
    //stuff here
catch ex
    throw ex


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Release] by SIM-5525056Jul 10, 2024 03:13 PM 6 712 Jul 10, 2024 03:13 PM
Last Post: SIM-5525056
[Python] by zelusAug 20, 2019 11:09 PM 5 388 Aug 20, 2019 11:09 PM
Last Post: whitekremlin
[Python] by Baba YagaDec 5, 2015 07:21 PM 3 250 Dec 5, 2015 07:21 PM
Last Post: tulamba