-1

Please Please Please

can anyone explain why the following code works if I manually enter it line by line but it doesn't work if I try to execute it through a batch file?

@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

I'm at a complete loss for what I'm missing here.

Dan Embry
  • 1
  • 1
  • 3
    Which part isn't working? You haven't actually described your problem at all. "It doesn't work" is a meaningless phrase. – SomethingDark Jul 08 '16 at 11:21
  • DOH! Sorry about that. The %OrgUnit% variable isn't being set when the script is run from a batch file. When I manually type it, it is. The last line of the script should display something like: At the end of the Restore, do you want to move dan-2813-pc to "CN=DAN-2810-PC,CN=Computers,DC=mydomain,DC=com" Instead, what I'm getting is: At the end of the Restore, do you want to move dan-2813-pc to – Dan Embry Jul 08 '16 at 11:57
  • you need [delayed expansion](http://stackoverflow.com/a/30284028/2152082) – Stephan Jul 08 '16 at 12:06
  • Thanks Stephan but you can set that EnableDelayedExpansion is turned on (line 2). – Dan Embry Jul 08 '16 at 12:54
  • When you run the batch file that "it doesn't work," are you using `CALL abatchfile.bat`? – lit Jul 08 '16 at 13:29
  • Whether I 'CALL' it (i.e. C:>call script.bat) or not, the result is still the same. – Dan Embry Jul 08 '16 at 13:35
  • enabling it is not enough. You also have to *use* it. See Anon Coward's answer. – Stephan Jul 08 '16 at 14:45
  • Please do not place the problem description in a comment, edit your question instead! – aschipfl Jul 08 '16 at 16:08

1 Answers1

0

The help for ENABLEDELAYEDEXPANSION says:

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.

And the help for /V: says:

/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.

In other words, if you want to see the changed value of a enviornment variable after setting it within a block, you need to change your code to use ! instead of %:

SET /p MoveOU=At the end of the %Action%, do you want to move %Target% to !OrgUnit! [Y/N]?
Anon Coward
  • 9,784
  • 3
  • 26
  • 37
  • GOT IT! Thank you so much! I mistakenly thought the % and ! were interchangeable and not delimiters for variable expansion. – Dan Embry Jul 08 '16 at 15:04