10

Is there any way to check if a OneDrive file is saved locally via a terminal in Windows?

  • I'd like to automatically run a check of all data files associated with a project without the user having to ensure all files are saved locally by selecting Always keep on this device in the OneDrive sync settings for that folder
  • Within a file's Properties, it's possible to see the file size and file size on disk, but when I try to query the file size from a terminal, it returns the full file size even though it isn't saved to disk (it would be a viable solution if I was able to query the size on disk):
    File Properties Screenshot (size & size on disk)
JW0914
  • 7,052
  • 7
  • 27
  • 48
Callum Munn
  • 103
  • 1
  • 5
  • Does your check program work in a way that prevents OneDrive from automatically downloading the files on demand? – u1686_grawity Apr 28 '22 at 13:46
  • 3
    It doesn't prevent it from downloading automatically but the projects are prepared with internet access and then used in areas of patchy or no internet connectivity so the idea is to flag up any files that should be downloaded during the project preparation stage – Callum Munn Apr 28 '22 at 14:06

1 Answers1

14

I think it's possible to query size-on-disk, but that could be misleading if you happened upon a sparse file, or NTFS compressed file, or something else that's legitimately smaller on disk.

Instead, query the file attributes (the same as Hidden/ReadOnly) – if the file is not cached locally, it will have the Offline1 and RecallOnAccess attributes (bits 0x1000 and 0x400000 respectively).

Such files also seem to have the undocumented Unpinned attribute (0x100000), though I'm not sure if it's always all 3 attributes that are present or if there are situations.

Meanwhile, "Always keep on this device" corresponds to the Pinned attribute (0x80000). Files that are temporarily cached have neither of those attributes.

In PowerShell, (Get-Item $file).Attributes will have these flags.

If you're working with bare Cmd (no access to PowerShell), the attrib command will show "Recall on access" as the M flag and (I think) "Offline" as O. It also allows changing the pinned/unpinned flags, so you can actually mark files as "always keep" purely through the command line.

This is mostly specific to OneDrive. Last I checked, Dropbox worked quite differently.


1 The Offline attribute in Windows predates OneDrive by years – its origin was the "Hierarchical Storage Management" subsystem, which allowed unused files to be offloaded to tape or similar. Back then, data that was stored on the running system was "online" and data that was sitting in a cupboard was "offline". With cloud storage, the meanings have flipped.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • 1
    You are indeed correct. A synced and locally available file with have attributes of `AL` visible in explorer, an "offline" unsynchronised file will show `ALOM` and as you say these should be easily detectable on the command line. – Mokubai Apr 28 '22 at 13:21
  • Last time I poked at OneDrive was around 2019, and I remember `attrib` handling some of the newly-added attributes in a different way than the rest (at least according to ProcMon), but I can't remember exactly how that worked. But as far as I can remember, in PowerShell they all showed up as part of the same `.Attributes` property. – u1686_grawity Apr 28 '22 at 13:22
  • Just wanted to say: Thanks for the history lesson at the end :) The way it got flipped is beatufiully counter intuitive and yet logical :) – David Mulder Apr 29 '22 at 06:38
  • 1
    FYI: It's the definition of *online* that's flipped, or more precisely, what *online* is referring to. *Online* w.r.t. storage always means "disk up and spinning, ready to interact", whereas OneDrive interprets *online* as with "internet connection". Clearly "your disk is online" is not the same as "your internet is online". – iBug Apr 29 '22 at 13:15