有多個檔案夾有兩個擴展名.zip和.txt檔案。但是,如果任何檔案夾只有一個.zip或只有一個.txt檔案,則移動命令將不起作用。
這是我發現的東西,但是我該如何為兩個擴展做呢?
Move-Item -LiteralPath (Get-ChildItem -File -Path N:\Download\*\*.txt).DirectoryName `
-Destination N:\Zip -Force -WhatIf
uj5u.com熱心網友回復:
可能有更有效的方法來執行此操作,但我使用 Where-Object 進行過濾。
# Customizable variables
$Source = "N:\Download"
$Destination = "N:\Zip"
$Extension1 = "*.txt"
$Extension2 = "*.zip"
# script
$Folders = Get-ChildItem -Path $Source -Recurse -Directory | Select-Object -ExpandProperty FullName
foreach ($Folder in $Folders) {
if ((Get-ChildItem -Path $Folder | Where-Object { $_.Name -like $Extension1}) -and (Get-ChildItem -Path $folder | Where-Object { $_.Name -like $Extension2})) {
Move-Item -Path "$Folder\$Extension1" -Destination $Destination -Force -Verbose
Move-Item -Path "$Folder\$Extension2" -Destination $Destination -Force -Verbose
}
}
使用這些變數,您可以輕松地使用其他源檔案夾和目標檔案夾或擴展自定義腳本。您可以在看到它的運行后省略 -Verbose 輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/534711.html
標籤:电源外壳
