Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string PathToClean = @"C:\sdelete -p 1 -s -a " + repositoryUrls[i];


What I have tried:

here repositoryurls[i] is a string array to used sdelete. But i want to delete only files from particular path not a folder. if folder is empty then it should not be deleted.
Posted
Updated 4-Sep-19 9:25am
Comments
phil.o 21-Apr-16 4:05am    
And what is sdelete exactly? Where does this executable come from?
Shrikesh_kale 21-Apr-16 5:12am    
it is Microsoft utility for deleting files from folder or dir
ZurdoDev 21-Apr-16 8:03am    
You should refer to the documentation for sdelete to know what its options are.
snorkie 21-Apr-16 11:03am    
SDELETE doesn't have any flags that will do this. Do you have to use sdelete? A simple recursive function would do this.
Philippe Mori 21-Apr-16 12:30pm    
Don't use external code for that. And by the way, don't assume it is available in C:\. It should not be here anyway.

If you don't have to use SDELETE, try messing with the following program. It deletes empty directories from the base directory. You could use this as a starting block and reverse the logic to delete directories with content. Then if you must use SDELETE, you can call it to delete a single directory when one is found.

Hogan

C#
using System;
using System.IO;

namespace EmptyDirectory
{
    class Program
    {
        static void Main(string[] args)
        {
            RecursiveSearch(@"c:\music");

            Console.WriteLine("Finished.");
            Console.ReadLine();
        }

        private static void RecursiveSearch(string folder)
        {
            foreach (string s in Directory.EnumerateDirectories(folder, "*", SearchOption.AllDirectories))
            {
                if (Directory.GetFiles(s).Length == 0 && Directory.GetDirectories(s, "*", SearchOption.TopDirectoryOnly).Length == 0)
                {
                    Directory.Delete(s);
                    Console.WriteLine("Deleted: " + s);
                }
            }
        }
    }
}
 
Share this answer
 
Comments
Shrikesh_kale 26-Apr-16 8:04am    
hi it does not delete empty folder at base level
I am giving path c:\music and it fails to delete at c:\music\non-empty-folder\non-empty-folder\empty-folder
Shrikesh_kale 26-Apr-16 8:04am    
it works for only one level
snorkie 26-Apr-16 8:25am    
Correct, the code I provided was a starting point to build further on. Was just trying to direct you in the right direction.
C#
using System;
using System.IO;

namespace EmptyDirectory
{
    class Program
    {
        static void Main(string[] args)
        {
            RecursiveSearch(@"c:\music");

            Console.WriteLine("Finished.");
            Console.ReadLine();
        }

        private static void RecursiveSearch(string folder)
        {
            foreach (string s in Directory.EnumerateDirectories(folder, "*", SearchOption.AllDirectories).reverse())
            {
                if (Directory.GetFiles(s).Length == 0 && Directory.GetDirectories(s, "*", SearchOption.TopDirectoryOnly).Length == 0)
                {
                    Directory.Delete(s);
                    Console.WriteLine("Deleted: " + s);
                }
            }
        }
    }
}
 
Share this answer
 
v2
Comments
Dave Kreskowiak 4-Sep-19 20:55pm    
Copying and pasting someones answer as your own? It's really obvious what you did.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900