Thursday, January 5, 2012

Sandstorm's File & Folder Lister

Wondering what are the contents of your harddrive?  Here is a tool for that I call Sandstorm's File & Folder Lister.  It comes under several names out here in the web, some of which are:

a.  Directory Printer
b. File and Folder Lister
c. File Tree Printer
s. etc.

And when I surfed, you have to eventually buy to have one.  But  why would you buy something that can be easily made through our beloved VFP?  

Here is a code snippet for my lister:


Local oForm As Form
oForm = Createobject('TreeForm')
oForm.Show(1)
Return

Define Class TreeForm As Form
      AutoCenter = .T.
      Height = 500
      Width = 390
      Caption = "Sandstorm's Files and Folder Lister"
      MinButton = .F.
      MaxButton = .F.
      BorderStyle = 1

      Add Object label1 As Label With;
            top=35, Left= 10, Caption = "Tree RootLines Style",;
            AutoSize = .T., FontBold = .T.

      Add Object opttree As OptionGroup With ;
            top = 50, Left = 10, ButtonCount = 2, Value = 1, AutoSize = .T.,;
            BorderStyle = 0

      Add Object chkInclude As Checkbox With;
            top=105, Left = 10, Caption = "Include Files", AutoSize = .T.

      Add Object cmdSelFolder As CommandButton With;
            top=140, Left= 15, Caption = "Select A Folder", AutoSize = .T.

      Add Object lblFolder As Label With;
            top=170, Left= 15, Caption = "None Selected...", AutoSize = .T.,;
            FontUnderline = .T., ForeColor = Rgb(255,0,0)

      Add Object lblElapsed As Label With;
            top=170, Left= 290, Caption = "Elapsed: ", AutoSize = .T.,;
            ForeColor = Rgb(255,0,255)

      Add Object edit1 As EditBox With;
            top = 195, Left = 10, Width = 370, Height = 250, FontName = "Tahoma"

      Add Object cmdTree As CommandButton With ;
            top = 460, Left = 10, AutoSize = .T., Caption = "\<Create Tree Now"

      Add Object cmdWord As CommandButton With ;
            top = 460, Left = 260, AutoSize = .T., Caption = "\<Open Inside Word"

      Procedure Init
            With Thisform.opttree
                  .option1.Caption = "Extended Characters (Shown Properly Inside Word)"
                  .option1.AutoSize = .T.
                  .option2.Caption = "ASCII Characters"
                  .option2.AutoSize = .T.
            Endwith
            Thisform.AddProperty("_SourceFolder","")
            Thisform.AddProperty("_TreeFile",Addbs(Getenv("TMP"))+Sys(3))

      Function cmdSelFolder.Click
            Thisform._SourceFolder  = Justpath(Getdir())
            If !Empty(Thisform._SourceFolder)
                  Thisform.lblFolder.Caption = Iif(Len(Thisform._SourceFolder)>35,;
                        Left(Thisform._SourceFolder,35)+"...",Thisform._SourceFolder)
            Else
                  Thisform.lblFolder.Caption = "None Selected..."
            Endif
      Endfunc

      Function cmdTree.Click
            If !Empty(Thisform._SourceFolder)
                  * Create treeview for edit and compute elapsed time
                  Thisform.lblElapsed.Caption = 'Creating List...'
                  Local lnMin, lnSec
                  lnMin = Minute(Datetime())
                  lnSec = Seconds()
                  oShell = Createobject("WScript.Shell")
                  If Thisform.opttree.Value = 1
                        oShell.Run('cmd /c '+Iif(Thisform.chkInclude.Value = 0,;
                              'tree "','tree /f "')+Thisform._SourceFolder+;
                              '" > '+Thisform._TreeFile,2,.T.)
                  Else
                        oShell.Run('cmd /c '+Iif(Thisform.chkInclude.Value = 0,;
                              'tree /a "','tree /a /f "')+Thisform._SourceFolder+;
                              '" > '+Thisform._TreeFile,2,.T.)
                  Endif
                  *change appearance in editbox to make it more readable
                  lcFile = Strtran(Filetostr(Thisform._TreeFile),;
                        Chr(196),Chr(173))
                  lcFile = Strtran(lcFile,Chr(195),"+")
                  lcFile = Strtran(lcFile,Chr(179),"|")
                  lcFile = Strtran(lcFile,Chr(192),"+")
                  Thisform.edit1.Value = m.lcFile
                  Thisform.lblElapsed.Caption = Iif(Minute(Datetime())-lnMin <= 0,;
                        TRANSFORM(Seconds()-lnSec)+" seconds",;
                        TRANSFORM(Minute(Datetime())-lnMin)+" Minutes")
                  * Disable Timer to stop blinking of elapsed time
            Else
                  Messagebox("Operation Aborted!",0+64,"Cancelled")
            Endif
      Endfunc

      Function cmdWord.Click
            If !Empty(Thisform._SourceFolder)
                  * Create a new one to ensure it does not overwrite the old
                  * instance in Word.  In case user decides to repititively
                  * open it inside MSWord without recreating a new TreeView
                  * INT(Seconds()) is a trick to ensure of uniqueness
                  Local lcTempFile
                  lcTempFile = Thisform._TreeFile+Transform(Int(Seconds()))
                  Strtofile(Filetostr(Thisform._TreeFile),m.lcTempFile)

                  * Automate
                  Wait Window "Please wait while converting the file to "+;
                        " a viewable format inside MSWord!" Nowait
                  Local loword As Word.Application
                  loword = Createobject("word.application")
                  loword.Documents.Open(m.lcTempFile,,,,,,,,,,437)
                  loword.Visible = .T.
            Else
                  Messagebox("Please Select a Folder then click Create "+;
                        "Tree Now button first!",0+64,"Ooooppssss!")
            Endif
      Endfunc

Enddefine




This can give you a list of the folders and/or the files inside each folder.  This is very useful in case you want to map out the contents of your harddrive, USB, etc.  I used this to study the clutters in our server where users'  files are located and from there were able to decide better which files needs to be deleted.

So there you have it, a files and folders lister; free, fast, and accurate to the end.  I hope that in addition to helping you with your files and folders mapping need, this tool can guide you also on how to use Native VFP and external objects.

Enjoy!

3 comments:

  1. Brilliant!

    Just a note to people who may try it: If you ever select the root directory C:\ with this tool, the app will seem to hang. If that happens, just let it be. If you have plenty of files in C drive it will take a while before it can make a list of all the folders and files.

    ReplyDelete
  2. As always, you mastering another little piece of work, we need a lot of people like you in this planet not cause what you do but why the reason you do it, to help others, wish you more brilliant ideas
    Ernesto

    ReplyDelete