[英]How can I run a powershell script in a for loop?
我的任務是為我的家人整理家庭錄像和照片。 我的腳本運行良好,但我必須針對每個目錄手動運行它們。 如何針對僅處於 1 深度的所有子項運行我的腳本?
我目前的粗略腳本如下:
$current_dir = Split-Path -Path $pwd -Leaf
$new_path = "H:\sorted\$current_dir\pictures"
Get-ChildItem -R . -Include ('*.jpg', '*.jpeg', '*.png') | Move-Item -Destination (New-Item -Force -Path "$new_path" -Type Directory)
$current_dir = Split-Path -Path $pwd -Leaf
$new_path = "H:\sorted\$current_dir\videos"
Get-ChildItem -R . -Include ('*.mp4', '*.mkv', '*.3pg','*.flv', '*.mov', '*.gif') | Move-Item -Destination (New-Item -Force -Path "$new_path" -Type Directory)
示例文件樹
E:.
├───April9383
├───April98765
│ └───carson
├───Cathy
├───Cathy(1)
├───Charlie
│ ├───Photos
│ └───Videos
├───daleville
我希望最終結構看起來像 Charlie 在示例中所做的那樣。 我怎樣才能用 E: 的循環運行這兩個?
我努力了
$sub_dir = $(Get-ChildItem . -Depth 1)
foreach ($sub in $sub_dir) {
picture-sort.ps1
}
但這采用了存儲所有示例文件的文件夾的名稱,而不是“April9383”等的名稱
解決了:
我最終接受了@Santiago 的回復,但進行了一些編輯,因為它並沒有完全按照我的需要工作。
我拿了這個並用它運行以結束
$base = "E:\sorted"
$current_dir = $pwd
# get the folders 1 level deep and enumerate
Get-ChildItem . -Depth 0 -Directory | ForEach-Object {
# join the destination with this folder's Name
$path = Join-Path $base -ChildPath $_.Name
$pic_path = "$path\pictures"
$vid_path = "$path\videos"
#source dirs
$stripped_dest_path = Split-Path -Path $path -Leaf
$src_path = Join-Path $current_dir $stripped_dest_path
#Print statement
Write-Output "Copying pictures from $src_path to $pic_path"
# get and move all pictures
Get-ChildItem -R $src_path -Include ('*.jpg', '*.jpeg', '*.png') |
Copy-Item -ErrorAction SilentlyContinue -Destination (New-Item -Force -Path "$pic_path" -Type Directory)
#Print statement
Write-Output "Copying videos from $src_path to $vid_path"
# get and move all videos
Get-ChildItem -R $src_path -Include ('*.mp4', '*.mkv', '*.3pg','*.flv', '*.mov', '*.gif', '*.avi') |
Copy-Item -ErrorAction SilentlyContinue -Destination (New-Item -Force -Path "$vid_path" -Type Directory)
Write-Output "END OF LOOP"
}
你幾乎就在那里,只需要將所有邏輯放在循環中:
$base = "H:\sorted"
# get the folders 1 level deep and enumerate
Get-ChildItem . -Depth 1 -Directory | ForEach-Object {
# join the destination with this folder's Name
$path = Join-Path $base -ChildPath $_.Name
# get and move all pictures
$_ | Get-ChildItem -Recurse -Include '*.jpg', '*.jpeg', '*.png' |
Move-Item -Destination (New-Item -Path (Join-Path $path -ChildPath 'Pictures') -Type Directory -Force)
# get and move all videos
$_ | Get-ChildItem -Recurse -Include '*.mp4', '*.mkv', '*.3pg','*.flv', '*.mov', '*.gif' |
Move-Item -Destination (New-Item -Path (Join-Path $path -ChildPath 'videos') -Type Directory -Force)
}
我最終接受了@Santiago 的回復,但進行了一些編輯,因為它並沒有完全按照我的需要工作。
我拿了這個並用它運行以結束
$base = "E:\sorted"
$current_dir = $pwd
# get the folders 1 level deep and enumerate
Get-ChildItem . -Depth 0 -Directory | ForEach-Object {
# join the destination with this folder's Name
$path = Join-Path $base -ChildPath $_.Name
$pic_path = "$path\pictures"
$vid_path = "$path\videos"
#source dirs
$stripped_dest_path = Split-Path -Path $path -Leaf
$src_path = Join-Path $current_dir $stripped_dest_path
#Print statement
Write-Output "Copying pictures from $src_path to $pic_path"
# get and move all pictures
Get-ChildItem -R $src_path -Include ('*.jpg', '*.jpeg', '*.png') |
Copy-Item -ErrorAction SilentlyContinue -Destination (New-Item -Force -Path "$pic_path" -Type Directory)
#Print statement
Write-Output "Copying videos from $src_path to $vid_path"
# get and move all videos
Get-ChildItem -R $src_path -Include ('*.mp4', '*.mkv', '*.3pg','*.flv', '*.mov', '*.gif', '*.avi') |
Copy-Item -ErrorAction SilentlyContinue -Destination (New-Item -Force -Path "$vid_path" -Type Directory)
Write-Output "END OF LOOP"
}
這應該工作:
$PictureFiles = @('*.jpg', '*.png')
$VideoFiles = @('*.mkv', '*.mp4')
$MyFiles = Get-ChildItem H:\sorted -Recurse -File -Include @($PictureFiles + $VideoFiles) -Depth 1
foreach ($File in $MyFiles) {
#Videos
if ($File.Extension -in ($VideoFiles -replace '\*')) {
mkdir ($File.DirectoryName + "\Videos\") -Force
Move-Item -Path $File.FullName -Destination ($File.DirectoryName + "\Videos\" + $File.Name) -Force
}
#Pictures
if ($File.Extension -in ($PictureFiles -replace '\*')) {
mkdir ($File.DirectoryName + "\Pictures\") -Force
Move-Item -Path $File.FullName -Destination ($File.DirectoryName + "\Pictures\" + $File.Name) -Force
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.