使用FOR的批处理脚本不起作用

时间:2013-11-21 18:45:00

标签: batch-file for-loop

我需要以下脚本的帮助:

它不会继续下一行machine.txt 如果我把“echo”放在“echo!machine”之后,它会显示machine.txt里面的机器,因此它可以工作!

但当“)”在脚本末尾时,它不会继续,并退出..

@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

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您无法在@PLTOFF正文中使用gotofor会打破goto循环上下文。

我会将标记的部分for:64bits放入子例程并:32bits

call

注意:我没有检查脚本的用途和逻辑,我刚刚解决了@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 / for问题。