Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? Keeping OneDrive Directories Pinned

Dec
265
4
While working with Gemini to write a script to force files to cloud-only, I noticed that Gemini wrote it in such a way that it skipped directories. I asked why. Gemini said directories occupy little space and should not be forced to be cloud-only. To be honest, I don't really know how OneDrive, iCloud, or Google Drive manages directories. For all I know, OneDrive may keep the directories local even if they are set to cloud-only; there is no advantage to them being cloud-only.

That aside, I asked Gemini to write a TCC script to set all directories to be pinned on my OneDrive. Here is the code Gemini generated.

==================

@echo off
setlocal

:: Define the root OneDrive folder
set "OneDriveRoot=D:\OneDrive"

:: Setup the log file in the script's current directory
set "LogFile=%_startpath%CacheOneDriveDir.log"

:: Initialize the log file
echo =================================================== > "%LogFile%"
echo CacheOneDriveDir Native Optimization Run: %_date %_time >> "%LogFile%"
echo Target Root: %OneDriveRoot% >> "%LogFile%"
echo =================================================== >> "%LogFile%"
echo. >> "%LogFile%"

iff not isdir "%OneDriveRoot%" then
echo [ERROR] OneDrive root folder not found at: %OneDriveRoot%
quit 1
endiff

echo Optimizing OneDrive directories...
echo Logging updates to: %LogFile%

:: 1. Force-Pin the Root Directory
:: (We do this silently as it's just a single fast operation)
attrib /q /D +P -U "%OneDriveRoot%"

:: ---------------------------------------------------------------------
:: 2. The Native Sweep (Zero Script-Level Looping)
:: ---------------------------------------------------------------------
:: TCC's ATTRIB command has native filtering (/A:...). It will internally
:: scan the disk, skip everything that doesn't match, apply the new
:: attributes, and output ONLY the paths it actually touched.

:: Sweep A: Find all subdirectories (/S /D) that are NOT Pinned (/A:D-P)
attrib /S /D /A:D-P +P -U "%OneDriveRoot%\*" >> "%LogFile%" 2>&1

:: Sweep B: Find all subdirectories (/S /D) that are stuck with the Unpinned flag (/A:DU)
attrib /S /D /A:DU +P -U "%OneDriveRoot%\*" >> "%LogFile%" 2>&1

:: Finalize the log entries
echo. >> "%LogFile%"
echo =================================================== >> "%LogFile%"
echo Optimization Completed: %_date %_time >> "%LogFile%"
echo =================================================== >> "%LogFile%"

echo.
echo [SUCCESS] Optimization complete. Check "%LogFile%" to view changed directories.
endlocal
quit

==================

Any thoughts on this?

Craig
 
Last edited:

The Tug-of-War Scenario​

Imagine you have a directory named \Projects that is Pinned, and inside it is a 5GB video file that is Unpinned.

  1. The Script Runs: At 11 PM, your "Ruthless Eviction" script runs. It targets that 5GB file, explicitly strips the pin (-P), applies the eviction command (+U), and successfully turns the file into a 0-byte cloud stub.
  2. OneDrive Wakes Up: A few seconds later, the background OneDrive sync engine notices a change has occurred in the \Projects directory.
  3. The Policy Enforcement: OneDrive checks the directory and sees its master policy: "I am a Pinned folder. Everything inside me must be local." It immediately sees the 5GB cloud stub, realizes it is out of compliance with the folder's policy, and automatically starts downloading the 5GB payload right back to your hard drive.
Your script successfully evicted it, but the folder's inheritance rule pulled it right back.

The Golden Rule of Eviction​

Because of this top-down inheritance, the golden rule for your automation setup is this: Never run an eviction script against a directory that you have manually pinned at the root level.

  • For your Archive folders: Leave the directories Unpinned, and let the script sweep through and dehydrate the aging files inside.
  • For your Active Work folders: Pin the directory so everything inside is lightning fast and locally available, but exclude that specific directory path from your script's target list (%Dirs%).
By keeping your pinned folders and your eviction folders strictly separated, you stop the sync engine from fighting your automation in the background!
 
Back
Top