How to delete all files and folders in a directory?
System.IO.DirectoryInfo di = new DirectoryInfo("YourPath");
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
If your directory may have many files, EnumerateFiles()
is more efficient than GetFiles()
, because when you use EnumerateFiles()
you can start enumerating it before the whole collection is returned, as opposed to GetFiles()
where you need to load the entire collection in memory before begin to enumerate it. See this quote here:
Therefore, when you are working with many files and directories, EnumerateFiles() can be more efficient.
The same applies to EnumerateDirectories()
and GetDirectories()
. So the code would be:
foreach (FileInfo file in di.EnumerateFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.EnumerateDirectories())
{
dir.Delete(true);
}
For the purpose of this question, there is really no reason to use GetFiles()
and GetDirectories()
.
Delete all files and folders in a directory
del *.*
instead of del *.db
. That will remove everything.
How to delete files/subfolders in a specific directory at the command prompt in Windows
You can use this shell script to clean up the folder and files within C:\Temp
source:
del /q "C:\Temp\*"
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat
How to delete all files and folders in a folder by cmd call
No, I don't know one.
If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:
del /q destination\*
for /d %x in (destination\*) do @rd /s /q "%x"
This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).
Note that within a batch file you need to double the %
within the for
loop:
del /q destination\*
for /d %%x in (destination\*) do @rd /s /q "%%x"
Remove only files and sub folders from a directory through command prompt
This is the command which has to be used.
CD "C:/Test" && FOR /D %i IN ("*") DO RD /S /Q "%i" && DEL /Q *.*
How to delete all files and folders in one folder on Android
Simplest way would be to use FileUtils.deleteDirectory from the Apache Commons IO library.
File dir = new File("root path");
FileUtils.deleteDirectory(dir);
Bear in mind this will also delete the containing directory.
Add this line in gradle file to have Apache
compile 'org.apache.commons:commons-io:1.3.2'
How to delete all files/folders but keep the root folder in C#
Change the code a little bit.
Add a new argument root, and pass it as false on the recursive calls.
static void Main(string[] args)
{
CleanupFiles(xxx, xxx, true);
}
void CleanupFiles(String sPath, int iDayDelAge, bool root)
{
if (iDayDelAge != 0) // enabled?
{
// Check for aged files to remove
foreach (String file in Directory.GetFiles(sPath))
{
FileInfo fi = new FileInfo(file);
if (fi.LastWriteTime < DateTime.Now.AddDays(iDayDelAge * -1)) // overdue?
{
fi.Delete();
}
}
// Recursively search next subfolder if available
foreach (String subfolder in Directory.GetDirectories(sPath))
{
CleanupFiles(subfolder, iDayDelAge, false);
}
// Remove empty folder
if (Directory.GetFiles(sPath).Length == 0 && Directory.GetDirectories(sPath).Length == 0 && !root)
{
Directory.Delete(sPath);
}
}
}
How to make a batch file delete all files and subfolders in a specific directory?
The standard FOR
command only iterates files. So you need a mechanism to list file and folders at the same time. You also need a mechanism to determine if the object iterated is a file or folder so that you use the correct command to either delete the file or remove the directory.
@echo off
CD /D "C:\root folder"
for /F "delims=" %%G in ('dir /b') do (
REM check if it is a directory or file
IF EXIST "%%G\" (
rmdir "%%G" /s /q
) else (
del "%%G" /q
)
)
How to delete all files from a specific folder?
string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
foreach (string filePath in filePaths)
File.Delete(filePath);
Or in a single line:
Array.ForEach(Directory.GetFiles(@"c:\MyDir\"), File.Delete);
Related Topics
Get Connection String from App.Config
How to Change Network Settings (Ip Address, Dns, Wins, Host Name) with Code in C#
When to Use Ref and When It Is Not Necessary in C#
C# - Multiple Generic Types in One List
Compiler Ambiguous Invocation Error - Anonymous Method and Method Group with Func<> or Action
How to Change Datatype of a Datacolumn in a Datatable
Code for Decoding/Encoding a Modified Base64 Url (In ASP.NET Framework)
Error: "Cannot Modify the Return Value" C#
C# Reflection: How to Get Class Reference from String
How to Delete a Line from a Text File in C#
Entering Keys Manually with Entity Framework
Why Does My .Net 4 Application Know .Net 4 Is Not Installed