简体   繁体   English

使用PowerShell将前N个文件从源目录复制到“序列化”目标目录

[英]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.

相关问题 如何将文件从源目录移动到具有文件名第一部分名称的目标目录? - How to move files from a source directory to a destination directory with name of first part of file name? 在PowerShell中递归地将一组文件从一个目录复制到另一个目录 - Recursively copy a set of files from one directory to another in PowerShell 使用 robocopy 复制文件时出现错误 5 (0x00000005) 创建目标目录 - Getting ERROR 5 (0x00000005) Creating Destination Directory while using robocopy to copy files 使用Powershell从特定目录中删除文件的代码 - Code to remove the files from a particular directory using powershell 如何使用命令行从来源复制文件的日期与目的地不同的文件 - How do I copy files from the source with different dates than the destination using the Command Line 将文件移动到目标目录的子文件夹中 - Move files into a subfolder of a destination directory 使用源文件夹作为名称将文件从源目录树复制到新位置 - Copy file from source directory tree to new location using source folder as name 将文件从一个目录复制并粘贴到当前工作目录中 - Copy and paste files from one directory into current working directory 如何将文件数组(3,500)从目录 A 复制到目录 B - How to copy array of files (3,500) from directory A to Directory B 将文件从 FTP 服务器复制到本地目录? - Copy files from FTP server to local directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM