Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use SETLOCAL in a batch file?

Tags:

batch-file

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.

like image 561
Dre_Dre Avatar asked Dec 04 '12 13:12

Dre_Dre


People also ask

What is Setlocal and Endlocal in batch file?

Setlocal syntaxBegins localization of environment changes in a batch file. Environment changes made after SETLOCAL is issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings.

What is Setlocal Enabledelayedexpansion in batch script?

Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL EnableDelayedExpansion command.

What is @echo off in batch?

batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

What is Setlocal Enableextensions Enabledelayedexpansion?

setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONSset variable modifications local to this script, i.e., the change to variable value disappears after the script ends. The variables revert to their original values. Without setlocal, the changes of variables preserves even after the bat script exits.


1 Answers

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

like image 98
Ken White Avatar answered Sep 24 '22 04:09

Ken White