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
}
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
- 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
- How can I copy 10 files to a new directory and rename them with a pattern?
- How can I rewrite the first 3 letters of a set of file names with random characters?
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:
- Computing.net - How to copy 6 random files from 100+ folders?
- Batch copy 50 random mp3 files from one folder to another?
- Copy a random file to another folder
- How to copy random image to another folder use batch file?
- Batch Script: How to Set the Number of Files to Copy or Move
- Choose Random text file in batch
- Randomly choose 15 files and copy them to output folder
- Pick random files and move them to another folder by batch
- Copy one random file to another folder recursively
- Select one random file and copy to another folder
- Pick random files from a folder and move them to another folder
- batch code not working when put inside a loop
- How long does a batch file take to execute?
- Batch file to copy files from one folder to another
- Counting number of files in folder and storing in a variable
- Batch file that counts and outputs results to a text file
- Count files in a folder and subfolders from the command line
- Copy a random file to another folder
- Batch - Randomly copy 1 of 10 files from location A to B?
- How do I loop a batch script only a certain amount of times?
- Windows batch file to copy and keep duplicates
- Is it possible to "batch" copy files in specific order?
- Opening random file from folder AND subfolders with batch script
- Batch file to copy files from one folder to another folder
- Make multiple copies of a single file, with each copy being assigned a unique filename by batch
- How do I batch copy files sequentially from one folder to another?
- Fastest method of copying files
- Random generator in the batch
- How to use random in BATCH script?
- Short Command Line Tips (Batch Files)
- WikiBooks Windows Batch Scripting
- What is the difference between % and %% in a cmd file?
- Conditional IF EXIST Statement for multiple filenames in BATCH file
- ss64 Copy
- ss64 Xcopy
- ss64 Random Numbers
- ss64 Start
- ss64 For
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
1
Get-ChildItem -path "C:\test\A\*.jpg" -file -recurse | select fullname | Get-Random -count 12
– Keith Miller – 2019-05-04T20:13:24.647True @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