C盘空间清理方法 - 软链接迁移指南

C盘空间清理方法 - 软链接迁移指南

通过将用户目录下的大型缓存目录迁移到其他盘(如D盘),可以有效释放C盘空间。

原理

Windows 支持两种目录链接方式:

  • Junction (目录连接) - 兼容性好,支持跨分区
  • Symbolic Link (符号链接) - 更灵活,需要管理员权限

本方案使用软链接将缓存目录从 C 盘指向 D 盘,应用程序仍按原路径访问,实际数据存储在 D 盘。

迁移步骤

1. 用户主目录下的目录

以下目录从 C:\Users\Administrator 迁移到 D:\Users\Administrator

目录 说明
.android Android SDK 缓存
.bito Bito AI 工具缓存
.cache 通用缓存目录
.cargo Rust Cargo 包缓存
.gradle Gradle 构建缓存
.m2 Maven 本地仓库
.npm npm 全局缓存
.bun bun 全局缓存
.rustup Rust 工具链

迁移命令:

batch 复制代码
:: 创建 D 盘目标目录结构
mkdir D:\Users\Administrator

:: 1. .android 目录
## 使用powershell执行:
move C:\Users\Administrator\.android D:\Users\Administrator\.android
## 使用cmd执行
mklink /J C:\Users\Administrator\.android D:\Users\Administrator\.android

:: 2. .bito 目录
move C:\Users\Administrator\.bito D:\Users\Administrator\.bito
mklink /J C:\Users\Administrator\.bito D:\Users\Administrator\.bito

:: 3. .cache 目录
move C:\Users\Administrator\.cache D:\Users\Administrator\.cache
mklink /D C:\Users\Administrator\.cache D:\Users\Administrator\.cache

:: 4. .cargo 目录
move C:\Users\Administrator\.cargo D:\Users\Administrator\.cargo
mklink /D C:\Users\Administrator\.cargo D:\Users\Administrator\.cargo

:: 5. .gradle 目录
move C:\Users\Administrator\.gradle D:\Users\Administrator\.gradle
mklink /J C:\Users\Administrator\.gradle D:\Users\Administrator\.gradle

:: 6. .m2 目录
move C:\Users\Administrator\.m2 D:\Users\Administrator\.m2
mklink /D C:\Users\Administrator\.m2 D:\Users\Administrator\.m2

:: 7. .npm 目录
move C:\Users\Administrator\.npm D:\Users\Administrator\.npm
mklink /D C:\Users\Administrator\.npm D:\Users\Administrator\.npm

:: 8. .bun 目录
move C:\Users\Administrator\.bun D:\Users\Administrator\.bun
mklink /D C:\Users\Administrator\.bun D:\Users\Administrator\.bun

:: 9. .rustup 目录
move C:\Users\Administrator\.rustup D:\Users\Administrator\.rustup
mklink /D C:\Users\Administrator\.rustup D:\Users\Administrator\.rustup

2. AppData\Local 目录下的目录

以下目录从 C:\Users\Administrator\AppData\Local 迁移到 D:\Users\Administrator\AppData\Local

目录 说明
Yarn Yarn 包管理器缓存
npm-cache npm 缓存目录
pnpm-cache pnpm 缓存目录

迁移命令:

batch 复制代码
:: 创建 D 盘目标目录结构
mkdir D:\Users\Administrator\AppData\Local

:: 1. Yarn 目录
move C:\Users\Administrator\AppData\Local\Yarn D:\Users\Administrator\AppData\Local\Yarn
mklink /D C:\Users\Administrator\AppData\Local\Yarn D:\Users\Administrator\AppData\Local\Yarn

:: 2. npm-cache 目录
move C:\Users\Administrator\AppData\Local\npm-cache D:\Users\Administrator\AppData\Local\npm-cache
mklink /D C:\Users\Administrator\AppData\Local\npm-cache D:\Users\Administrator\AppData\Local\npm-cache

:: 3. pnpm-cache 目录
move C:\Users\Administrator\AppData\Local\pnpm-cache D:\Users\Administrator\AppData\Local\pnpm-cache
mklink /D C:\Users\Administrator\AppData\Local\pnpm-cache D:\Users\Administrator\AppData\Local\pnpm-cache

一键迁移脚本

powershell脚本: Clean-C-Drive.bat

bash 复制代码
@echo off
:: Clean-C-Drive.bat - 支持在 CMD 和 PowerShell 中运行
:: 管理员权限检查
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo 需要管理员权限!
    echo 请右键此脚本,选择"以管理员身份运行"
    pause
    exit /b 1
)

set targetDrive=D
set targetBase=%targetDrive%:\Users\Administrator

echo 创建目标目录结构...
if not exist "%targetBase%" mkdir "%targetBase%"
if not exist "%targetBase%\AppData" mkdir "%targetBase%\AppData"
if not exist "%targetBase%\AppData\Local" mkdir "%targetBase%\AppData\Local"

echo.
echo ========================================
echo 开始迁移缓存目录...
echo ========================================

:: 用户主目录下的目录
echo 迁移用户主目录...
if exist "%USERPROFILE%\.android" (
    move "%USERPROFILE%\.android" "%targetBase%\.android" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.android' -Target '%targetBase%\.android' -Force" >nul 2>&1
    )
    echo [DONE] .android
)

if exist "%USERPROFILE%\.bito" (
    move "%USERPROFILE%\.bito" "%targetBase%\.bito" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.bito' -Target '%targetBase%\.bito' -Force" >nul 2>&1
    )
    echo [DONE] .bito
)

if exist "%USERPROFILE%\.cache" (
    move "%USERPROFILE%\.cache" "%targetBase%\.cache" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.cache' -Target '%targetBase%\.cache' -Force" >nul 2>&1
    )
    echo [DONE] .cache
)

if exist "%USERPROFILE%\.cargo" (
    move "%USERPROFILE%%.cargo" "%targetBase%\.cargo" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.cargo' -Target '%targetBase%\.cargo' -Force" >nul 2>&1
    )
    echo [DONE] .cargo
)

if exist "%USERPROFILE%\.gradle" (
    move "%USERPROFILE%\.gradle" "%targetBase%\.gradle" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.gradle' -Target '%targetBase%\.gradle' -Force" >nul 2>&1
    )
    echo [DONE] .gradle
)

if exist "%USERPROFILE%\.m2" (
    move "%USERPROFILE%\.m2" "%targetBase%\.m2" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.m2' -Target '%targetBase%\.m2' -Force" >nul 2>&1
    )
    echo [DONE] .m2
)

if exist "%USERPROFILE%\.npm" (
    move "%USERPROFILE%\.npm" "%targetBase%\.npm" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.npm' -Target '%targetBase%\.npm' -Force" >nul 2>&1
    )
    echo [DONE] .npm
)

if exist "%USERPROFILE%\.bun" (
    move "%USERPROFILE%\.bun" "%targetBase%\.bun" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.bun' -Target '%targetBase%\.bun' -Force" >nul 2>&1
    )
    echo [DONE] .bun
)

if exist "%USERPROFILE%\.rustup" (
    move "%USERPROFILE%\.rustup" "%targetBase%\.rustup" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%USERPROFILE%\.rustup' -Target '%targetBase%\.rustup' -Force" >nul 2>&1
    )
    echo [DONE] .rustup
)

echo.
echo 迁移AppData\Local目录...
if exist "%LOCALAPPDATA%\Yarn" (
    move "%LOCALAPPDATA%\Yarn" "%targetBase%\AppData\Local\Yarn" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%LOCALAPPDATA%\Yarn' -Target '%targetBase%\AppData\Local\Yarn' -Force" >nul 2>&1
    )
    echo [DONE] Yarn
)

if exist "%LOCALAPPDATA%\npm-cache" (
    move "%LOCALAPPDATA%\npm-cache" "%targetBase%\AppData\Local\npm-cache" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%LOCALAPPDATA%\npm-cache' -Target '%targetBase%\AppData\Local\npm-cache' -Force" >nul 2>&1
    )
    echo [DONE] npm-cache
)

if exist "%LOCALAPPDATA%\pnpm-cache" (
    move "%LOCALAPPDATA%\pnpm-cache" "%targetBase%\AppData\Local\pnpm-cache" >nul 2>&1
    if %errorLevel% equ 0 (
        powershell -Command "New-Item -ItemType SymbolicLink -Path '%LOCALAPPDATA%\pnpm-cache' -Target '%targetBase%\AppData\Local\pnpm-cache' -Force" >nul 2>&1
    )
    echo [DONE] pnpm-cache
)

echo.
echo ========================================
echo 迁移完成!C盘空间已释放
echo ========================================
pause
相关推荐
晚风吹人醒.4 个月前
Apache Web网站服务:安装、编译、配置,全流程讲解
服务器·apache·软连接·网页·硬链接
Jay 173 年前
BUU [HCTF 2018]Hideandseek
数据库·python·web安全·网络安全·软连接·mac地址·session伪造
小小傻瓜牙3 年前
centos7做软连接及注意事项
linux·centos·软连接·创建软连接