0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Cドライブ肥大化が止まらない

Last updated at Posted at 2025-02-02

お金もないCドライブの空き容量もない人
せめてDドライブが空いてるなら
緊急手術です(自己責任です)
だいたい移動しちゃいけないファイルが肥大化している
でも移動できるのもある、ならシンボリックリンク(ググれ)で
移動してしまえ(暴論)
以下に参考を張っておきますが、くれぐれも自己責任で使ってね♡

cToD_Drive_Move_And_Simboliclink.ps1
# 注意: このスクリプトは管理者権限で実行してください。

# 移動元と移動先のパス設定
$source = "C:\Users\%Username%\AppData\" # 例としてTempフォルダを移動
$destination = "D:\Users\%Username%\AppData\"

# 移動元ディレクトリの存在チェック
if (-not (Test-Path $source)) {
    Write-Host "エラー: 移動元ディレクトリが存在しません。処理を中止します: $source"
    exit
}

if (-not (Test-Path $destination)) {
    Write-Host "エラー: 移動先ディレクトリが存在しません。処理を中止します: $source"
    exit
}

# 移動元ディレクトリ内のフォルダを取得
$folders = Get-ChildItem -Path $source -Directory

foreach ($folder in $folders) {
    $subFolderName = $folder.Name
    $sourceFolderPath = $folder.FullName
    $destinationFolderPath = Join-Path $destination $subFolderName

    # 移動先ディレクトリがすでに存在する場合、スキップ
    if (Test-Path $destinationFolderPath) {
        Write-Host "移動先ディレクトリが存在します。処理をスキップします: $destinationFolderPath"
        continue
    }

    try {
        # フォルダを移動
        Write-Host "移動中: $sourceFolderPath$destinationFolderPath に移動します..."
        Move-Item -Path $sourceFolderPath -Destination $destinationFolderPath -ErrorAction Stop

        # 元の場所にシンボリックリンクを作成
        Write-Host "シンボリックリンク作成中: $sourceFolderPath -> $destinationFolderPath"
        New-Item -Path $sourceFolderPath -ItemType SymbolicLink -Target $destinationFolderPath -ErrorAction Stop
    } catch {
        Write-Host "フォルダ '$sourceFolderPath' の移動/リンク作成に失敗しました: $_"
        # エラー情報をログファイルに記録する処理を追加
    }
}

※意味を理解して分かる人だけがコードを動かしていいと聖書にあった

Q.壊れた!
A.知らん

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?