Thursday, 29 November 2018

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


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:


Answer



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


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...