Page 1 of 1

drives files and folders

Posted: Sat Sep 15, 2012 3:30 am
by oldefoxx
There are three things that every person that programs has to get into sooner or
later, and they are tied together. That is drives, folders, and files. Now many
books on programming only get as far as the file stage. A few might get into
the current folder as well. It is a very few that go beyond that point.

Let's start off with drives first. Following long established protocols, when dealing
with DOS or Windows, drives are assigned a letter of identification from the 26
letters in the alphabet. Early PCs only had floppy drives, and the letters "A" and
"B" were reserved for them, since the idea was to just have one or two floppy
drives per PC. That seemed more than adequate at the time.

The first large drives where given the letter "C"., and if a second was installed,
it was given the letter "D". Note that capital letters and lower case letters all
address the same drive, so "C" is the same as "c" and "D" is the same as "d".

When CD drives came along, and later the DVD and DVD/CD drives, they also got
a single letter identifier. PCs fell into a new configuration, of one floppy drive
(the "A" drive), one hard drive (the "C" drive), and one CD or DVD/CD drive (the
"D" drive). Some PCs came out with a "hidden" D Drive, which held a backup of
all the software that came initially installed on the PC when it was sold as new.

Hard drives got bigger at a fast rate, This created problems with existing
software being able to access a whole drive. What they did was divide a large
hard drive into logical drives, each one seeming to be a separate hard drive to
the system. Each one correspondingly got its own drive letter. Because of the
conventions followed, a hard drive could be divided into four logical drives. To
go beyond this limit later on, one logical drive was earmarked to be an extended
logical drive, and it could be divided in additional logical drives. But while the
drives growing in how much data they could hold, they were shrinking in size
and the system software was catching up with handling larger capacity drives.

What you have now is hardly any floppy drives, the drive letters "A" and "B'
unused as a consequent, a huge hard drive as your drive "C", your CD or
DVD/CD drive now the "D" and maybe the "E" drive if you have two, possibly
some networked drives that are each given a letter, and beyond that, it is
anyone's guess. The owner of the PC should know what he or she has, but
how about writing software that works with as few or as many drives as there
actually are?

Okay =, so you have drives to deal with, and to do that, you want your software
to be able to check out the ones that are there. And here is a program that
shows you several ways of doing that using PureBasic:

Code: Select all

Procedure waitkey()
  Print("Press a key to continue... ")
  Repeat
    Delay(25)
    k$=Inkey()
    If k$>""
      Break
    EndIf
  ForEver
  PrintN("")
EndProcedure
  
OpenConsole()                                  ; First we must open a console
ConsoleTitle ("What Drives Do I Have (Can I Use)")                                                  
EnableGraphicalConsole(1)
ConsoleColor(15,1)
ClearConsole() 
PrintN("Here are four ways to identify the usable drives in a PC system.")
PrintN("MAXIMIZE your console screen to seem them all at work, one at a time.")
ConsoleLocate (0,3)                          ; x y position 
PrintN("We try to access each drive in turn using ExamineDirectory()")
PrintN("")
For a.l=Asc("C") To Asc("Z")
  b.l = ExamineDirectory(#PB_Any, Chr(a)+":\", "\")
  
  If b
    PrintN("Drive "+Chr(a)+": can be used.")
    FinishDirectory(b)
  EndIf
Next
waitkey()
PrintN("")
PrintN("Now we try to access each drive in turn using OpenFile()")  
PrintN("")
For a.l=Asc("C") To Asc("Z")
  b.l = OpenFile(#PB_Any, Chr(a)+":\nul")
  If b
    PrintN("Drive "+Chr(a)+": can be used.")
    CloseFile(b)
  EndIf
Next
waitkey()
PrintN("")
PrintN("Now we try to access each drive in turn using ReadFile()")  
PrintN("")
For a.l=Asc("C") To Asc("Z")
  b.l = ReadFile(#PB_Any, Chr(a)+":\nul")
  If b
    PrintN("Drive "+Chr(a)+": can be used.")
    CloseFile(b)
  EndIf
Next
PrintN("")
PrintN(" If you fail to see a drive, such as a CD drive or DVD/CD drive, it could be")
PrintN(" that it is not available, not ready or there is some other reason that it is")
PrintN(" not responding.  The three checks above do not depend on whether the drive")
PrintN(" is Read Only or not.  If you want to be sure that, the next check (and a")
PrintN(" valid file name rather than 'nul') should verify that it can be written to.")
PrintN("")
waitkey()
PrintN("")
PrintN("Now we try to access each drive in turn using CreateFile()")  
PrintN("")
For a.l=Asc("C") To Asc("Z")
  b.l = CreateFile(#PB_Any, Chr(a)+":\nul")
  If b
    PrintN("Drive "+Chr(a)+": can be used.")
    CloseFile(b)
  EndIf
Next
waitkey()
Now I mentioned folders as well. When thinking of folders, think of those manila things
that fit in office cabinets. Cabinets come in different sizes with different number of
drawers in them, going back different depths. The cabinets are like the hard drives
discussed earlier. Most anything can go into them. To keep it orderly, we put in
folders and arrange things in the folders and by folders, putting names to the folders
so that we know where we are. But when you are talking about folders, you are also
going to run into three other terms: subfolders, directories, and subdirectories.

So folders are also directories, right? Exactly. So what is a subfolder or a subdirectory?
It is a folder or directory that is linked back to a higher level folder or directory. It's
like looking at a tree, and seeing branches growing out of branches, and more
branches growing out of them in turn. Now that you have been shown how to get
around from tree to tree (meaning drive to drive), your next journey might be to
get from limb to limb as you work your way up a tree. Limbs in this case another
name for branches, like folders are another name for directories.


.

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Posted: Sat Sep 15, 2012 9:24 am
by c4s
@oldefoxx
There is another easier and probably faster way using the FileSize() function:

Code: Select all

Debug "Available drives:"
For i = 'A' To 'Z'
	If FileSize(Chr(i)+":\") = -2  ; "File" exists and is a directory
		Debug "- Drive with letter "+#DQUOTE$+Chr(i)+#DQUOTE$+" is available"
	EndIf
Next
Anyway, it's up to the moderators but I think this should go into the Tricks 'n' Tips forum. The help file already contains good examples of how of deal with files/folders and the whole idea of this "let's start" chapter is to just give beginners a quick overview of how to start...

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Posted: Sat Sep 15, 2012 9:25 pm
by oldefoxx
I'm not really fussy about the method used for identifying the drives. I just set out to
see if I could find one or more ways, and I came up with four. You've added a fifth.

I'm sitting here now trying to contemplate just how you would range up one of those
drive and find either all the folders and subfolders it contains, or both all the folders
and subfolders and all the files that it contains. Combining all this effort together,
you could do all sorts of things, like:

Determine how many files you have with the extensions of "TMP", "BAK", or "LOG",
and how much drive space they consume. Maybe get rid of some as well.

Find all your files with the extensions "ZIP" or "RAR" and move them to a new RAR-ZIP
folder set up somewhere on your system.

Find any and all files that have a file length of zero and delete them. Heck, maybe you
decide a filesize of 5 and under is too small and get rid of a few more.

Find all empty folders and delete them.

Check for files that are duplicates. Can't always go by name, but you can line them up
by size first, then whether the content matches if two files have the same size. The
date created or the date last written to may be significant as well,

Yes, you can get programs that do these things for you. If that is they way you want to
fly, nobody is going to stop you. But what if you could do it yourself? Would you want to?

Re: drives files and folders

Posted: Sat Sep 15, 2012 10:33 pm
by c4s
oldefoxx wrote:I'm sitting here now trying to contemplate just how you would range up one of those
drive and find either all the folders and subfolders it contains, or both all the folders
and subfolders and all the files that it contains.
The PureBasic forum is really a gold mine for answering such questions. For example try searching for "recursive search"...

Re: drives files and folders

Posted: Sun Sep 16, 2012 1:07 am
by kenmo
Hello oldefoxx,

Here's some code I posted last year, you may be interested -- it recursively finds files, and then you can process them by size, type, contents, or otherwise:

http://www.purebasic.fr/english/viewtop ... 5&p=350186

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Posted: Sun Sep 16, 2012 2:48 am
by BorisTheOld
oldefoxx wrote:Combining all this effort together,
you could do all sorts of things, like:

Determine how many files you have with the extensions of "TMP", "BAK", or "LOG",
and how much drive space they consume. Maybe get rid of some as well.

Find all your files with the extensions "ZIP" or "RAR" and move them to a new RAR-ZIP
folder set up somewhere on your system.

Find any and all files that have a file length of zero and delete them. Heck, maybe you
decide a filesize of 5 and under is too small and get rid of a few more.

Find all empty folders and delete them.

Check for files that are duplicates. Can't always go by name, but you can line them up
by size first, then whether the content matches if two files have the same size. The
date created or the date last written to may be significant as well,
It's irresponsible to state or imply that any of the above are desirable activities.

If I tried any of that stuff I'd destroy all of my systems.