0

I need a script to run in a window to monitor my folder. When I put a jpg file in this folder, then the script running in the window should

  • open that jpg file with photoshop droplet (.exe),
  • wait for a while and
  • run to check .jpg files again.

The script I have now works, but I need to modify it.

I want the window to open jpg file with a random droplet that I created: it may be 5 droplets let's say I have 5 droplets that is 1.exe, 2.exe, 3.exe, 4.exe, 5.exe instead of just 123.exe droplet (this script). How do I do this?

@echo off
:loop
if exist "C:\Users\Administrator\Desktop\converted\*.jpg" (
for %%a in ("C:\Users\Administrator\Desktop\converted\*.jpg") do (
start "" /w "C:\Users\Administrator\Desktop\123.exe" "%%a"
ping -n 20 localhost >nul
del "%%a"
)
)
ping -n 60 localhost >nul
goto :loop

Edited on 27 May 2015 After Changing script I try move your script before "for loop" it's work but It's keep the same value in output, not random to every loop, please see my script

@echo off

:loop
set /a n=%random% %%5+1  (  <-- I move your line to this)
if exist "C:\Users\Administrator\Desktop\converted\*.jpg" (
for %%b in ("C:\Users\Administrator\Desktop\converted\*.jpg") do (
start "" /w "C:\Users\Administrator\Desktop\%n%.exe" "%%b"
ping -n 20 localhost >nul
del "%%b"
)
)
ping -n 60 localhost >nul
goto :loop
0

2 Answers 2

2

You can use %random% to perform this. set /a n=%random%%6+1 will generate a random number between 1 and 5.

%random% % 6 generates a number between 0 and 5 so %random% % 6 + 1 generates a number between 1 and 6.

Doc: https://stackoverflow.com/questions/5777400/how-to-use-random-in-batch-script

EDIT : I can't comment yet so here is how to implement this:

@echo off
:loop
if exist "C:\Users\Administrator\Desktop\converted\*.jpg" (
for %%a in ("C:\Users\Administrator\Desktop\converted\*.jpg") do (
set /a n=%random%%6+1
start "" /w "C:\Users\Administrator\Desktop\%n%.exe" "%%a"
ping -n 20 localhost >nul
del "%%a"
)
)
ping -n 60 localhost >nul
goto :loop

In fact, set /a n=... define a new variable "n". You can use it by typing %n%.

8
  • Doesn't work for me, seem like no value in the command, window keep alert me "Windows cannot find 'C:\Users\Administrator\Desktop\.exe'. Make Sure you typed the name correctly, and then try again.
    – iamway
    May 26, 2015 at 16:31
  • What if you echo the value just before the start command? Like this : echo %n%
    – Dano
    May 26, 2015 at 16:40
  • noting happen after echo ? why ,anything wrong with my computer?
    – iamway
    May 26, 2015 at 17:37
  • My bad. You have to write 2 percent signs: set /a n=%random% %% 6 + 1
    – Dano
    May 26, 2015 at 19:50
  • @Dano: Correct your batch file too and double the % sign.
    – Karan
    May 26, 2015 at 19:58
0

I found the solution, thanks dano to let me think how to do this after I done searching about how to use it in for loop, I finally got the answer that I have to use ! instead of % and use "SETLOCAL ENABLEDELAYEDEXPANSION" too

Many Thanks

@echo off

:loop
SETLOCAL ENABLEDELAYEDEXPANSION
if exist "C:\Users\Administrator\Desktop\converted\*.jpg" (
for %%b in ("C:\Users\Administrator\Desktop\converted\*.jpg") do (
set /a n=!random! %%5+1 
start "" /w "C:\Users\Administrator\Desktop\!n!.exe" "%%b"
ping -n 20 localhost >nul
del "%%b"
)
)
ping -n 60 localhost >nul
goto :loop

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .