• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 479
  • Last Modified:

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,

  • 4
  • 2
2 Solutions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now