0

Either Powershell or batch script will work. I want to distribute every N number of files from directory A to directory B1, B2, B3, etc.

Example: C:\a (has 9 .jpg files) file1.jpg file2.jpg ... file9.jpg

then c:\b1, C:\b2, C:\b3 should have 3 files each. it should create directories C:\b* as well.

So far I came up with this code, works fine but copies ALL the files from directory A to directory B:

$sourceFolder = "C:\a"
$destinationFolder = "C:\b"
$maxItems = 9
Get-Childitem  $sourceFolder\*.jpg | ForEach-Object {Select-Object -First $maxItems | Robocopy $sourceFolder $destinationFolder /E /MOV}

2 Answers 2

1

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)
     }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! Works like a charm... it does create 3 different directories as well.
0

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++
}

6 Comments

Thanks but it does not create 3 different directories. Though copies all files in 1 directory... which is what the program I wrote does eventually anyway.
Thanks but it still does not work. I had to change to $sourceFolder\*.jpg(without double quotes) and to New-Item $destinationFolder$i (with $i at the end) and it did went through a little bit. but i had 8 files in c:\a and it create c:\b with 4 files and C:\b\1 with another 4 files directory.
@user5084064 The code in my answer worked fine when I tested it. Please provide evidence.
1) it creates empty directory b 2) it also creates b1 b2... bn files if there are n files in c:\a directory New-Item : Item with specified name C:\b already exists. At C:\ansgar.ps1:12 char:13 + New-Item <<<< $destinationFolder -Type Directory | Out-Null + CategoryInfo : ResourceExists: (C:\b:String) [New-Item], IOException + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
@user5084064 The New-Item statement in my sample code doesn't use the variable $destinationFolder. Please edit your question and show the actual code you're using.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.