Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rescursive Delete in VB?

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
0
0
US
i need to perform these following task, the first one i pretty much got<br>
1)Specify Path<br>
2)Remove all folders that begin with _<br>
3) move through all subfolders removing _<br>
4) Remove all files that begin with d_<br>
5) Reset all File attributes to Normal(no Read Only, sys,etc)<br>
<br>
I could do this easily if i was in VC++, but i was wondering<br>
how would you go about this recursivly in VB6<br>
<br>
kb244 <p>Karl<br><a href=mailto:kb244@bellsouth.net>kb244@bellsouth.net</a><br><a href= </a><br>
 
Whats your definition of easy?<br>
In VB use the &quot;Dir&quot; command<br>
It has different parameters<br>
You will probably need some sort of loop<br>
there is an example in help <br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
i'm using FileSystemObject right now to figure it out, and a loop wouldnt be it, cuz then you'd never know how many times, and so on, a rescursion is a function that calls itself over and over again, and backs out one when it hits an endpoint, then continues to move along.<br>
the DeleteFolder under FileSystemObject will delete any folder matching the criteria whether or not it has contents in it, so this might be usefull to me already figured out how to find a file within a given path.<br>
<br>
Private Function FindFile(ByVal sFol As String, sFile As String, nDirs As Integer, nFiles As Integer) As Long<br>
Dim tFld As Folder, tFil As File, FileName As String<br>
Set fld = fso.GetFolder(sFol)<br>
FileName = Dir(fso.BuildPath(fld.path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)<br>
While Len(FileName) &lt;&gt; 0<br>
FindFile = FindFile + FileLen(fso.BuildPath(fld.path, FileName))<br>
nFiles = nFiles + 1<br>
List1.AddItem fso.BuildPath(fld.path, FileName) ' Load ListBox<br>
FileName = Dir() ' Get next file<br>
DoEvents<br>
Wend<br>
Label1 = &quot;Searching &quot; & vbCrLf & fld.path & &quot;...&quot;<br>
nDirs = nDirs + 1<br>
If fld.SubFolders.Count &gt; 0 Then<br>
For Each tFld In fld.SubFolders<br>
DoEvents<br>
FindFile = FindFile + FindFile(tFld.path, sFile, nDirs, nFiles)<br>
Next<br>
End If<br>
End Function<br>
<br>
Private Sub Command5_Click()<br>
List1.Clear<br>
Dim nDirs As Integer, nFiles As Integer, lSize As Long<br>
Dim sDir As String, sSrchString As String<br>
sDir = InputBox(&quot;Please enter the directory to search&quot;, &quot;FileSystemObjects example&quot;, &quot;C:\&quot;)<br>
sSrchString = InputBox(&quot;Please enter the file name to search&quot;, &quot;FileSystemObjects example&quot;, &quot;vb.ini&quot;)<br>
MousePointer = vbHourglass<br>
Label1.Caption = &quot;Searching &quot; & vbCrLf & UCase(sDir) & &quot;...&quot;<br>
lSize = FindFile(sDir, sSrchString, nDirs, nFiles)<br>
MousePointer = vbDefault<br>
MsgBox Str(nFiles) & &quot; files found in&quot; & Str(nDirs) & &quot; directories&quot;, vbInformation<br>
MsgBox &quot;Total Size = &quot; & lSize & &quot; bytes&quot;<br>
End Sub<br>
<br>
(just in case you were curious) <p>Karl<br><a href=mailto:kb244@bellsouth.net>kb244@bellsouth.net</a><br><a href= </a><br>
 
OK i got it figured out, thanks any how tho, if yer curious to see how, here you go<br>
need 2 listboxes (list1 list2), a directory list (dir1) and a Drive List (drive1)<br>
a Command Button(called Start), a label (label1, pretty big too)<br>
everything is by default name, other than the start button<br>
<br>
Option Explicit<br>
Dim fso As New FileSystemObject<br>
Dim fld As Folder<br>
<br>
Private Sub Dir1_Change()<br>
Form1.Caption = Dir1.path<br>
End Sub<br>
<br>
Private Sub Drive1_Change()<br>
Dir1.path = Drive1.Drive<br>
End Sub<br>
<br>
Private Sub Form_Load()<br>
Dir1.path = Drive1.Drive<br>
End Sub<br>
Private Sub Start_Click()<br>
Dim NumD As Integer, NumF As Integer, CountDF As Integer, j As Integer<br>
CountDF = CleanUp(Dir1.path, &quot;d_*.*&quot;, NumD, NumF)<br>
Label1.Caption = &quot;Found &quot; & NumF & &quot; Files and Searched &quot; & NumD & &quot; Directories&quot;<br>
For j = 0 To List1.ListCount - 1<br>
fso.DeleteFile List1.List(j), True<br>
Next j<br>
For j = 0 To List2.ListCount - 1<br>
fso.DeleteFolder List2.List(j), True<br>
Next j<br>
End Sub<br>
Private Function CleanUp(ByVal sFol As String, sFile As String, nDirs As Integer, nFiles As Integer) As Long<br>
Dim tFld As Folder, tFil As File, FileName As String<br>
Set fld = fso.GetFolder(sFol)<br>
FileName = Dir(fso.BuildPath(fld.path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)<br>
While Len(FileName) &lt;&gt; 0<br>
CleanUp = CleanUp + FileLen(fso.BuildPath(fld.path, FileName))<br>
nFiles = nFiles + 1<br>
List1.AddItem fso.BuildPath(fld.path, FileName) ' Load ListBox<br>
FileName = Dir() ' Get next file<br>
DoEvents<br>
Wend<br>
Label1 = &quot;Searching &quot; & vbCrLf & fld.path & &quot;...&quot;<br>
nDirs = nDirs + 1<br>
If fld.SubFolders.Count &gt; 0 Then<br>
For Each tFld In fld.SubFolders<br>
DoEvents<br>
If Left(tFld.Name, 1) = &quot;_&quot; Then List2.AddItem (tFld.path)<br>
CleanUp = CleanUp + CleanUp(tFld.path, sFile, nDirs, nFiles)<br>
Next<br>
End If<br>
End Function <p>Karl<br><a href=mailto:kb244@bellsouth.net>kb244@bellsouth.net</a><br><a href= </a><br>
 
Hmm... i however get an Overflow error in this function<br>
<br>
Private Function CleanUp(ByVal sFol As String, sFile As String, nDirs As Integer, nFiles As Integer) As Long<br>
Dim tFld As Folder, tFil As File, FileName As String<br>
Set fld = fso.GetFolder(sFol)<br>
FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)<br>
While Len(FileName) &lt;&gt; 0<br>
CleanUp = CleanUp + FileLen(fso.BuildPath(fld.Path, FileName))<br>
nFiles = nFiles + 1<br>
List1.AddItem fso.BuildPath(fld.Path, FileName) ' Load ListBox<br>
FileName = Dir() ' Get next file<br>
DoEvents<br>
Wend<br>
Label1 = &quot;Searching &quot; & vbCrLf & fld.Path & &quot;...&quot;<br>
nDirs = nDirs + 1<br>
If fld.SubFolders.Count &gt; 0 Then<br>
For Each tFld In fld.SubFolders<br>
DoEvents<br>
If Left(tFld.Name, 1) = &quot;_&quot; Then List2.AddItem (tFld.Path)<br>
CleanUp = CleanUp + CleanUp(tFld.Path, sFile, nDirs, nFiles)<br>
Next<br>
End If<br>
End Function <p>Karl<br><a href=mailto:kb244@bellsouth.net>kb244@bellsouth.net</a><br><a href= </a><br>
 
try this (not tested)<br>
<br>
Private Sub ff(fldSrch as Scripting.Folder)<br>
Dim fld as Scripting.Folder<br>
Dim fyl as Scripting.File<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;if fldSrch.SubFolders.Count Then<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for each fld in fldSrch.Subfolders<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ff fld<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next fld<br>
&nbsp;&nbsp;&nbsp;&nbsp;end if<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;for each fyl in fldSrch.Files<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if .... ' your name check<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' add to list box or something<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' before you delete it<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end if<br>
&nbsp;&nbsp;&nbsp;&nbsp;next fyl<br>
<br>
end sub<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
I was just curious. I have a folder in which files are saved automatically. I would like to implement a program to delete files that were saved older than 3 months ago. My problem is that when I try to say:

for each file in folder
'determine file date
'if file date older than 3 months delete
end if

I get an error at the for each file in folder. Evidentally, the file variable is not equal to anything so this will not work. How would I access each individual file within a singl folder so that I can then access it's properties and then delete if neccessary?

Thanks B-)
 
Dim strCurrentFile as string
strCurrentFile = Dir(&quot;C:\MyFolder\*.*&quot;)
Do Until strCurrentFile = &quot;&quot;
'determine file date
'if file date older than 3 months delete
strCurrentFile = Dir()
Loop
 
That would work if you are going to delete every file. How would I go about moving to the next file within a folder if I do not delete the strCurrentFile?
 
That's where the *.* comes in. I assume you would have a naming convention of some kind if you only wanted to delete files of a certain type. For instance, let's assume that the folder has multiple types of files, but you only want to delete those from your &quot;Current News Letter&quot;. You named those files CNL & the first 5 letters of the submitters last name: CNLJones.txt
So now, your code would be:

Dim strCurrentFile as string
strCurrentFile = Dir(&quot;C:\MyFolder\CNL*.txt&quot;)
Do Until strCurrentFile = &quot;&quot;
If file date older than 3 months then
delete
End if
strCurrentFile = Dir()
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top