Wednesday 31 July 2019

windows - Move a copy of unknown named images and rename - Powershell or Batch Script




  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.*

Answer



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
.
12/03/2019 11:39
..
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
.
12/03/2019 12:07
..
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



No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...