Home > Software engineering >  Select one random image file from a folder, rename it, and copy it, to another folder
Select one random image file from a folder, rename it, and copy it, to another folder

Time:10-31

I'm attempting to create a "Image Changer" that can move an image from a folder on my local computer, rename it "wallpaper.jpg," and move it into a profile folder in Firefox, essentially changing it at random.

I need to write a batch file for Windows that will select a random image file from a folder, rename it, and then copy it to another folder. The old image file would then have to be overwritten each time to replace it in the new folder location. I'd still need a copy of that file to keep it in its original location.

FYI, it doesn't need to be a batch file, but that is what I am used to. Also it would be nice if could loop doing this in the background until I kill the command.

Thanks in advance for your help...

I tried running this

@echo off
setlocal EnableDelayedExpansion
cd \particular\folder
set n=0
for %%f in (*.*) do (
   set /A n =1
   set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768 1"
copy "!file[%rand%]!" \different\folder

Which would copy the image to the other folder, but I was struggling to figure out how to rename it. Really trying to wrap my head around using batch scripts.

CodePudding user response:

Renaming is just the action of moving a file to a file with a different name. That's why there's no rename command in GNU/Linux for example. In your case \different\folder\wallpaper.jpg should do the trick i think.

Also be sure to test if Firefox supports you to change the wallpaper like that, or if it has to reload for it to change

  • Related