PowerShell パスの連結とフォルダの移動と削除のスクリプト例

Windows 標準で .NET オブジェクトなどを利用できる CLI (Command Line Interface) PowerShell を用いて、指定したフォルダの移動と削除を行うスクリプト例実行結果を紹介します。
上位のフォルダのパスと、その下位のパスを連結するコマンドレットについても紹介します。

PowerShell パスの連結とフォルダの移動と削除のスクリプト例2

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

移動するフォルダと削除するフォルダ

PowerShell でフォルダを移動・削除する例として

.\addons\godot-git-plugin-v3.1.1\addons\godot-git-plugin\ フォルダを
.\addons\godot-git-plugin\ に移動します。

その後、
.\addons\godot-git-plugin-v3.1.1\ フォルダを削除します。

PowerShell パスの連結とフォルダの移動と削除のスクリプト例1
G:\Dev\PowerShell\Extract-ZipFile\2>tree
フォルダー パスの一覧:  ボリューム ボリューム
ボリューム シリアル番号は 9691-AE5E です
G:.
└─addons
    └─godot-git-plugin-v3.1.1
        └─addons
            └─godot-git-plugin
                ├─linux
                ├─macos
                └─win64

スクリプト例

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

# 移動先と移動元のフォルダパス
$basePath = ".\addons"
$moveSrcPath = Join-Path $basePath "godot-git-plugin-v3.1.1\addons\godot-git-plugin"
$moveDstPath = Join-Path $basePath "godot-git-plugin"

# フォルダの移動
Move-Item -Path $moveSrcPath -Destination $moveDstPath -Force

# 削除するフォルダパス
$removeFolderPath = Join-Path $basePath "godot-git-plugin-v3.1.1"

# フォルダの削除
Remove-Item -Path $removeFolderPath -Recurse -Force

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

共通のフォルダのパスと、その下位のパスの連結

Join-Path コマンドレットで上位のパスと下位のパスを連結した結果を返します。
例では $moveSrcPath には、”.\addons\godot-git-plugin-v3.1.1\addons\godot-git-plugin” が格納されます。

$basePath = ".\addons"
$moveSrcPath = Join-Path $basePath "godot-git-plugin-v3.1.1\addons\godot-git-plugin"

Combines a path and a child path into a single path.

パスと子パスを 1 つのパスに結合します。

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

フォルダの移動

Move-Item コマンドレットで、指定したパスのアイテム(例ではフォルダ)を、指定したパスに移動します。
-Force により上書きする場合も暗黙的に実行します。

Move-Item -Path $moveSrcPath -Destination $moveDstPath -Force

Moves an item from one location to another.

アイテムをある場所から別の場所に移動します。

-Path
Specifies the path to the current location of the items. The default is the current directory. Wildcard characters are permitted.

項目の現在の場所へのパスを指定します。デフォルトは現在のディレクトリです。ワイルドカード文字を使用できます。

-Destination
Specifies the path to the location where the items are being moved. The default is the current directory. Wildcards aren’t permitted.
To rename the item being moved, specify a new name in the value of the Destination parameter.

項目を移動する場所へのパスを指定します。デフォルトは現在のディレクトリです。ワイルドカードは許可されません。
移動する項目の名前を変更するには、Destination パラメーターの値に新しい名前を指定します。

-Force
Forces the command to run without asking for user confirmation. Implementation varies from provider to provider. For more information, see about_Providers.

ユーザーに確認を求めずにコマンドを強制的に実行します。実装はプロバイダーごとに異なります。詳細については、「about_Providers」を参照してください。

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

フォルダの削除

Remove-Item コマンドレットで指定したパスのアイテム(例ではフォルダ)を -Force パラメータを指定して強制的に削除します。
-Recurse パラメータにより、下位のフォルダも再帰的に削除されますが、削除されないケースが既知の問題としてあります。

Remove-Item -Path $removeFolderPath -Recurse -Force

Deletes the specified items.

指定した項目を削除します。

-Path
Specifies a path of the items being removed. Wildcard characters are permitted.

削除する項目のパスを指定します。ワイルドカード文字を使用できます。

-Recurse
Indicates that this cmdlet deletes the items in the specified locations and in all child items of the locations.
The Recurse parameter might not delete all subfolders or all child items. This is a known issue.

このコマンドレットが、指定された場所にある項目とその場所のすべての子項目を削除することを示します。
Recurse パラメーターでは、すべてのサブフォルダーまたはすべての子項目が削除されない場合があります。これは既知の問題です。

-Force
Forces the cmdlet to remove items that can’t otherwise be changed, such as hidden or read-only files or read-only aliases or variables. The cmdlet can’t remove constant aliases or variables. Implementation varies from provider to provider. For more information, see about_Providers. Even using the Force parameter, the cmdlet can’t override security restrictions.

隠しファイルや読み取り専用ファイル、読み取り専用のエイリアスや変数など、他の方法では変更できない項目をコマンドレットで強制的に削除します。コマンドレットは定数のエイリアスまたは変数を削除できません。実装はプロバイダーごとに異なります。詳細については、「about_Providers」を参照してください。 Force パラメーターを使用しても、コマンドレットはセキュリティ制限をオーバーライドできません。

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

実行結果

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

PowerShell パスの連結とフォルダの移動と削除のスクリプト例2

スクリプトを実行するまでのフォルダ構成は以下です。

G:\Dev\PowerShell\Extract-ZipFile\2>tree
フォルダー パスの一覧:  ボリューム ボリューム
ボリューム シリアル番号は 9691-AE5E です
G:.
└─addons
    └─godot-git-plugin-v3.1.1
        └─addons
            └─godot-git-plugin
                ├─linux
                ├─macos
                └─win64

コピーしたスクリプトを PowerShell アプリに貼り付けEnter キー実行すると、 addons\ 直下に godot-git-plugin フォルダが移動して、もともとあった godot-git-plugin-v3.1.1 フォルダは削除されました。
godot-git-plugin フォルダの下位のフォルダ・ファイルも移動しました。

PowerShell パスの連結とフォルダの移動と削除のスクリプト例3
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

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

PS G:\Dev\PowerShell\Extract-ZipFile\2> # 移動先と移動元のフォルダパス
>> $basePath = ".\addons"
>> $moveSrcPath = Join-Path $basePath "godot-git-plugin-v3.1.1\addons\godot-git-plugin"
>> $moveDstPath = Join-Path $basePath "godot-git-plugin"
>>
>> # フォルダの移動
>> Move-Item -Path $moveSrcPath -Destination $moveDstPath -Force
>>
>> # 削除するフォルダパス
>> $removeFolderPath = Join-Path $basePath "godot-git-plugin-v3.1.1"
>>
>> # フォルダの削除
>> Remove-Item -Path $removeFolderPath -Recurse -Force
PS G:\Dev\PowerShell\Extract-ZipFile\2>

以下はスクリプトを実行した後のフォルダ構成です。

G:\Dev\PowerShell\Extract-ZipFile\2>tree
フォルダー パスの一覧:  ボリューム ボリューム
ボリューム シリアル番号は 9691-AE5E です
G:.
└─addons
    └─godot-git-plugin
        ├─linux
        ├─macos
        └─win64

関連記事

今回、例で用いたフォルダの元となった godot-git-plugin-v3.1.1 については、以下の記事を参照してください。

その zip ファイルを PowerShell で解凍するスクリプト例は以下の記事を参照してください。

まとめ

今回は、Windows 標準で .NET オブジェクトなどを利用できる CLI (Command Line Interface) PowerShell を用いて、指定したフォルダの移動と削除を行うスクリプト例実行結果を紹介しました。
上位のフォルダのパスと、その下位のパスを連結するコマンドレットについても紹介しました。

参照サイト 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をコピーしました