批量文件设置变量来自文本文件的内容

时间:2016-07-08 11:08:58

标签: batch-file cmd

请拜托

任何人都可以解释为什么如果我逐行手动输入以下代码,但如果我尝试通过批处理文件执行它,它就不起作用了吗?

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SETLOCAL EnableExtensions

Set Source=%computername%
Set Backups=%Source%\c$\users\travel\desktop
Set Target=dan-2813-pc
Set Action=Restore

    IF EXIST "\\%Backups%\%Source%-OrgUnit.txt" (

      SET /p OrgUnit=<\\%Backups%\%Source%-OrgUnit.txt

      SET /p MoveOU=At the end of the %Action%, do you want to move %Target% to %OrgUnit% [Y/N]?

        )

ENDLOCAL

我完全失去了我在这里失踪的东西。

1 个答案:

答案 0 :(得分:0)

ENABLEDELAYEDEXPANSION的帮助说:

ENABLEDELAYEDEXPANSION / DISABLEDELAYEDEXPANSION
    enable or disable delayed environment variable
    expansion. These arguments takes precedence over the CMD
    /V:ON or /V:OFF switches. See CMD /? for details.

/V:的帮助说:

/V:ON   Enable delayed environment variable expansion using ! as the
        delimiter. For example, /V:ON would allow !var! to expand the
        variable var at execution time.  The var syntax expands variables
        at input time, which is quite a different thing when inside of a FOR
        loop.

换句话说,如果您希望在块中设置环境变量之后查看更改后的值,则需要更改代码以使用!而不是%

SET /p MoveOU=At the end of the %Action%, do you want to move %Target% to !OrgUnit! [Y/N]?