0

I need help with my script below:

It does not continue for the next row of machine.txt If I put the ")" after "echo !machine", it shows the machines inside the machine.txt, therefore, it works!

But when the ")" is at the end of the script it does not continue, and exit..

@echo off
set server=\\server01\share

dir /b /o %server% |find "i32" |more +2 > 32.txt
FOR /F "tokens=*" %%A in (32.txt) do SET file32=%%A

dir /b /o %server% |find "i64" |more +2 > 64.txt
FOR /F "tokens=*" %%B in (64.txt) do SET file64=%%B

setlocal EnableDelayedExpansion

for /f "tokens=*" %%C in (machines.txt) do (
  set "machine=%%C"
  echo !machine!
  if exist "\\!machine!\c$\Program Files (x86)" goto 64bits
  goto goo

  :goo
  if exist "\\!machine!\c$\Arquivos de Programas (x86)" goto 64bits
  goto 32bits

  :64bits
  xcopy /D /Y /F /C %server%\%file64% \\!machine!\c$\
  PsExec.exe -d \\!machine! "C:\%file64%" /q
  goto end

  :32bits
  xcopy /D /Y /F /C %server%\%file32% \\!machine!\c$\
  PsExec.exe -d \\!machine! "C:\%file32%" /q
  goto end

  :end
  echo Finished !micro!
)
pause

Any idea? Thanks!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
the-burgh
  • 39
  • 5

1 Answers1

0

You cannot use goto within the for body as goto breaks the for loop context.

I would place your labelled sections :64bits and :32bits into sub-routines and call them:

@echo off
set server=\\server01\share

dir /B /O %server% | find "i32" | more +2 > 32.txt
FOR /F "tokens=*" %%A in (32.txt) do SET file32=%%A

dir /B /O %server% | find "i64" | more +2 > 64.txt
FOR /F "tokens=*" %%B in (64.txt) do SET file64=%%B

setlocal EnableDelayedExpansion

for /F "tokens=*" %%C in (machines.txt) do (
  set "machine=%%C"
  echo !machine!
  if exist "\\!machine!\c$\Program Files (x86)" (
    call :64bits
  ) else (
    if exist "\\!machine!\c$\Arquivos de Programas (x86)" (
      call :64bits
    ) else (
      call :32bits
    )
  )
  echo Finished !micro!
)
pause
endlocal
exit /B

:64bits
xcopy /D /Y /F /C %server%\%file64% \\!machine!\c$\
PsExec.exe -d \\!machine! "C:\%file64%" /q
exit /B

:32bits
xcopy /D /Y /F /C %server%\%file32% \\!machine!\c$\
PsExec.exe -d \\!machine! "C:\%file32%" /q
exit /B

Note: I did not check the purpose nor the logic of your script, I just fixed the for/goto issue.

aschipfl
  • 33,626
  • 12
  • 54
  • 99