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)

Report •

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
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

xcopy "%file%" "C:\another folder\new_image_name.jpg"

Report •

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 •

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

Good luck

Report •

Related Solutions

Ask Question