Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting multiple files with wildcard

Tags:

c#

windows

You know that in linux it's easy but I can't just understand how to do it in C# on Windows. I want to delete all files matching the wildcard f*.txt. How do I go about going that?

like image 423
prongs Avatar asked Jan 10 '12 16:01

prongs


People also ask

How do I select multiple files to delete at once?

Click the first file or folder, and then press and hold the Ctrl key. While holding Ctrl , click each of the other files or folders you want to select.

How do you delete a file with a wildcard in Python?

Python Delete Files Wildcard To remove files by matching a wildcard pattern such as '*. dat' , first obtain a list of all file paths that match it using glob. glob(pattern) . Then iterate over each of the filenames in the list and remove the file individually using os.

How do I delete multiple files in a folder?

You can delete multiple files or folders by holding down the Ctrl key and clicking each file or folder before pressing Delete . You can hold down the Shift key while pressing the Delete key to prevent files from going to the Recycle Bin when deleted.


1 Answers

You can use the DirectoryInfo.EnumerateFiles function:

var dir = new DirectoryInfo(directoryPath);  foreach (var file in dir.EnumerateFiles("f*.txt")) {     file.Delete(); } 

(Of course, you'll probably want to add error handling.)

like image 190
Ry- Avatar answered Oct 25 '22 19:10

Ry-