I synced a SharePoint directory to my Windows file system using OneDrive. As seen in the image below, some files are available locally, while others are only available in the cloud:
When I double-click on a file that's only available in the cloud, it gets downloaded automatically. However, I want to achieve the same result programmatically using the terminal (CMD).
Here’s what I tried:
I navigated to the directory containing the file:
cd "C:\Users\MyUsername\OneDrive\Documents"
I attempted to use the attrib
command to make the file available locally:
attrib -P +U my_file.iso
This did not produce any output, and the file remains only in the cloud.
I also tried without the file extension:
attrib -P +U my_file
This resulted in an error:
Could not find that file.
I want to programmatically trigger the download of a specific file (e.g., my_file.iso
) from the synced SharePoint directory to make it available locally, without manually double-clicking it.
attrib -P +U
on the file.dir
.I figured out I could use this simple PowerShell script:
\$filePath = "${filePath.replace('/', '\\')}"
if (!(Test-Path \$filePath)) {
throw "Could not find ISO file at \$filePath"
}
\$fileAttributes = (Get-Item \$filePath).Attributes
if (\$fileAttributes -band [System.IO.FileAttributes]::Offline) {
Write-Host "The file is only available in the cloud. Downloading..."
Get-Content \$filePath > \$null
Write-Host "The file has been successfully downloaded."
} else {
Write-Host "The file is already available locally."
}