0

I'm trying to set up scheduled tasks for all the machines in our network. So they don't all have the same start time I edit the XML file to use a random time. The changing the XML works fine, but as soon as I put a For loop around it to use servers.txt I get an error message: "+10 was unexpected at this time."

@echo off

for /F "tokens=*" %%i in (servers.txt) do (

set file=original.xml
set insertline=9
SET /a uur=(%RANDOM%*6/32768)+10
SET /a minuut=(%RANDOM%*50/32768)+10
set output=output.XLM

(for /f "tokens=1* delims=[]" %%a in ('find /n /v "##" ^< "%file%"') do (
    if "%%~a"=="%insertline%" (
    echo ^<StartBoundary^>2018-12-17T%uur%:%minuut%:43.9766025^<^/StartBoundary^>
    REM ECHO.%%b
    ) ELSE (
    echo.%%b
    )
    )) > %output%

SchTasks /Create /S \\%%i /RU username /RP password /XML output.XLM /TN task_name

)
Noto
  • 1
  • Oh by the way, the reason I use the +10 is that random numbers below 10 only give a single digit. I suspect that could give an issue with the time format. So it should give a number between 10 and 59. uur = hour minuut = minute – Noto Dec 17 '18 at 11:57
  • 2
    try with [delayed expansion](https://ss64.com/nt/delayedexpansion.html) – npocmaka Dec 17 '18 at 11:57
  • tried delayed expansion, same error :( – Noto Dec 17 '18 at 12:50
  • Delayed expansion is really needed here, but it's not the reason for `+10 was unexpected at this time.` – Stephan Dec 17 '18 at 12:52
  • [possible duplicate](https://stackoverflow.com/questions/53673340/modifying-the-path-variable-from-batch/53685829#53685829) (along with [delayed expansion](https://stackoverflow.com/questions/30282784/variables-in-batch-not-behaving-as-expected/30284028#30284028)) – Stephan Dec 17 '18 at 12:55

3 Answers3

1

The closing parentheses in your set commands are closing the for loop too early.

Here are two possible ways to overcome that:

SET /a "uur=(%RANDOM%*6/32768)+10"
SET /a minuut=(%RANDOM%*50/32768^)+10

The first one secures the ) by enclosing the whole assignment in quotes, the second escapes the ) (with a caret ^). (you should consistently use ONE of that possibilities throughout your whole script - I'd recommend the first one (with quotes))

Another failure: %RANDOM% will give you the same value each time within a command block (use delayed expansion to overcome that.

Later in the script, you have the same issue with the file and insertline variables. So also change from %file% and %insertline% to !file! and !insertline!.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • @echo off setlocal EnableDelayedExpansion for /F "tokens=*" %%i in (servers.txt) do ( set file=original.xml set insertline=9 SET /a "uur=(!RANDOM!*6/32768)+10" SET /a "minuut=(!RANDOM!*50/32768)+10" set output=output.XLM echo !uur!:!minuut! )' That works, but when I also use the 2nd loop: The syntax of the command is incorrect. – Noto Dec 17 '18 at 13:46
  • please edit your new code into your question; A comment doesn't have format capabilities and leaves your code nearly unreadable. – Stephan Dec 17 '18 at 13:51
0

I think overwriting output.xml is intended to create a new task from the template with different (random) StartBoundary.

But all the constants (re-defined inside the code block) should be moved outside to the front.

As the inner for has only the if/else command, one set of parentheses is superfluous.

Proper indenting helps to keep overview of the code.

:: Q:\Test\2018\12\17\SO_53814701.cmd
@echo off & Setlocal EnableDelayedExpansion

set file=original.xml
set insertline=9
set output=output.XLM

for /F "tokens=*" %%i in (servers.txt) do (
    SET /a "uur=(!RANDOM!*6/32768)+10"
    SET /a "minuut=(!RANDOM!*50/32768)+10"
    (   for /f "tokens=1* delims=[]" %%a in (
            'find /n /v "##" ^< "%file%"'
        ) do if "%%~a"=="%insertline%" (
            echo ^<StartBoundary^>2018-12-17T%uur%:%minuut%:43.9766025^<^/StartBoundary^>
            REM ECHO.%%b
        ) ELSE (
            echo.%%b
        )
    ) > %output%
    SchTasks /Create /S \\%%i /RU username /RP password /XML output.XLM /TN task_name
)
0

Well... I changed something and now it works...

@echo off
setlocal EnableDelayedExpansion

for /F "tokens=*" %%i in (servers.txt) do (

set file=original.xlm
set insertline=9
SET /a "uur=(!RANDOM!*6/32768^)+10"
SET /a "minuut=(!RANDOM!*50/32768^)+10"
set output=output.XLM
echo !uur!:!minuut!
(for /f "tokens=1* delims=[]" %%a in ('find /n /v "##" ^< "!file!"') do (
if "%%~a"=="!insertline!" (
echo ^<StartBoundary^>2018-12-17T!uur!:!minuut!:43.9766025^<^/StartBoundary^>
REM ECHO.%%b
) ELSE (
echo.%%b
)
)) > !output!

SchTasks /Create /S \\%%i /RU username /RP password /XML output.XLM /TN task_name

)
)
)

I think it was changing all the % for ! within the FOR

Noto
  • 1