2

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:

7
  • 1
    If 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? May 4, 2019 at 19:33
  • 1
    Get-ChildItem -path "C:\test\A\*.jpg" -file -recurse | select fullname | Get-Random -count 12 May 4, 2019 at 20:13
  • 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.
    – user900431
    May 5, 2019 at 1:57
  • 1
    It'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. May 5, 2019 at 1:59
  • 1
    I'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? May 5, 2019 at 2:22

1 Answer 1

0

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

2
  • Works perfectly, random, no duplicates, rename both jpg and png, super fast... excellent job!
    – user900431
    May 5, 2019 at 4:50
  • Finally finished, used your script here
    – user900431
    Jun 27, 2019 at 7:35

You must log in to answer this question.