PowerShell zip ファイルを指定したフォルダに解凍するスクリプト例

Windows 標準で .NET オブジェクトなどを利用できる CLI (Command Line Interface) PowerShell を用いて、指定した .zip ファイルを指定したフォルダに解凍するスクリプト例実行結果を紹介します。
解凍先フォルダの存在の確認と、存在しない場合にフォルダを作成するコマンドレットについても紹介します。

PowerShell zip ファイルを指定したフォルダに解凍するスクリプト例2

※ Windows PowerShell PSVersion 5.1.19041.6093 を使用します。

解凍する zip ファイル

以下の記事で紹介した Release Godot VCS Git Plugin v3.1.1 · godotengine/godot-git-plugin の godot-git-plugin-v3.1.1.zip リンクからダウンロードした .zip ファイルをサンプルとして解凍します。

zip を配置したフォルダで PowerShell を起動

zip ファイルの解凍のテストに用いるフォルダを作成して、 zip ファイルを置きます。
PowerShell を起動して、そのフォルダに移動します。
※ Windows 10 の場合は、フォルダをエクスプローラで開いて、アドレスバーに powershell⏎ と入力すると、そのフォルダをカレントディレクトリにして PowerShell を起動できます。

PowerShell zip ファイルを指定したフォルダに解凍するスクリプト例1

PowerShell スクリプトの実行

以下のスクリプトをコピーして、 PowerShell で実行します。


# zip ファイルパスと解凍先フォルダパスの指定
$zipFilePath = ".\godot-git-plugin-v3.1.1.zip"
$extractFolderPath = ".\addons"

# 解凍先フォルダがなければ作成
if (-Not (Test-Path -Path $extractFolderPath )) {
    New-Item -ItemType Directory -Path $extractFolderPath | Out-Null
}

# ZIP ファイルを解凍
Expand-Archive -Path $zipFilePath -DestinationPath $extractFolderPath -Force

各コマンドレットについては、以下の小さな章を参照してください。

解凍先のフォルダの存在を確認

解凍先のフォルダの存在を Test-Path コマンドレットで確認します。

if (-Not (Test-Path -Path $extractFolderPath )) {

Determines whether all elements of a path exist.

パスのすべての要素が存在するかどうかを判断します。

-Path
Specifies a path to be tested. Wildcard characters are permitted. If the path includes spaces, enclose it in quotation marks.

テストするパスを指定します。ワイルドカード文字を使用できます。パスにスペースが含まれる場合は、引用符で囲みます。

Test-Path (Microsoft.PowerShell.Management) – PowerShell | Microsoft Learn と Google 翻訳

存在しない場合はフォルダを作成

存在しない場合は、 New-Item コマンドレットでフォルダを作成します。

New-Item -ItemType Directory -Path $extractFolderPath | Out-Null

Creates a new item.

新しいアイテムを作成します。

-ItemType
Specifies the provider-specified type of the new item. The available values of this parameter depend on the current provider you are using.

新しい項目のプロバイダー指定のタイプを指定します。このパラメーターで使用できる値は、使用している現在のプロバイダーによって異なります。

If your location is in a FileSystem drive, the following values are allowed:

File
Directory
SymbolicLink
Junction
HardLink

-Path
Specifies the path of the location of the new item.

新しい項目の場所のパスを指定します。

New-Item (Microsoft.PowerShell.Management) – PowerShell | Microsoft Learn と Google 翻訳

パイプラインの次の工程で、 Out-Null を指定することで、フォルダ作成結果のメッセージの標準出力を行わないようしています。

Hides the output instead of sending it down the pipeline or displaying it.

出力をパイプラインに送信したり表示したりするのではなく、非表示にします。

Out-Null (Microsoft.PowerShell.Core) – PowerShell | Microsoft Learn と Google 翻訳

Zip ファイルの解凍

指定した zip ファイルExpand-Archive コマンドレットで、指定したフォルダに解凍します。
-Force パラメータをつけると、上書きを許可します。

Expand-Archive -Path $zipFilePath -DestinationPath $extractFolderPath -Force

Extracts files from a specified archive (zipped) file.

指定したアーカイブ (zip) ファイルからファイルを抽出します。

-Path
Specifies the path to the archive file.

アーカイブ ファイルへのパスを指定します。

-DestinationPath
By default, Expand-Archive creates a folder in the current location that’s the same name as the ZIP file. The parameter allows you to specify the path to a different folder. The target folder is created if it doesn’t exist.

デフォルトでは、Expand-Archive は現在の場所に ZIP ファイルと同じ名前のフォルダーを作成します。このパラメーターを使用すると、別のフォルダーへのパスを指定できます。ターゲットフォルダーが存在しない場合は作成されます。

-Force
Use this parameter to overwrite existing files. By default, Expand-Archive doesn’t overwrite.

既存のファイルを上書きするには、このパラメータを使用します。デフォルトでは、Expand-Archive は上書きしません。

Expand-Archive (Microsoft.PowerShell.Archive) – PowerShell | Microsoft Learn と Google 翻訳

実行結果

前述のスクリプトをコピーして PowerShell貼り付けEnter キー実行すると、指定した解凍先フォルダ addons\ が存在しない場合、フォルダが作成され、その中に zip が解凍されました。

PowerShell zip ファイルを指定したフォルダに解凍するスクリプト例2
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

新しいクロスプラットフォームの PowerShell をお試しください https://aka.ms/pscore6

PS G:\Dev\PowerShell\Extract-ZipFile\1> # zip ファイルパスと解凍先フォルダパスの指定
>> $zipFilePath = ".\godot-git-plugin-v3.1.1.zip"
>> $extractFolderPath = ".\addons"
>>
>> # 解凍先フォルダがなければ作成
>> if (-Not (Test-Path -Path $extractFolderPath )) {
>>     New-Item -ItemType Directory -Path $extractFolderPath | Out-Null
>> }
>>
>> # ZIP ファイルを解凍
>> Expand-Archive -Path $zipFilePath -DestinationPath $extractFolderPath -Force
PS G:\Dev\PowerShell\Extract-ZipFile\1>

指定したフォルダに、 zip が解凍されていることを確認できました。

PowerShell zip ファイルを指定したフォルダに解凍するスクリプト例3

まとめ

Windows 標準で .NET オブジェクトなどを利用できる CLI (Command Line Interface) PowerShell を用いて、指定した .zip ファイルを指定したフォルダに解凍するスクリプト例実行結果を紹介します。
解凍先フォルダの存在の確認と、存在しない場合にフォルダを作成するコマンドレットについても紹介します。

参照サイト Thank You!

記事一覧 → Compota-Soft-Press

コメント

Ads Blocker Image Powered by Code Help Pro

お願い - Ads Blocker Detected

このサイトは広告を掲載して運営しています。

ポップアップを閉じて閲覧できますが、よろしければ

このサイト内の広告を非表示にする拡張機能をオフにしていただけませんか?

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

タイトルとURLをコピーしました