Link to home
Start Free TrialLog in
Avatar of BASDRaiders
BASDRaiders

asked on

Copy Random File to Another Direcotory - Batch File

I'm looking for some help to create a batch file or other type of file that can run as a scheduled task on a Windows server.  I want to randomly change wallpaper in my organization each day.  My idea is to put wallpaper images in one directory and then when the script runs, it randomly grabs an image, copies it to another directory, and renames it something like wallpaper1.bmp .  That way I can set a group policy using wallpaper1.bmp, yet still achieving the random wallpaper change.

Can anyone help with this?
Avatar of TheCleaner
TheCleaner
Flag of United States of America image

It would be far simpler to just create each bmp as "Day1.bmp" to "DayN.bmp".  Then the script would be far more simplified and you won't risk grabbing the same image two days in a row.

The script could be set to run each day and grab "Day%day%.bmp" idea...so on November 16th it would grab "day16.bmp" and copy it to the other directory as "wallpaper.bmp" and your GPO would say that wallpaper.bmp is the background image.

Just a thought.
Avatar of Steve Knight
if you do want random then it is easy enough.

the variable %random% gives you a number between 1 and 32768.  We can count how many files in the dir you say, divide the random number by that many files and take the remainder and that will give you a number 1 to n.

so this will give you a number in %num% based on the dir you set in the SET folderG line.

you can then use that with a more or for command to get the right entry:

for /f "skip=%num% delims=" %%f in ('dir /a-d /b "%folder%\*.bmp"') do (
  echo Chosen %%f
  copy "%folder%\~%%f" "\\server\share\wallpaper.bmp"
)

See how you go with that for starters.

Steve
Avatar of BASDRaiders
BASDRaiders

ASKER

Thank you for the replies.  I do want it to be truly random.  I don't mind if it grabs the same image two days in a row if that's how the random number shakes out.

You'll have to excuse my ignorance, but I really don't know where to begin with scripting this.  Let's say I have a folder with 5 bitmaps in it and I want to randomly choose one for the wallpaper.  I need to know exactly how to write the script so that it will randomly choose a bitmap, copy it to another directory and call it wallpaper1.bmp while overwriting/replace whatever may already be in the destination folder.
see my answer above. Hopefully that should give you what you want.  not showing the code i typed for random bit from mobile for some reason, will check it shows alright from pc when i get home shortly.

Steve
Steve....I don't see the "random" part either.

Let's say my files reside in c:\wallpaper and will be copied to c:\gpo\wallpaper.  Can you show me in the script where those directories would be entered?
ok did wonder, just has a funny char. Showing when i look on mobile where i typed it all :-(

Anyway will be back shortly and bus too bumpy to type all those symbols again... Will post when i get back unless someone dives in first.

Steve
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Steve:
May I ask, why:
dir /b /a-d "%folder%*.bmp" ^| find /c /v ""
outputs the quantity of found files?

Thanks
Rene,
because   find /c /v ""   will count all lines not containing nothing => all lines.
  (echo one& echo,& echo three) | find /c /v ""
will result in a three hence.
We could also go and parse for the dir cmd summary output to get the file count.
Steve....that did exactly what I wanted.

Thank you very much!!!
No problem, glad it helped.  Thanks for clarifying Qlemo.

Steve
Oh BTW, we like "500 points" better thsan "125 points" where possible please :-)
Thanks Q :)
OK....I'm new to the whole point thing on here.  I'll keep that in mind with future questions.
Oops, too slow on this one.  I was working on a slightly different version of what Steve proposed though, so at this point I'll file that away.  Ran into a funny quirk of the SET /A with %RANDOM% and wasn't able to work around it, odd.

Code was:

set /A R=%RANDOM% %% %Total%

Open in new window

and at runtime (ECHO OFF) I got:

[c:\ee]set /A R=11113 % 1154
The syntax of the command is incorrect.

Open in new window

Odd...

~bp
Hello dears,

Excellent the script!

I am using it, however I have noticed that it repeats some wallpapers, without having finished the cycle.

Greetings and I hope for your help.

Richard

@echo off
set folder=C:\Scripts\Wallpaper\wallpapers
set destfolder=C:\Scripts\Wallpaper\wallAplicated
REM Get count of the number of .bmp files in folder above then get random number then divide
REM it by the count and take remainder.  No. is 0 to count -1
for /f "delims=" %%C in ('dir /b /a-d "%folder%\*.jpg" ^| find /c /v ""') do set /A num=%random% %% %%C

for /f "delims=" %%F in ('dir /b /a-d "%folder%\*.jpg" ^| more +%num%') do set name=%%F & goto next

:next

echo Wallpaper is now %name%
copy "%folder%\%name%" "%destfolder%\wallpaper.jpg"
That is quite likely.... it just chooses a random number, that could be the same number in a row 10 times or ten different numbers etc.  You could do something maybe where it does

random number from 1-10
knock off each number until all are picked
then start again.

If the files could be renamed one way would be to look for, say, Y_somefile.bmp to choose random from.  When it is shown then rename to N_somefile.bmp so next time not included in the list.

Then if there is no result for Y_*.bmp then rename N_*.bmp to Y_*.bmp

Steve
Bets bet is make yourself a new question and post a link to it here and say what you do want it to do, .e.g. like I said above make sure it always shows 1-10 in random order then rest to another 1-10 or whatever.
Hi Steve,

Thanks for your prompt response!

Honestly I do not know much about programming, my strong is OS administrator and security.

Please, if you can help me and apologize for the inconvenience, equally if you can not understand.

Regards!!

Richard
Ok please make your own new question here with what you want and post a link to it here  will look at it for you.