PowerShell小僧:階層構造のフォルダに配置されているファイル [コンピューター]
1.階層構造のフォルダの子フォルダにあるファイルを、親フォルダに移動する。
2.子フォルダを削除する。
というスクリプトを作りました。
コードは以下のとおり。
$MyFilePath1 = "D:\aaa\bbb" # 親フォルダ
Set-Location -LiteralPath $MyFilePath1 # 親フォルダに移動
Get-ChildItem -Recurse -File|ForEach-Object{
$MyFilePath2 = ($_|Split-Path -Parent) # ファイルがあるフォルダ
if($MyFilePath1 -ne $MyFilePath2){ # 親フォルダとファイルがあるフォルダが不一致→ファイルは子フォルダにある
wrire-host $_
Move-Item $_ $MyFilePath1 # ファイルが子フォルダにある場合、カレント(親)フォルダにファイルを移動
}
}
Get-ChildItem -Directory|ForEach-Object{
write-host $_
Remove-Item -Recurse $_ # 子フォルダを削除
}
2.子フォルダを削除する。
というスクリプトを作りました。
コードは以下のとおり。
$MyFilePath1 = "D:\aaa\bbb" # 親フォルダ
Set-Location -LiteralPath $MyFilePath1 # 親フォルダに移動
Get-ChildItem -Recurse -File|ForEach-Object{
$MyFilePath2 = ($_|Split-Path -Parent) # ファイルがあるフォルダ
if($MyFilePath1 -ne $MyFilePath2){ # 親フォルダとファイルがあるフォルダが不一致→ファイルは子フォルダにある
wrire-host $_
Move-Item $_ $MyFilePath1 # ファイルが子フォルダにある場合、カレント(親)フォルダにファイルを移動
}
}
Get-ChildItem -Directory|ForEach-Object{
write-host $_
Remove-Item -Recurse $_ # 子フォルダを削除
}
タグ:powershell
ヤバイぜ! ありがとうございます(^_0)ノ
by cheese999 (2022-08-11 03:19)