转移Nuget目录

以下是一段PS脚本,将C盘中的NUGET转移到D盘,并写入环境变量 请首先输入以下命令,检查变量是否和我预定一致, `dotnet nuget locals all --list` 如果一致,则直接用下面的脚本即可完成导入

 


# 定义新的缓存路径
$newHttpCachePath = "D:\LocalCache\nuget\v3-cache"
$newPackagesPath = "D:\LocalCache\nuget\packages"
$newPluginsCachePath = "D:\LocalCache\nuget\plugins-cache"
$newScratchPath = "D:\LocalCache\nuget\NuGetScratch"

# 定义旧的缓存路径
$oldHttpCachePath = "C:\Users\Admin\AppData\Local\NuGet\v3-cache"
$oldPackagesPath = "C:\Users\Admin\.nuget\packages"
$oldScratchPath = "C:\Users\Admin\AppData\Local\Temp\NuGetScratch"
$oldPluginsCachePath = "C:\Users\Admin\AppData\Local\NuGet\plugins-cache"

# 创建新的文件夹
New-Item -ItemType Directory -Force -Path $newHttpCachePath
New-Item -ItemType Directory -Force -Path $newPackagesPath
New-Item -ItemType Directory -Force -Path $newPluginsCachePath
New-Item -ItemType Directory -Force -Path $newScratchPath

# 移动文件并显示进度
function Move-WithProgress {
    param (
        [string]$source,
        [string]$destination
    )
    $items = Get-ChildItem -Path $source -Recurse
    $totalItems = $items.Count
    $currentItem = 0

    foreach ($item in $items) {
        $relativePath = $item.FullName.Substring($source.Length + 1)
        $targetPath = Join-Path -Path $destination -ChildPath $relativePath
        $targetDir = Split-Path -Path $targetPath -Parent

        if (-not (Test-Path -Path $targetDir)) {
            New-Item -ItemType Directory -Force -Path $targetDir | Out-Null
        }

        Move-Item -Path $item.FullName -Destination $targetPath -Force
        $currentItem++
        $progress = [math]::Round(($currentItem / $totalItems) * 100)
        Write-Progress -Activity "Moving files" -Status "$progress% complete" -PercentComplete $progress
    }
}

Move-WithProgress -source $oldHttpCachePath -destination $newHttpCachePath
Move-WithProgress -source $oldPackagesPath -destination $newPackagesPath
Move-WithProgress -source $oldScratchPath -destination $newScratchPath
Move-WithProgress -source $oldPluginsCachePath -destination $newPluginsCachePath

# 设置永久用户环境变量
[System.Environment]::SetEnvironmentVariable("NUGET_HTTP_CACHE_PATH", $newHttpCachePath, [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable("NUGET_PACKAGES", $newPackagesPath, [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable("NUGET_PLUGINS_CACHE_PATH", $newPluginsCachePath, [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable("NUGET_SCRATCH", $newScratchPath, [System.EnvironmentVariableTarget]::User)

# 刷新当前会话的环境变量
$env:NUGET_HTTP_CACHE_PATH = $newHttpCachePath
$env:NUGET_PACKAGES = $newPackagesPath
$env:NUGET_PLUGINS_CACHE_PATH = $newPluginsCachePath
$env:NUGET_SCRATCH = $newScratchPath

Write-Output "环境变量和文件夹已成功设置和移动。"
posted @ 2024-07-17 11:13  摇头怕怕  阅读(172)  评论(0)    收藏  举报