How to copy 12 random jpgs to a folder and rename by batch or powershell fast (Windows 10 Spotlight Related)

1

In the search for a final solution to randomizing the login screens as part of an answer for Keep Windows 10 lock screen spotlight pictures but turn off all texts hints/balloons, I am seeking help, I am still very new to the world of batch, though think it will be a useful solution when finished.

Now extending this question to power shell as per discussion with @KeithMiller

Windows batch file or powershell file that can:

  • Copy 12 random jpg images and rename to new location
  • No duplicates
  • Actually be random
  • To run fast in just a few seconds even if choosing from 700 to 1500 files.
  • To search for .jpg
  • .Jpg file names to be unknown, so any files can be selected or added to the folder.
  • Rename the 12 images to new location half as .jpg half as .png: img100.jpg, img101.jpg, img102.jpg, img103.jpg, img104.jpg, img105.jpg and also img100.png, img101.png, img102.png, img103.png, img104.png, img105.png

Note: Windows 10 will still uses the jpgs as png even though they are renamed. With this solution there will be up to 12 random background user lock screen, and also, as far as I have tested this allows for the 5 cache images under lock screen settings.


Powershell Copies 12 images to new location randomly (.PS1)
$d = gci "C:\Test\A\*.jpg" | resolve-path  |  get-random -count 12
Copy-Item $d  -destination C:\Test\B

from Stack Exchange, already works with no duplicates, now only need to find out to seperate out the paths in order to rename. Possible code found that may help:

foreach ($file in $sourcefiles)
    {
    $newdir = $file.DirectoryName.Replace( $sourcepath, $destination )

     If (-not (test-path $newdir))
     {
        md $newdir
     }

      Copy-Item -Path $file.FullName -Destination $newdir


     }

from Microsoft Technet


Batch code to count files, then produce 12 random numbers.
@for /f %%G in ('2^>nul dir "C:\test\A\*.jpg" /a-d/b/-o/-p/s^|find /v /c ""') do set N=%%G

@echo Total files: %N%

@echo off & setlocal EnableDelayedExpansion

for /L %%a in (1 1 12) do (
        call:rand 1 %N%
        echo !RAND_NUM!
)

goto:EOF

REM The script ends at the above goto:EOF.  The following are functions.

REM rand()
REM Input: %1 is min, %2 is max.
REM Output: RAND_NUM is set to a random number from min through max.
:rand
SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
goto:EOF

Counting JPG and Giving Random Number

  • This does not account for duplicates, though I thought it would be a good way to quickly count images and come up with a number to select.

this is based on post here and here


The prior solution that I have was made for this was thanks to @DavidPostill

This solution works for about 150 images, then unfortunately runs for to long. I made an error, apologies, and did not know that windows 10 slideshow randomizes the images on its own.

I have really tried my best to research the topic, though coding is still beyond me, so any help would be greatly appreciated. I have included reading/research below...


Reading and Research:

Under A Tree

Posted 2019-05-04T08:50:10.420

Reputation: 849

1If you're starting out & just learning, you should be doing this in PowerShell. Is there a reason you're not or would you like to convert? – Keith Miller – 2019-05-04T19:33:06.393

1Get-ChildItem -path "C:\test\A\*.jpg" -file -recurse | select fullname | Get-Random -count 12 – Keith Miller – 2019-05-04T20:13:24.647

True @KeithMiller ! I have just updated the question with some powershell code. I have used power-shell on windows 7 and found it clunky and slow, though good for heavy scripts, I just tested it and its much faster on windows 10 to initiate. Also random works much with out duplicates now! The other reason was I already had the working script for takeown and icacls working in batch, though I work them out in powershell also if this works out. – Under A Tree – 2019-05-05T01:57:11.343

1It's very smooth on Windows 10. Takeown and icacls are both executeable that can be called from PowerShell --- so conversion of your existing scripts should be relatively easy and a good learning exercise. – Keith Miller – 2019-05-05T01:59:53.053

1I'm confused by your copy/rename portion of your request. Is a single source file being copied twice --- once as .jpg and once as .png? Or are you just arbitrarily keeping half of the results as .jpg and half as .png? And, just curious, what is that point of this part? – Keith Miller – 2019-05-05T02:22:21.590

@KeithMiller you got it correct, strange request, I am trying to get windows 10 to add 5 cache images to lock-screen thumbnails, and windows dosn't mind if a jpg is named as a png. If it dosn't have png and jpg in the C:/windows/web/screen then I can only get 3 or 4 thumbnails to show under (Run -> ms-settings:lockscreen) Picture, this enables a work around. – Under A Tree – 2019-05-05T04:48:54.180

@KeithMiller This script I will still use as my base, though I found that the png and jpg, while Windows 10 uses them kind of randomly, it did not help increase the cache above the 3 images I was getting, I must not of cleared the cache in my tests. So I guess if more images is wanted for different uses, I would need another script to add them to the program data SID lock screen folders. Great effort though, will still be used in my answer. – Under A Tree – 2019-05-10T03:41:00.243

Answers

1

This should be close to what you want:

$SelectCount = 12
$SourcePath  = "C:\test\A\*.jpg"
$DestPath    = 'C:\test\Renamed'

If (!(test-path $DestPath)) {md $DestPath | out-null}

$files = Get-ChildItem -path $SourcePath -file -recurse | Get-Random -count $SelectCount
for ($i = 0; $i -lt $files.count; $i += 2) {
   copy-item $files[$i] -destination ('{0}\file{1:000}.jpg' -f $DestPath, ($i/2+100)) -whatif
   copy-item $files[$i+1] -destination ('{0}\file{1:000}.png' -f $DestPath, ($i/2+100)) -whatif
}

Remove the -whatif parameters from the copy-item statements if you want them to actually execute.

Keith

Keith Miller

Posted 2019-05-04T08:50:10.420

Reputation: 1 789

Works perfectly, random, no duplicates, rename both jpg and png, super fast... excellent job! – Under A Tree – 2019-05-05T04:50:45.527

Finally finished, used your script here

– Under A Tree – 2019-06-27T07:35:23.430