Picturesフォルダー内のすべてのファイルをカウントしています
Get-ChildItem C:\pictures -force | Group-Object extension | Sort-Object count -descending | ft count,name -auto
次に、すべての MTS ファイル (ビデオ) を別のフォルダーにコピーしています。
Get-ChildItem C:\pictures -force -recurse -include *.MTS | Copy-Item -Destination c:\video
これはうまく機能します。しかし、年ごとにフォルダを作成しc:\video
、対応するファイルをコピーするにはどうすればよいですか?
アップデート:
Shay はこれを達成するのを手伝ってくれました。現在、次のコードがあります。
# Create a folder for each year and move the specified files to the corresponding folders
Get-ChildItem $fromFolder -Force |
Group-Object {$_.CreationTime.Year} | Foreach-Object {
# Testing to see if the folder exist
if(!(Test-Path $toFolder\$($_.Name))) {
$folder = New-Item -Path "$toFolder\$($_.Name)" Itemtype Directory -Force
echo "Created $toFolder\$($_.Name)"
} else {
echo "Folder $toFolder\$($_.Name) exist"
}
# Testing to see if the file exist in the target directory
if(!(Test-Path $_.group)) {
$_.group | Copy-Item -Destination $folder.FullName
echo "Copyied $_ to $folder"
} else {
echo "File exist"
}
}
フォルダーを正常にテストしますが、すべてのTest-Path
ファイルをスキップします。どういうわけかループを壊していますか?それともパイプラインを台無しにしますか?