There are many .bmp files present in my C:\TEMP directory.
I am using following code to delete all .bmp file in my C:\TEMP directory but somehow its not working as I expect. Can anyone help me in this?
string[] filePaths = Directory.GetFiles(#"c:\TEMP\");
foreach (string filePath in filePaths)
{
if (filePath.Contains(".bmp"))
File.Delete(filePath);
}
I have already checked that .bmp file and the directory has no read only attribute
For starters, GetFiles has an overload which takes a search pattern http://msdn.microsoft.com/en-us/library/wz42302f.aspx so you can do:
Directory.GetFiles(#"C:\TEMP\", "*.bmp");
Edit: For the case of deleting all .bmp files in TEMP:
string[] filePaths = Directory.GetFiles(#"c:\TEMP\", "*.bmp");
foreach (string filePath in filePaths)
{
File.Delete(filePath);
}
This deletes all .bmp files in the folder but does not access subfolders.
Should also use .EndsWith instead of .Contains
You Could write below code in fast way:
string[] t = Directory.GetFiles(Environment.CurrentDirectory, "*.pdf");
Array.ForEach(t, File.Delete);
or For Text Files:
string[] t = Directory.GetFiles(Environment.CurrentDirectory, "*.txt");
Array.ForEach(t, File.Delete);
So, You could write code for all extension and all directories.
Related
How can I iterate and print all of the file paths of all .pdf files in a specific directory when using Directory.EnumerateFiles?
The following code only returns the path of the first .pdf file in the specified directory.
IEnumerable<string> files = Directory.EnumerateFiles(#"C:\MyFolder", "*.pdf*", SearchOption.AllDirectories);
foreach (string file in files) {
Console.WriteLine("File Path:{0}", file);
}
// Returns:
// C:\MyFolder\firstPdfFile.pdf
Again, what I want is to be able to print all of the paths of all pdfs files in a specified directory. What am I missing?
EDIT: I'm not getting any errors, but the only thing I see in the console is the path of the first pdf found in the specified directory and I was expecting to see the path of all pdf files in that directory.
haven't used function you written, but suggest you to try out below
string[] filePaths = Directory.GetFiles(#"c:\MyFolder\", "*.pdf",
SearchOption.AllDirectories);
// returns:
// "c:\MyFolder\fist.pdf"
// "c:\MyFolder\subdirectory\sss.pdf"
I'm looking for a way to avoid System.IO.PathTooLongException. I read that I could put \\?\ in front of a long path.
The problem is that I have FileInfo objects and I ask me how to add the \\?\ path prefix to my FileInfo paths without writting useless code.
Note that I have folders with several thousand of files to copy and I don't want that my application takes a lot of useless time during his process.
Thanks.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
FileInfo[] files = dir.GetFiles(Constants.ALL_FILES, SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
file.CopyTo(destAbsolutePath);
}
I'm experiencing a strange issue here and not quite sure what I'm missing. I ran the code below and it did create a .zip file and I watched the size increase from 0KB to 8,992KB. However, as I open the .zip file, I don't see any file. And if I try to "Extract All..." from explorer, it shows "Windows can not complete the extraction" because the .zip file "is invalid". Any idea what I did wrong?
if (File.Exists(ZipName))
File.Delete(ZipName);
using (ZipArchive archive = ZipFile.Open(ZipName, ZipArchiveMode.Create))
{
foreach (string sFileName in FileNames)
{
archive.CreateEntryFromFile(sFileName,sFileName,CompressionLevel.Optimal);
}
}
You need to strip the drive letter from the entryName, which is the 3rd parameter to CreateEntryFromFile().
So instead of
archive.CreateEntryFromFile(sFileName, sFileName, CompressionLevel.Optimal);
Use
archive.CreateEntryFromFile(sFileName, sFileName.Substring(3), CompressionLevel.Optimal);
I ran into the same problem, and I discovered a solution. It turns out that I was feeding the method a full path to the file for both filename arguments, like so:
// This gets a list of files with their full paths
var fileList = Directory.GetFiles(_outputDir, "*.txt");
foreach (var file in fileList)
{
archive.CreateEntryFromFile(file, file, CompressionLevel.Optimal);
}
That gave me the same problem you had -- an empty zip file, which was invalid. (By the say, my file paths weren't that long -- they were C:\Results\[some_guid].txt.)
The solution was to get the files as FileInfo objects, then for the first argument use the full path and for the second argument just use the file name, like so:
// This gets a set of FileInfo objects, with a FullName property that is
// the full path to the file, and a Name property that is just the file name.
var directoryInfo = new DirectoryInfo(_outputDir);
var fileList = directoryInfo.GetFiles("*.txt");
foreach (var file in fileList)
{
archive.CreateEntryFromFile(file.FullName, file.Name, CompressionLevel.Optimal);
}
This ended up working perfectly. If I opened the zip file, the files were there. If I extracted it, the extraction worked and the files seemed to be valid.
I am new to C# and I am trying to build a console application to copy a specific file type .e.g *.txt that is nested within subfolders to be copied to another directory.
The directory looks something like this
C:\V1.1\Folder_*\Folder\Folder\Folder\Filetype.txt
* meaning today's date
How would I get a list of files matching that pattern?
Try something like this.
var path = String.Format(#"C:\V1.1\Folder_{0}\Folder\Folder\Folder",
DateTime.ToString("dd-MM-yyyy"))
DirectoryInfo di = new DirectoryInfo(path);
var files=di.GetFiles("*.txt", SearchOption.AllDirectories)
You can check this site out for more information.
Thanks for all the help. I ended up doing this:
foreach (var file in Directory.GetFiles(sourceDir, "*.txt", SearchOption.AllDirectories))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);
This is a simple question and I am sure you C# Pros know it.
If you want to grab the contents of a directory on a hard drive (local or otherwise) in a C Sharp program, how do you go about it?
Call Directory.GetFiles.
If you mean get a directory then it is very simple. The following code will give you a list of all the directories in a given directory and then list all the files in the directory:
string dir = #"C:\Users\Joe\Music";
DirectoryInfo dirInfo = new DirectoryInfo(dir);
FileInfo[] files = dirInfo.GetFiles();
DirectoryInfo[] directories = dirInfo.GetDirectories();
foreach (DirectoryInfo directory in directories)
Console.WriteLine(directory.Name);
foreach (FileInfo file in files)
Console.WriteLine (file.Name);