0

I need help getting this batch script to work. I am trying to iterate with an IF and GOTO function and it works fine for me until the point where I insert the following code inside the IF.

This is the erro that is presenting Error: ) was unexpected at this time.

CODE I NEED INSIDE THE IF

echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,("%date2%")) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"

set "date=%yyyy%%mm%%dd%"

echo %yyyy%
echo %mm%
echo %dd%

echo Export dia %date%

CODE WITHOUT THE IF WHICH WORKS

Set  desde=20220420
Set  hasta=20220422

:DesdeFormat
set "YYYY=%desde:~0,4%"
set "MM=%desde:~4,2%"
set "DD=%desde:~6,2%"
set "from=%yyyy%-%mm%-%dd%"
echo From= %from%

:HastaFormat
set "YYYY=%hasta:~0,4%"
set "MM=%hasta:~4,2%"
set "DD=%hasta:~6,2%"
set "to=%yyyy%-%mm%-%dd%"
echo To= %to%

echo Fecha de [%from%] hasta [%to%]

echo Wscript.Echo #%to%# - #%from%# >tmp.vbs
for /f %%a in ('cscript /nologo tmp.vbs') do set "total=%%a"
del tmp.vbs

echo ***The Total number of days from %from% until %to% is [%total%]***
echo ****INICIANDO ITERACION****
 
set /a i=1
echo iteraciones totales:%total% Y el inicio es: %i%

:inicio
IF %i% LEQ %total% (

echo THE CODE/SCRIPT ABOVE SHOULD BE HERE

set /a "i=%i%+1"
::iteracion
echo iteracion: %i%
echo Total: %total%

) else (GOTO :END)
GOTO :inicio

:END 
echo EL FIN

pause
Alex
  • 1
  • 1
  • 1
    You will need [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Oct 25 '22 at 19:02

1 Answers1

0

A few things wrong with your code...

First, date is a magic variable. It contains the current date in the user's selected format. If you set it like any other variable, then it becomes a user-variable, and the value you set it to overrides the system value. Coders will expect date to contain the system value, so it is a really bad idea to use it as a user-variable. Choose another name.

Second, without the code that needs to be inserted, you have

:inicio
IF %i% LEQ %total% (
echo THE CODE/SCRIPT ABOVE SHOULD BE HERE
set /a "i=%i%+1"
::iteracion
echo iteracion: %i%
echo Total: %total%
) else (GOTO :END)
GOTO :inicio

::iteracion is actually a label - sometimes called a "broken label" because you cannot reach it with a goto. Within a "code block" (parenthesised sequence of commands) labels are not allowed, but do not generate an error. Use rem, not :: in code blocks.

Third is the meat of the matter. Batch does not count (s. The first ) it finds matches the ( of the IF %i% LEQ %total% ( so it objects to the following ) - hence the message.

A solution is to insert a caret before the ), ie replace ) with ^) wherever the ) is to be taken literally, not as part of the (in this case, if) statement.

A much simpler solution is to change the sense of the if to

IF %i% GTR %total% GOTO END

echo THE CODE/SCRIPT ABOVE SHOULD BE HERE

set /a "i=%i%+1"
::iteracion
echo iteracion: %i%
echo Total: %total%
GOTO :inicio

Note also that there is no need to use a colon in a goto, with the sole exception of goto :eof where :eof specifically means "physical end-of-file".

Magoo
  • 77,302
  • 8
  • 62
  • 84