10

I would like to provide a way to recognize when a large file is fragmented to a certain extent, and alert the user when they should perform a defragmentation. In addition, I'd like to show them a visual display demonstrating how the file is actually broken into pieces across the disk.

I don't need to know how to calculate how fragmented it is, or how to make the visual display. What I need to know is two things: 1) how to identify the specific clusters on any disk which contain pieces of any particular given file, and 2) how to identify the total number of clusters on that disk. I would essentially need a list of all the clusters which contain pieces of this file, and where on the disk each of those clusters is located.

Most defragmentation utilities have a visual display showing how the files are spread across the disk. My display will show how one particular file is split up into different areas of a disk. I just need to know how I can retrieve the necessary data to tell me where the file's clusters/sectors are located on the disk, so I can further determine how fragmented it is.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • Are you looking to identify the file(s) in a particular sector, or are you looking to identify the sectors occupied by a given file? – James L. Oct 05 '12 at 18:50
  • The sectors occupied by a given file. – Jerry Dodge Oct 05 '12 at 18:51
  • 5
    This is a basic operating system question (see [NTFS](http://en.wikipedia.org/wiki/NTFS)). Information about the physical locations of files on disk involves (very low level) strolls through the NTFS information (specifically the MFT - see `Metafiles` in the linked page). It's a very broad question to ask here. Study the link and research NTFS, and then you can come back and ask specific, concise questions. :-) For now, voting to close as not a real question because of the broad scope. (I did NOT downvote, however.) – Ken White Oct 05 '12 at 18:53
  • 1
    A similar discussion on social.msdn...: http://social.msdn.microsoft.com/Forums/en-AU/vcgeneral/thread/9448eea5-b0c8-4060-8972-3e632daa5066 (I did not down-vote either. It sure is nice when a down-voter leaves a comment!) – James L. Oct 05 '12 at 18:54
  • 1
    And one more similar thread (http://www.codeproject.com/Questions/425710/find-files-occupied-sectors-on-hard-disk) where they say you can't do it without a low-level kernel driver. – James L. Oct 05 '12 at 19:00
  • What are you trying to do with the information? How will the user use the information? – David Heffernan Oct 05 '12 at 19:29
  • 1
    @DavidHeffernan Is that relevant to the process of acquiring this information? It will be displayed in a graphic view showing how various files have been saved/split in the sectors on a disk. – Jerry Dodge Oct 05 '12 at 19:39
  • Do you want to map the entire disk? In which case how are you going to display it? Do you know how many sectors there are on modern disks? – David Heffernan Oct 05 '12 at 19:54
  • This was part of my question actually is how to identify how many. I will not show an image of the entire disk, only zoom in on certain areas. But that all depends on how the data is actually saved - if one small file contains bits of data at the beginning and end of the disk, then it will virtually show the entire disk (zoomed out very small) but if the file is grouped in the same place, it will zoom in. The goal is to compare multiple different files and analyze how they were actually saved, visually see if and how they're split up. – Jerry Dodge Oct 05 '12 at 20:11
  • 1
    Start [here](http://msdn.microsoft.com/en-us/library/windows/desktop/aa363970%28v=vs.85%29.aspx) for info on the type of structure you need to pass to [DeviceIOControl](http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216%28v=vs.85%29.aspx) in order to even start retrieving the information about the number of sectors. It doesn't even touch the part about physical file locations. Good luck. – Ken White Oct 05 '12 at 20:18
  • 1
    @Jerry: "visually see if and how they're split up" - you do realize you can save the same file 10 times, and it can be split/not split/split into a different number of locations differently each time based on the disk content at the time, fragmentation, file size, and other factors? IOW, you seem to be working on a flawed concept from the start. – Ken White Oct 05 '12 at 20:52
  • @KenWhite Yes, I do understand, and I'm not expecting multiple copies of the same file to be saved the same way. – Jerry Dodge Oct 05 '12 at 20:56
  • 1
    @James, that Code Project page does not say you need a special driver for what Jerry has asked. It says you need a special driver to *write* to a sector, which is not what this question is asking for at all. The only thing in this question that hints about writing is the mention of defragmentation tools, and even that doesn't require making any driver — there's [an API for it](http://msdn.microsoft.com/en-us/library/windows/desktop/aa363911.aspx). – Rob Kennedy Oct 05 '12 at 22:04
  • @RobKennedy The end goal of this project is to actually demonstrate to a user when certain files are fragmented, and visually show how fragmented it is, to explain how the needle on the disk jumps around to read/write that file. Now, with the link in your last comment, I can go one step further and actually defragment the file for them :D – Jerry Dodge Oct 06 '12 at 19:26
  • Question: If I modify my question to point out the one question which I really meant to ask, could I get it re-opened? Because all this is new to me, and I was unclear what I really wanted to ask until I saw what was involved. – Jerry Dodge Oct 06 '12 at 23:59
  • 1
    Why not just post a new question and ask it there? This one has an accepted answer. – Ken White Oct 07 '12 at 00:18
  • True, but that one would be considered an exact duplicate, and we would be producing redundant questions. I was hoping for someone to come along with a more detailed answer than a couple references and a quote from one of them, in which case the accepted answer might change. – Jerry Dodge Oct 07 '12 at 00:20
  • 1
    @Jerry - The original question asked for general pointers (*"Is there a way to ...*"), hence the answer you got.. Please don't modify a question that would render a given answer irrelevant. If you ask a new different-enough question, it shouldn't get closed as a duplicate. – Sertac Akyuz Oct 07 '12 at 00:57
  • @SertacAkyuz The accepted answer still does answer the question. I included the same details, but worded it differently to make my point more clear. – Jerry Dodge Oct 07 '12 at 02:11
  • @Jerry - Indeed it does seem so. It's as generalized as before. Put my vote back as I like the question, but I guess it's still not an exact fit on SO. – Sertac Akyuz Oct 07 '12 at 02:20
  • Once again, I have to ask... You **are aware** that Windows versions since Vista automatically defrag the drive in the background, and therefore there's no need to defrag it yourself? – Ken White Oct 07 '12 at 03:12
  • @KenWhite There are some other things which you're not aware of, such as this being a SQL Database file which cannot be defragmented without stopping SQL Server. – Jerry Dodge Oct 07 '12 at 17:55
  • @JerryDodge In which case, what is the user going to do with that knowledge? Stop SQL server? Defragmentation tends not to be a big deal with database files now that modern machines have so much RAM that the disk cache hides the fragmentation. – David Heffernan Oct 07 '12 at 18:00
  • 1
    Voters please see http://meta.stackexchange.com/questions/149789/re-question-12751684-how-do-i-identify-literally-where-files-are-stored-on-a-d – Harry Johnston Oct 08 '12 at 20:21
  • @JerryDodge: What if the disk is a SSD? Fragmentation doesn't matter, and telling a user to defragment is actually counter-productive (it reduces their SSD's lifespan). – josh3736 Oct 08 '12 at 20:26
  • 1
    @DavidHeffernan: Presumably the purpose is to answer the question, "should I schedule a maintenance period so that I can defragment the database file"? – Harry Johnston Oct 08 '12 at 20:57

1 Answers1

11

You can use the DeviceIoControl function with the FSCTL_GET_RETRIEVAL_POINTERS control code.

The FSCTL_GET_RETRIEVAL_POINTERS operation retrieves a variably sized data structure that describes the allocation and location on disk of a specific file. The structure describes the mapping between virtual cluster numbers (VCN offsets within the file or stream space) and logical cluster numbers (LCN offsets within the volume space).

RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 2
    While you're there, see [`IOCTL_DISK_GET_DRIVE_GEOMETRY_EX`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365171%28v=vs.85%29.aspx) for sector count. – Sertac Akyuz Oct 05 '12 at 22:30
  • Awesome, got the structure down in Delphi, now time for me to decipher what this huge list of numbers actually means :D Thank you. – Jerry Dodge Oct 06 '12 at 19:10