Solved

Write to a Specific Disk Location

Posted on 2000-03-20
12
188 Views
Last Modified: 2010-05-02
Hi,
   Is there any way to write/save a file to a specific portion of a hard or floppy disk using VB5?.
   I see MS Scandisk (for DOS) showing disk areas filled by data and others empty. So I assume that there must be a way to build up a picture of the disk sectors themselves, and if thats so, perhaps dump a file to a specific empty disk space. I wonder if anyones ever heard of any way of doing this. Any leads to stuff on the net that might help would be appreciated.

                  chevy75@mindspring.com
0
Comment
Question by:thunder44
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 2638764
This is specifically NOT recommended as it violates windows task encapsulation rules. Generally there is no need for this low level device stuff.

You can get the contents of the disk by looking at the FAT (but not under NT). The FAT contains the pointer to the first cluster of the file chain and you *could* theoretically write a file block by block, but VB has no provision to do so as this is not a function that application level code should be performing.

There are internal hooks for this stuff because SCANDISK and DEFRAG can do it, but they weren't written in VB. I can do it in DOS using BIOS calls, but the need has never arisen at the VB level.

M
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2638782
Windows / DOS uses a forward seeking File Allocation method. What that means is that when a disk write operation is issued.. the operating system goes through the file allocation table sequentially, and starts writing the file in the first open space it encounters. If the file is longer than the free sector, the operating system continues along the file allocation table, and uses the next available sector. When you delete a file, the file allocation sectors that were used by it are freed up.

As the free sectors and sectors that need to be written don't always match up, you get what is called file fragmentation.. where parts of a given file could be all over the disk.. and yes.. with a high level of file fragmentation.. reading a file can be an all over the disk retrieval chore. That is why MS provides a Disk Defragmenting utility, which when run, pieces your files back together all in one contiguous space.. and I suggest you run it routinely.

As the File Allocation table is a very low level operating system function.. (if it gets screwed up.. you lose your whole disk).. it is definitely NOT a good idea to mess with it.

In all honesty, if you defrag your disk routinely, then having a little bit of fragmentation (<5%) is no real big problem.. you won't even notice it.. <smile>.
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2638843
wsh2's comments do NOT apply to NTFS disks that use a whole 'nother approach.

M
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2638969
Mark2150's comment about NTFS should be taken in good stead.. <smile>.

As Mark2150 is an expert on NTFS, perhaps he could elaborate on this whole 'nother approach' concept. As NTFS is a VERY mature technology (ie.. been around for quite a bit), I tend to believe that NTFS shares many of the same traits as its Windows / DOS counterparts. The only difference being.. it is wrapped up in phenomenal security.. combined with perpetual disk housekeeping tasks being performed routinely.

0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2639297
wsh2: NTFS is *not* just FAT with security. An NTFS drive doesn't have a single monolithic FAT, and the format of the directories is also different--in fact, it's possible for a small file to be completely contained within its directory entry under NTFS! In addition, an NTFS file is formed of one or more "streams", one of which is named (the one you actually see) and the others accessible programmatically. I could go into a *lot* more detail if I could find the relevant MSDN article on this...
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2639306
wsh2: Found it! Try looking at Knowledge Base article Q100108 for a concise description of FAT, HPFS and NTFS. This is fairly old (it refers to NT 3.1) but it gives a good idea of the differences.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 12

Expert Comment

by:mark2150
ID: 2639912
Didn't say I was expert on NTFS. Said that FAT's don't apply - which is 100% correct. NTFS is sufficiently differnt that all existing disk tools have to be rewritten to support. Entire paradigm of storage allocation is radically different. Since NT was designed to eliminate DOS there are no "DOS Calls" to handle it. All old .ASM that does direct disk I/O will *NOT* run.

M
0
 

Author Comment

by:thunder44
ID: 2642775
     Thanks for the input so far, just as a point of reference, any attempt to mess with the FAT would obviously be done on a disk carrying nothing valuable in the first place. This whole idea is an experiment out of curiosity, not necessity. The initial victim would probably be a floppy.
      I obviously need to research more on the FAT and using BIOS calls even if that means retreating from VB5 into some DOS level language like C, vaugely remember someone doing BIOS calls in this and QB45 but I may  be mistaken.        
     Judging by the comments above there doesn't seem to be a pre-cut solution anyones heard of, but mark2150's comments come closest to a place to start (should have thought of BIOS calls myself), so I'll take it from there. Post an answer not a comment mark2150 and collect your points. Thanks to all.
     
     
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2643124
Congrats Mark2150.. <smile>.


0
 
LVL 12

Accepted Solution

by:
mark2150 earned 100 total points
ID: 2643255
Just use the "Accept comment as answer" feature...

M
0
 

Author Comment

by:thunder44
ID: 2645688
Thanks to all
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2645862
My Que "DOS Programmers Reference" (ISBN 0-88022-327-8) says that you should be using BIOS INT13H, function 03H, "Write Disk Sector".

Calling registers:
AH = 03H
AL = Number of sectors (128 byte) to transfer (1-9)
ES:BX = Pointer to data buffer
CH = Track
CL = Sector
DH = Head (0-1)
DL = Drive (0-3)

Have FUN! :-)

M
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now