批处理脚本与FOR不起作用 [英] batch script with FOR does not work
本文介绍了批处理脚本与FOR不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它不会继续下一行machine.txt
如果我把)放在echo!machine,它显示machine.txt里面的机器,因此,它的工作!
但是当)在脚本的末尾
@echo off
set server = \\server01\share
dir / b / o%server%| findi32| more +2> 32.txt
FOR / Ftokens = *%% A in(32.txt)do SET file32 = %% A
dir / b / o%server%| find i64| more +2> 64.txt
FOR / Ftokens = *%% B in(64.txt)do SET file64 = %% B
setlocal EnableDelayedExpansion
/ ftokens = *%% C in(machines.txt)do(
setmachine = %% C
echo!machine!
if exists\\!机器!\c $ \程序文件(x86)goto 64位
goto goo
:goo
如果存在\\!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已完成!micro!
)
pause
有什么想法吗?谢谢!
解决方案您不能在<$ c $内使用
goto
对于循环上下文, for 正文为 goto
打破了。 b
$ b 我将标记的部分:64位
和:32位
例程和调用
它们:
@echo off
set server = \\server01\share
dir / B / O%server%|找到i32|更多+2> 32.txt
FOR / Ftokens = *%% A in(32.txt)do SET file32 = %% A
dir / B / O%server%找到i64|更多+2> 64.txt
FOR / Ftokens = *%% B in(64.txt)do SET file64 = %% B
setlocal EnableDelayedExpansion
/ Ftokens = *%% C in(machines.txt)do(
setmachine = %% C
echo!machine!
if exists\\! (
call:64bits
)else(
if exists)\\!machine!\ $ $ \Arquivos (x86)(
调用:64位
)其他(
调用:32位
)
)
echo完成!micro!
)
暂停
endlocal
退出/ B
:64位
xcopy / D / Y / F / C%server%\%file64% \\!机器!\c $ \
PsExec.exe -d \\机器! C:\%file64%/ q
exit / B
:32bits
xcopy / D / Y / F / C%server%\%file32%\\ \\\!机器!\c $ \
PsExec.exe -d \\机器! C:\%file32%/ q
exit / B
注意:我没有检查脚本的目的和逻辑,我只是为
/ goto $ c $修正了 c>问题。
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!
解决方案 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.
这篇关于批处理脚本与FOR不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文