2
  1. Copy first ten jpg files in folder in alphanumerical order to new folder with out prior knowledge of jpg file names.
  2. Give each ten files in new location specific names like 'e100.jpg' - 'e110.jpg'.
  3. Powershell or batch script solution to run in windows enviroment.

Current research across related stack exchange posts:

  1. SO - Copy first N files from source directory to “serialized” destination directory using powershell

  2. SF - Copy first 10 files from a folder and subfolders

  3. SO - Powershell to pick random files from a folder and move them to another folder

  4. SU - How to batch copy rename files

  5. SU - How to optionally copy and rename a file in Windows?

  6. SU - PowerShell Script to Rename - Copy - Move

  7. Bulk renaming of files in PowerShell with sequential numeric suffixes

If I were to guess at a solution, based on the research as my coding skill are not very good, I think one of the solutions would look something like this:

Powershell

$excludealreadycopieditems = @()
$sourcefolder = "C:\SYS1"
$destinationFolder = "C:\SYS2"
$maxitems = 10
#Calculate how many folders should be created:
$folderstocreate = [math]::Ceiling((get-childitem $sourcefolder\*.jpg).count / $maxitems)
#Copy the items (if moving in stead of copy use Move-Item)
get-childitem $sourcefolder\*.jpg -Exclude $excludealreadycopieditems | sort-object name | select -First $maxitems | Copy-Item -Destination $destinationFolder$i ;
#Exclude the already copied items:
$excludealreadycopieditems = $excludealreadycopieditems + (get-childitem $destinationFolder$i\*.jpg | select -ExpandProperty name)
     }
ls *jpg | Foreach {$i=1} {Rename-Item _ -NewName ("$($.100){$:110#} .jpg" -f $i++) -whatif}

CMD

@ECHO OFF
SET SrcCount=0
SET SrcMax=10
FOR %F IN (C:\SYS1\*.jpg) DO IF !SrcCount! LSS %SrcMax% (
      SET /A SrcCount += 1
      ECHO !SrcCount! COPY %F C:\SYS2
      COPY %F C:\temp\output
      )
ren *.jpg e100.* e103.* e104.* e105.* e106.* e107.* e108.* e109.* e110.*
2
  • First ten is either e101.jpg..e110.jpg or e100.jpg..e109.jpg, otherwise I count eleven. Your cmd sample uses syntax !SrcCount! but doesn't enable delayed expansion with setlocal.
    – LotPings
    Mar 12, 2019 at 12:10
  • @LotsPing I made a mistake, yes the script I was looking for also required to start count from 0, which is why my sample was wrong (with 11), though don't want to change the question as it has already been answered well.
    – user900431
    Mar 13, 2019 at 0:10

2 Answers 2

2

How can I copy 10 files to a new directory and rename them with a pattern?

Use the following batch file (test.cmd):

@echo off
setlocal EnableDelayedExpansion
set "source_dir=f:\test\jpg"
set "target_dir=f:\test\target"
for /f "tokens=*" %%f in ('dir /b %source_dir%\*.jpg') do (
  set /a "count+=1"
  set /a "target_count=!count!+100"
  copy "%source_dir%\%%f" "!target_dir!\e!target_count!.jpg" > nul
  if !count! EQU 10 goto :done
  )
rem finished
:done
endlocal

Notes:

  • Change source_dir and target_dir as appropriate

Example:

> dir jpg
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test\jpg

12/03/2019  11:39    <DIR>          .
12/03/2019  11:39    <DIR>          ..
12/03/2019  11:34             4,429 Test_image (01).jpg
12/03/2019  11:34             4,429 Test_image (02).jpg
12/03/2019  11:34             4,429 Test_image (03).jpg
12/03/2019  11:34             4,429 Test_image (04).jpg
12/03/2019  11:34             4,429 Test_image (05).jpg
12/03/2019  11:34             4,429 Test_image (06).jpg
12/03/2019  11:34             4,429 Test_image (07).jpg
12/03/2019  11:34             4,429 Test_image (08).jpg
12/03/2019  11:34             4,429 Test_image (09).jpg
12/03/2019  11:34             4,429 Test_image (10).jpg
12/03/2019  11:34             4,429 Test_image (11).jpg
12/03/2019  11:34             4,429 Test_image (12).jpg
12/03/2019  11:34             4,429 Test_image (13).jpg
12/03/2019  11:34             4,429 Test_image (14).jpg
12/03/2019  11:34             4,429 Test_image (15).jpg
12/03/2019  11:34             4,429 Test_image (16).jpg
              16 File(s)         70,864 bytes
               2 Dir(s)  1,005,493,501,952 bytes free

> test
> dir target
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test\target

12/03/2019  12:07    <DIR>          .
12/03/2019  12:07    <DIR>          ..
12/03/2019  11:34             4,429 e101.jpg
12/03/2019  11:34             4,429 e102.jpg
12/03/2019  11:34             4,429 e103.jpg
12/03/2019  11:34             4,429 e104.jpg
12/03/2019  11:34             4,429 e105.jpg
12/03/2019  11:34             4,429 e106.jpg
12/03/2019  11:34             4,429 e107.jpg
12/03/2019  11:34             4,429 e108.jpg
12/03/2019  11:34             4,429 e109.jpg
12/03/2019  11:34             4,429 e110.jpg
              10 File(s)         44,290 bytes
               2 Dir(s)  1,005,493,379,072 bytes free

Further Reading

0
0

So you did research a fair amount, but without evaluating your results it's not much worth.

In NTFS formatted drives an alphanumeric like sort is implied.

PowerShell:

Get-ChildItem -Path C:\Sys1 -Filter *.jpg -File |
  Sort-Object Name | Select-Object -First 10 |
    ForEach-Object -Begin {$i=100} -Process {
      Copy-Item -Path $_ -Destination (Join-Path "C:\Sys2" ("e{0:D3}{1}" -f $i++,$_.Extension)) -WhatIf
    }

If the output looks ok, remove the -WhatIf

0

You must log in to answer this question.