批处理文件。来自文件的变量输入不起作用

时间:2014-09-03 13:31:38

标签: variables batch-file for-loop text input

所以我正在尝试做什么。我从他们看起来像这样的文本文件中获取变量

000001 server1 server2
000002 server2 server1

我编写的第一组代码将执行,但只执行文本文件的最后一行

Set _InputFile=c:\usermovelist.txt
For /F "tokens=1-3* delims= " %%A IN (%_InputFile%) DO ( 
Set _var1=%%A
Set _var2=%%B
Set _var3=%%C
)
xcopy \\%_var2%\e$\profiles\%_var1%.V2 \\%_var3%\e$\profiles\%_var1%.V2 /m /e /y /O /h
xcopy \\%_var2%\e$\users\%_var1% \\%_var3%\e$\users\%_var1% /m /e /y /O /h
pause
ren \\%_var2%\e$\profiles\%_var1%.V2 %_var1%.old.V2.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
ren \\%_var2%\e$\users\%_var1% %_var1%.old.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%

然后我尝试了这个

Set _InputFile=c:\usermovelist.txt

For /f "tokens=* delims= " %%f in (%_InputFile%) do (
set line=%%f
call :processToken
)


:processToken

For /F "tokens=1-3* delims= " %%A IN (%line%) DO ( 
Set _var1=%%A
Set _var2=%%B
Set _var3=%%C
)

xcopy \\%_var2%\e$\profiles\%_var1%.V2 \\%_var3%\e$\profiles\%_var1%.V2 /m /e /y /O /h
xcopy \\%_var2%\e$\users\%_var1% \\%_var3%\e$\users\%_var1% /m /e /y /O /h
pause
ren \\%_var2%\e$\profiles\%_var1%.V2 %_var1%.old.V2.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
ren \\%_var2%\e$\users\%_var1% %_var1%.old.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%

但这并没有将变量读入xcopy部分,但它逐行读取,我也需要它。我可以按任意键,然后向下移动

C:\Users\xxxxx\Desktop>For /F "tokens=1-3* delims= " %A IN (000002 server2 server1) DO (
 Set _var1=%A
 Set _var2=%B
 Set _var3=%C

我是新手,所以提前感谢任何帮助!对不起,长篇文章我加粗了好东西

1 个答案:

答案 0 :(得分:0)

删除不需要的变量

Set "_InputFile=c:\usermovelist.txt"
For /F "usebackq tokens=1-3* delims= " %%A IN ("%_InputFile%") DO ( 
    xcopy \\%%B\e$\profiles\%%A.V2 \\%%C\e$\profiles\%%A.V2 /m /e /y /O /h
    xcopy \\%%B\e$\users\%%A \\%%C\e$\users\%%A /m /e /y /O /h
    pause
    ren \\%%B\e$\profiles\%%A.V2 %%A.old.V2.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
    ren \\%%B\e$\users\%%A %%A.old.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
)