42

How do you use setlocal in a batch file? I am just learning scripting and would like it explained to me in very simple terms.

I have a script that stops and says < was unexpected at this time it may have something to do with not having any setlocal statements in the script.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Dre_Dre
  • 745
  • 2
  • 6
  • 15

3 Answers3

27

You make the first line SETLOCAL. This example is from the linked article below:

rem *******Begin Comment**************
rem This program starts the superapp batch program on the network,
rem directs the output to a file, and displays the file
rem in Notepad.
rem *******End Comment**************
@echo off
setlocal
path=g:\programs\superapp;%path%
call superapp>c:\superapp.out
endlocal
start notepad c:\superapp.out

The most frequent use of SETLOCAL is to turn on command extensions and allow delayed expansion of variables:

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

For more info on SETLOCAL see the Command Line Reference at Microsoft TechNet.

Direct link to Setlocal

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 3
    Can you give me an example of how it works? How and why you would use it? – Dre_Dre Dec 04 '12 at 13:56
  • 13
    I think the first sentence of the linked site does that, which is "Starts localization of environment variables in a batch file. Localization continues until a matching endlocal command is encountered or the end of the batch file is reached.". In other words, it allows you to set environmental variables (that are usually global) only seen inside your batch file and automatically cleans them up when the `ENDLOCAL` call is executed or the batch file ends. – Ken White Dec 04 '12 at 14:00
  • Yes, this is a great example - if you need to add something to the `PATH` variable, but the script needs to be run multiple times, you can make is like in the example. Also, you don't need to cleanup the environment after your script. IOW, by invoking`setlocal` before you `SET` anything, you make Windows shell scripts behave like Bash scripts (which cannot alter the parent shell's environment). – Tomasz Gandor Oct 07 '16 at 12:00
  • 2
    Be aware that `setlocal` also sets any directory changes as local as well! This means that after, you will return to you original directory. – Dylan Watson Mar 07 '18 at 02:51
11

Suppose this code:

If "%getOption%" equ  "yes" (
   set /P option=Enter option: 
   echo Option read: %option%
)

Previous code will NOT work becase %option% value is replaced just one time when the IF command is parsed (before it is executed). You need to "delay" variable value expansion until SET /P command had modified variable value:

setlocal EnableDelayedExpansion
If "%getOption%" equ  "yes" (
   set /P option=Enter option: 
   echo Option read: !option!
)

Check this:

set var=Before
set var=After & echo Normal: %var%  Delayed: !var!

Guess what the output is...

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    is the answer to the question "before"? – Dre_Dre Dec 07 '12 at 17:13
  • 2
    It is `Normal: Before Delayed: After`, but Delayed variable Expansion must be Enabled in order for this to work. Type `SET /?` and carefully read from "Finally, support for delayed environment variable expansion has been added." on... – Aacini Dec 08 '12 at 01:33
-4

Try this:

SET PATH=%PATH%;%~dp0;

This will get your local folder your are running the batch from and add it to the current path.

example: if your are running a .bat or .cmd from d:\tools\mybatch.bat it will add d:\tools to the current path so that it may find additional files on that folder.

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
HandyManny
  • 37
  • 2