[英]Copy first N files from source directory to “serialized” destination directory using powershell
Either Powershell or batch script will work. Powershell或批处理脚本都将起作用。 I want to distribute every N number of files from directory A to directory B1, B2, B3, etc. 我想将每N个文件从目录A分发到目录B1,B2,B3等。
Example: C:\\a (has 9 .jpg files) file1.jpg file2.jpg ... file9.jpg 示例:C:\\ a(具有9个.jpg文件)file1.jpg file2.jpg ... file9.jpg
then c:\\b1, C:\\b2, C:\\b3 should have 3 files each. 那么c:\\ b1,C:\\ b2,C:\\ b3应该每个都有3个文件。 it should create directories C:\\b* as well. 它也应该创建目录C:\\ b *。
So far I came up with this code, works fine but copies ALL the files from directory A to directory B: 到目前为止,我想出了以下代码,可以正常工作,但是将所有文件从目录A复制到目录B:
$sourceFolder = "C:\a"
$destinationFolder = "C:\b"
$maxItems = 9
Get-Childitem $sourceFolder\*.jpg | ForEach-Object {Select-Object -First $maxItems | Robocopy $sourceFolder $destinationFolder /E /MOV}
This also works, will calculate how many new folders should be created. 这也可以,将计算应创建多少个新文件夹。
$excludealreadycopieditems = @()
$sourcefolder = "C:\a"
$destinationFolder = "C:\b"
$maxitemsinfolder = 3
#Calculate how many folders should be created:
$folderstocreate = [math]::Ceiling((get-childitem $sourcefolder\*.jpg).count / $maxitemsinfolder)
#For loop for the proces
for ($i = 1; $i -lt $folderstocreate + 1; $i++)
{
#Create the new folders:
New-Item -ItemType directory $destinationFolder$i
#Copy the items (if moving in stead of copy use Move-Item)
get-childitem $sourcefolder\*.jpg -Exclude $excludealreadycopieditems | sort-object name | select -First $maxitemsinfolder | Copy-Item -Destination $destinationFolder$i ;
#Exclude the already copied items:
$excludealreadycopieditems = $excludealreadycopieditems + (get-childitem $destinationFolder$i\*.jpg | select -ExpandProperty name)
}
Something like this should do: 这样的事情应该做:
$cnt = 0
$i = 1
Get-ChildItem "$sourceFolder\*.jpg" | % {
if ($script:cnt -ge $maxItems) {
$script:i++
$script:cnt = 0
}
$dst = "$destinationFolder$script:i"
if (-not (Test-Path -LiteralPath $dst)) {
New-Item $dst -Type Directory | Out-Null
}
Copy-Item $_.FullName $dst
$script:cnt++
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.