Delete files from multiple subfolders in ASP.NET

Hi Experts,

Is there any way in ASP.NET, (vb please), that one can issue a command to delete a given filename from a given path but to also delete it from any subfolders that may exist?  i.e. the file for deletion can exist in multiple folders and I want to delete any instance that may or may not occur in each subfolder?

Help appreciated as always,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Om PrakashCommented:
Please check the example code:

this deletes all files from folder & sub folder but can be modified to search & delete specific file from all location.
jammy-d0dgerAuthor Commented:
thanks for the reply but not quite what I'm looking for.  I'm aware that I could write a bit of code to loop through 'known' subfolders and look for a file, but using this snippet, I would need to know all the names of the subfolders first.  Ideally what I'd like to do is delete a certain file from 'any' subfolder that 'might' exist, i.e. like a wildcard, so that I don't need to know the subfolder list in advance, (or care for that matter!).
For instance, if I were in DOS, I would say
"del *.jpg /S"
and this would delete all jpg files from the current folder and all subfolders.
Om PrakashCommented:
In this case you can run dos command from vb code
'This code will create "abcd" directory in current Path.
'You cannot debug normally: to work this need the dos
'window to have focus
'If you want to see dos window, comment the
'last commands that close that window
option Explicit

private Sub Command1_Click()

Dim strCommand as string
Dim retPointer as Long

'if winNt
retPointer = Shell("cmd.exe", vbMaximizedFocus)

'if win98/95/me, the following:
'retPointer = Shell("command.exe", vbMaximizedFocus)
'launch a command:
'activate the dos window
AppActivate retPointer
'put the command in string variable -start with a space!-
strCommand = " mkdir abcde"
'write it to the dos window:
SendKeys strCommand, true
'you've just typed something. Need to press enter?
SendKeys "{ENTER}", true

'close dos windows:
SendKeys "Exit", true
SendKeys "{ENTER}", true
End Sub

Open in new window

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

jammy-d0dgerAuthor Commented:
I'm probably being a bit paranoid but 'actually' dropping to DOS on a web app, feels a bit... uncontrolled.  I know it's my own fault for mentioning it but I just thought it showed what I was trying to achieve, I wasn't expecting you to suggest I actually run it as a command.  It's very interesting that it can be done, but I'd feel worried about this fire and forget approach for deleting files en-masse.
I think I have found the .NET function I need, DirectoryInfo.GetFileSystemInfos Method (String)
Using this I will be able to build a list of subfolders on the fly, then traverse them all and search for the filename string required and then issue the delete command.  Doing it this way, I will feel more in control of what's going on, so to speak.
Your idea will work, but here is a little more efficient approach because it doesn't use the heavier FileSystemInfo objects (I haven't tested it but I think it will work):

Private Sub DeleteFile(directoryPath As String, fileName As String, searchSubdirectories As Boolean)
	Dim filePath As String = Path.Combine(directoryPath, fileName)

	If File.Exists(filePath) Then
	End If

	If searchSubdirectories Then
		Dim dir As New DirectoryInfo(directoryPath)

		For Each subdirectory As DirectoryInfo In dir.GetDirectories()
			DeleteFile(subdirectory.FullName, fileName, searchSubdirectories)
	End If
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jammy-d0dgerAuthor Commented:
that 'looks' like an excellent chunk of code there rdogmartin!  I have to leave my desk for a bit right now but will try this approach later and report back.  thanks for help so far guys.
jammy-d0dgerAuthor Commented:
Thanks for your help on this question.  The code snippet provided by rdogmartin worked like a charm, perfectly achieving what I'd set out to do. So I've awarded the lion's share to rdog but felt that I should give 50 points to om_prakash_p for the interesting link re: running DOS commands.

Thanks again experts.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.