Sync Any Local Folder with OneDrive

Create symbolic links using the 'mklink' command to synchronize OneDrive and the blog's post folder.

This post was translated from my Chinese blog post with the aid of ChatGpt.

In order to write blog posts on other devices, I initially placed all my blog files on OneDrive. However, I soon realized that this approach led to a significant number of file changes every time I executed ‘hexo cl && hexo g.’ This wasn’t very friendly to the OneDrive server, especially since I was using it without contributing much. So, I decided to keep only the ‘./source/_posts’ folder and manually copy and paste my articles after writing them.

A few days ago, I had a brilliant idea: could I make OneDrive automatically sync this folder? After some searching, I came across the ‘mklink’ command.

Below, there are some explanations about the 'mklink' command. If you don't want to read it, you can skip it by clicking the button below. Skip

Introduction to the ‘mklink’ Command

The command introduction references content from SS64 and Microsoft Docs, and was inspired by the article from 逍遥峡谷. Special thanks for the valuable references.

The ‘mklink’ command is used to create symbolic or hard links for directories or files. It was introduced starting from Windows Vista and is supported on Windows operating systems from Vista onwards.

Comparison

The ‘mklink’ command provides three types of links: ‘Symbolic Link,’ ‘Hard Link,’ and ‘Junction.’ Here’s a comparison of these three types with shortcuts:

TypeLinks FilesLinks FoldersCross-Drive LinksPoints to Nonexistent Target
Shortcut
Hard Link
Junction (Directory Junction / Symbolic Link)
Symbolic Link
  • Symbolic links and junctions are based on NTFS reparse points.
  • Hard links are achieved by having multiple file entries pointing to the same inode, similar to Unix hard links. Even if the original filename is deleted, hard links continue to work, as they directly point to the data on the disk.
  • It is possible (though not advisable) to create two links that point to each other in a loop or create a link that points to itself. Symbolic links may expose security vulnerabilities in applications that do not handle these situations.
  • Both symbolic links and hard links do not support .zip compressed files.

Syntax

1
mklink [[/d] | [/h] | [/j]] <link> <target>
ParametersDescription
/dCreate a directory symbolic link. By default, this command creates a file symbolic link.
/hCreate a hard link instead of a symbolic link.
/jCreate a junction (directory junction).
<link>Specify the name of the symbolic link to create.
<target>Specify the path (relative or absolute) that the new symbolic link will reference.
/?Display help at the command prompt.

If there are spaces in the <link> and <target> paths, they should be enclosed in double quotation marks "".

Elevated Privileges

By default, running the mklink command requires administrator privileges. However, starting from Windows 10 build 14972, if ‘Developer Mode’ is enabled, the command can be run without elevation.

Moving and Selecting

  • Selecting a ‘Symbolic Link’ in File Explorer will select the source file (or directory).
  • Selecting a ‘Junction’ in File Explorer will select the ‘Junction’ itself.
  • Dragging a ‘Symbolic Link’ in File Explorer to a new location will only move the ‘Symbolic Link’ itself.
  • Dragging a ‘Junction’ in File Explorer to a new location will move the source file directory to the new location.

Demonstration

Below are the tests I conducted for each type:

For more detailed explanations, you can refer to the article on 少数派 (in Chinese), which explains it very clearly.

Create test folders link and target and create a test file ./target/target.txt. At this point, the link folder is empty.
File Symbolic Link 1

Execute the command:

1
mklink E:\Test\link\link.txt E:\Test\target\target.txt

Now, the link folder contains link.txt, which has a file type of .symlink. Its icon is the same as a shortcut, but it is a symbolic link. Any changes made to one file will be synchronized to the other. The symbolic link points to the path of the source file, and the actual file is still located in ./target/target.txt.
File Symbolic Link 2

Create test folders and target, and create a test file ./target/target.txt.
Directory Symbolic Link 1

Execute the command:

1
mklink /d E:\Test\link E:\Test\target

Now, a link folder is created with an icon similar to a shortcut, and its contents are the same as the source folder. Any changes made to one folder will be synchronized to the other. The symbolic link points to the path of the source folder, and the actual storage location is still the target source folder. You can see that the link folder has a different path in the address bar and title bar.
Directory Symbolic Link 2
Directory Symbolic Link 3

Create test folders link and target, and create a test file ./target/target.txt. At this point, the link folder is empty.
File Hard Link 1

Execute the command:

1
mklink /h E:\Test\link\link.txt E:\Test\target\target.txt

Now, the link folder contains link.txt, which still has the file type Text Document and the same icon as the source file. A hard link directly points to the file on the disk.
File Hard Link 2

Junction (Directory Junction)

Create test folders and target, and create a test file ./target/target.txt.
Junction 1

Execute the command:

1
mklink /j E:\Test\link E:\Test\target

The result is similar to a directory symbolic link, with the only difference being that the link folder has the same path in the address bar and title bar. However, the actual storage location of the files is still the target source folder.
Junction 2

Creating Symbolic Links for OneDrive

Start by opening OneDrive’s settings and ensure that all items in the “General” and “Notifications” sections are checked.
OneDrive Settings

Next, move the _posts folder from your blog to OneDrive and rename it. It’s essential to move the folder; your blog folder should not contain an _posts folder.

Then, press Win + S, type cmd, and run it “As Administrator.” Enter the mklink command. Please note that the mklink command can only be run in the cmd prompt; it’s not supported in PowerShell.

1
mklink /d D:\Siriusq\source\_posts C:\Users\Sun\OneDrive\Siriusq

In this command, the first path is the location of your blog articles, and the second path is the location in OneDrive where you want to store your blog articles. This might seem counterintuitive, with OneDrive as the source folder, but it’s necessary to avoid OneDrive continuously displaying sync as suspended, which can be confusing.

After executing the command, you’ll receive a prompt:

1
Symbolic link created for D:\Siriusq\source\_posts <<===>> C:\Users\Sun\OneDrive\Siriusq

Link Created

Once the link is created successfully, open your blog folder, and you will see that the address bar shows the current folder’s path while the title bar displays the OneDrive folder’s path. Now, any changes made to either side of the link will be reflected on the other, achieving synchronization.