見出し画像

(AI問答)Q.powershell zip 展開 ソース例 上書きはしないで別名保存とする

A1.ChatGPT (OpenAI)

閉じてしまったので無し

A2.Gemini (Google Bard)

PowerShell で ZIP ファイルを展開し、上書きせずに別名保存するソース例

PowerShell で ZIP ファイルを展開し、既存のファイルを上書きせずに別名で保存するサンプルコードを以下に示します。

PowerShell

# ZIP ファイルのパス
$zipPath = "path/to/your/archive.zip"

# 展開先のパス
$destinationPath = "path/to/destination/folder"

# ZIP ファイルを展開
try {
  # 展開先のフォルダが存在しない場合は作成
  if (!(Test-Path $destinationPath)) {
    New-Item -ItemType Directory -Path $destinationPath | Out-Null
  }

  # ZIP ファイルを展開(上書きしない)
  Expand-Archive -Path $zipPath -DestinationPath $destinationPath -NoClobber

  Write-Host "ZIP ファイル '$zipPath' を '$destinationPath' に展開しました。"
}
catch {
  Write-Error "ZIP ファイルの展開中にエラーが発生しました: $_"
}

解説:

  1. $zipPath: 展開する ZIP ファイルのパスを指定します。

  2. $destinationPath: 展開先のフォルダパスを指定します。

  3. Test-Path $destinationPath: 展開先のフォルダが存在するかどうかを確認します。

  4. New-Item -ItemType Directory -Path $destinationPath: 展開先のフォルダが存在しない場合に作成します。

  5. Expand-Archive -Path $zipPath -DestinationPath $destinationPath -NoClobber: ZIP ファイルを展開します。

    • -NoClobber オプションを指定することで、既存のファイルを上書きせずに展開します。

  6. try...catch: エラー処理を行います。ZIP ファイルの展開中にエラーが発生した場合、エラーメッセージを表示します。

ポイント:

  • -NoClobber オプションを使用することで、既存のファイルが誤って上書きされるのを防ぎます。

  • 展開先のフォルダが存在しない場合は、事前に作成しておく必要があります。

  • エラー処理を組み込むことで、問題発生時に適切な対応を取ることができます。

注意:

  • ZIP ファイルの展開には時間がかかる場合があります。

  • 展開先のフォルダに十分な空き容量があることを確認してください。

このサンプルコードを参考に、ご自身の環境に合わせて ZIP ファイルのパスや展開先のパスを変更して実行してください。

A3. Copilot (BingAI)

Sure thing! Here's a PowerShell script that extracts the contents of a ZIP file without overwriting existing files by saving them with a different name:

# Define the path to the ZIP file and the destination folder
$zipPath = "C:\path\to\your.zip"
$destinationPath = "C:\path\to\extract\"

# Load the .NET assembly for handling ZIP files
Add-Type -AssemblyName System.IO.Compression.FileSystem

# Extract the ZIP file
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $destinationPath)

# Get a list of all files in the destination folder
$files = Get-ChildItem -Path $destinationPath -File

foreach ($file in $files) {
    # Check if the file already exists
    if (Test-Path ($destinationPath + $file.Name)) {
        # Generate a new name for the file
        $newFileName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name) + "_new" + $file.Extension
        $newFilePath = Join-Path $destinationPath $newFileName

        # Rename the file
        Rename-Item -Path $file.FullName -NewName $newFileName
    }
}

Make sure to replace `C:\path\to\your.zip` and `C:\path\to\extract` with the actual paths to your ZIP file and destination folder. This script will extract the contents of the ZIP file and rename any files that would be overwritten by appending "_new" to their names.

Let me know if you need any further assistance!

A4.Grok (Xai)

PowerShellでZIPファイルを展開し、既存のファイルを上書きせずに別名で保存する例を以下に示します。この例では、展開するファイルが既に存在する場合、新しいファイル名に「_new」を追加します。

powershell

# ZIPファイルのパス
$zipFilePath = "C:\path\to\your\file.zip"
# 展開先のディレクトリ
$extractPath = "C:\path\to\extract"

# ZIPファイルを展開
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [IO.Compression.ZipFile]::OpenRead($zipFilePath)

foreach ($entry in $zip.Entries) {
    $destinationPath = Join-Path $extractPath $entry.FullName
    $directoryPath = Split-Path $destinationPath -Parent

    # ディレクトリが存在しない場合作成
    if (-Not (Test-Path -Path $directoryPath)) {
        New-Item -ItemType Directory -Path $directoryPath | Out-Null
    }

    if (Test-Path $destinationPath) {
        # ファイルが既に存在する場合、ファイル名に "_new" を追加
        $newFileName = (Get-Item $destinationPath).BaseName + "_new" + (Get-Item $destinationPath).Extension
        $destinationPath = Join-Path $directoryPath $newFileName
    }

    # ファイルを展開
    [IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $destinationPath, $false)
}

$zip.Dispose()

このスクリプトでは以下のことが行われます:

  • ZIPファイル内の各エントリについて、展開先のパスを決定します。

  • 既に同名のファイルが存在する場合、ファイル名に _new を追加して上書きを避けます。

  • 必要なディレクトリが存在しない場合、それらを作成します。

注意点として、Add-Type -AssemblyName System.IO.Compression.FileSystem はPowerShell 5.1以降で必要です。古いバージョンのPowerShellを使用している場合は、Shell.Application COMオブジェクトを使用する別の方法を検討する必要があります。

A5.NOTE+ AI (Geminiらしい)

今回は無し

結論

#コマンドプロンプト#poweshell での移動はゴミ箱に入らないので素人の方はこれをそのままコピペしないように気を付けてくださいね。

いいなと思ったら応援しよう!

エレキテル之助
開発のための諸経費(機材、Appleの開発者、サーバー運用)に利用します。 https://t.co/vgyoSFLk59 サポートによりnoteの運営にも貢献できます。

この記事が参加している募集