# Random file select (not rename)

August 10, 2010 at 13:40:43
Specs: Windows 7 64 bit
 I'm trying to create a batch file that will randomly select an image from a folder, then copy and rename it to a different folder on the same computer.I did some searching here and elsewhere online and found the solution to the copy and rename part:xcopy c:\image.jpg "C:\another folder\new_image_name.jpg"However, I'm stumped with how to randomly select an image without changing the image file names to add a random number. Is it possible to simply select a random image file without first adding random numbers to the file names?Thanks in advance.

See More: Random file select (not rename)

#1
August 11, 2010 at 04:07:33
 Try this, change the paths\filenames to suit. Untested...The selection spread depends on how many files are available. the script is set for a max of 50 files, if more or less are available change the random number generator line to suit.@echo off cls setlocal enabledelayedexpansion set /a rand=%random%%%50+1 pushd path\to\folder\containing\.jpg files\ set nbr=1 for /f "delims=*" %%1 in ('dir /a-d /b *.jpg') do ( set file=%%1 set /a nbr+=1 if !nbr! gtr !rand! goto getout ) :getout xcopy "%file%" "C:\another folder\new_image_name.jpg" 

Report •

#2
August 12, 2010 at 08:51:08
 Yep, with a couple of minor changes e.g., using a /y switch to overwrite w/o prompting, it works...thanks Wahine! I'm just starting to learn bat scripting in windows, and this was a start.

Report •

#3
August 12, 2010 at 14:14:54
 Thank you for coming back to report your success.Good luck

Report •
Related Solutions