LoginSignup
1
1

More than 5 years have passed since last update.

ショートカットのリンク先ファイルを一括保存するバッチファイル (PowerShell)

Last updated at Posted at 2016-07-10

目的

共有ディレクトリ上の Excel 方眼紙をみんなで(ロックしあって|ブック共有して)編集しながら作業してる場合、
バージョン管理よりもとりあえず短期的にバックアップとりたい場面が多かったため、こんなスクリプト頻用してました。

※バージョン管理もバックアップもどちらも必要で、別目的・別タイミングで実施という方針

今必要な関連ファイルのショートカットをたくさんぶちこんだ作業用ディレクトリを作って
起動短縮&バックアップ迅速化という運用です。

確認した環境

  • Windows 7
  • PSVersion 2.0

コード

ショーカット一括保存するバッチファイル.bat
@powershell -NoProfile -ExecutionPolicy Unrestricted "$s=[scriptblock]::create((gc \"%~f0\"|?{$_.readcount -gt 1})-join\"`n\");&$s" %*&goto:eof

#ただし実体は PowerShell

try {
  $fromDir = 'バックアップしたいショートカットが詰まったディレクトリ'
  $toDir   = "${fromDir}-backup" + (date -format 'yyyyMMdd-HHmmss')   # $fromDir 末尾がフォルダ区切り文字でない想定

  $shell = New-Object -ComObject WScript.Shell
  $files = $(dir "${fromDir}/*.lnk" | %{$shell.CreateShortcut($_).targetPath })

  if ($null -eq $files) {
    echo "$fromDir is empty."
  } else {
    mkdir -force "${toDir}" > $null
    $files | %{
      if (test-path -LiteralPath "$_") {
        copy -Recurse -LiteralPath "$_" "$toDir"
      } else {
        Write-Warning "'$_' not found."
      }
    }
    echo 'copy finished.'
  }
} finally {
  Read-Host 'pause...  '
}

注意

バッチファイルから PowerShell を呼び出す方法 by @cd01

を参考にしています。
コレを見て「こわわ…」って思わない方は使わないようにしましょう。

別用途

ショートカットではなくて一括で特定ディレクトリ以下を保存する場合とかは $files 辺りを書き換えればいけます

1
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
1
1