<div dir="auto"><div><br><br><div class="gmail_quote"><div dir="ltr">Le ven. 21 sept. 2018 17:47, Tony Morehen <<a href="mailto:tmorehen@ajm-software.com" target="_blank" rel="noreferrer">tmorehen@ajm-software.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Here's an all Gambas function. It's recursive and includes the size of <br>
the original directory. It's comparable in speed to du.<br>
<br>
Public Function ScanDir(sDir As String) As Long<br>
<br>
Dim sFile As String<br>
Dim sPath As String<br>
Dim hStat As Stat<br>
Dim aDir As String[]<br>
Dim iSize As Long<br>
<br>
Try hStat = Stat(sDir)<br>
If Error Then Return -1<br>
iSize = hStat.Size<br>
If hStat.Type <> gb.Directory Then Return iSize<br>
Try aDir = Dir(sDir)<br>
<br>
For Each sFile In aDir<br>
<br>
sPath = sDir &/ sFile<br>
Try hStat = Stat(sPath)<br>
If Error Then Continue<br>
<br>
With hStat<br>
If .Type = gb.Directory Then<br>
Try iSize += ScanDir(sPath)<br>
Endif<br>
Try iSize += .Size<br>
End With<br>
Next<br>
Return iSize<br>
<br>
End<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">This is not a bad function but there is some little things illogical. </div><div dir="auto"><br></div><div dir="auto">You must not manage errors is the function as it return in this case a false value not managed. It is better if you let Gambas propagate an error you manage then on the initial call.</div><div dir="auto"><br></div><div dir="auto"><span style="font-family:sans-serif">Public Function ScanDir(sDir As String) As Long</span><br style="font-family:sans-serif"><br style="font-family:sans-serif"><span style="font-family:sans-serif"> Dim sFile As String</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> Dim sPath As String</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> Dim hStat As Stat</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> Dim iSize As Long</span><br style="font-family:sans-serif"> 'Generate an error if it is not a dir<br style="font-family:sans-serif"><br style="font-family:sans-serif"><span style="font-family:sans-serif"> For Each sFile In Dir (sDir)</span><br style="font-family:sans-serif"><br style="font-family:sans-serif"><span style="font-family:sans-serif"> sPath = sDir &/ sFile</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> hStat = Stat(sPath)</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> With hStat</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> If .Type = gb.Directory Then</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> iSize += ScanDir(sPath)</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> Endif</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> iSize += .Size</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> End With</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> Next</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> Return iSize</span><br style="font-family:sans-serif"><br style="font-family:sans-serif"><span style="font-family:sans-serif">End</span><br></div><div dir="auto"><span style="font-family:sans-serif"><br></span></div><div dir="auto"><span style="font-family:sans-serif">Use will be</span></div><div dir="auto"><span style="font-family:sans-serif"><br></span></div><div dir="auto"><span style="font-family:sans-serif">Try ISize =Scandir (sDir)</span></div><div dir="auto"><span style="font-family:sans-serif"><br></span></div><div dir="auto"><span style="font-family:sans-serif">If error then... it is not possible to get the dir size.</span></div><div dir="auto"><span style="font-family:sans-serif"><br></span></div><div dir="auto"><span style="font-family:sans-serif">In this function we can (need) add management for links</span></div><div dir="auto"><span style="font-family:sans-serif"><br></span></div><div dir="auto"><span style="font-family:sans-serif"><br></span></div><div dir="auto"><span style="font-family:sans-serif">--</span></div><div dir="auto"><span style="font-family:sans-serif">Fabien</span></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"></blockquote></div></div></div>