Setlocal command in Windows
The setlocal command helps start the process of differentiating environment variables in a batch file. This process will continue until paired with the appropriate endlocal command or the batch file end.To better understand how to use this command, see the example below.
Setlocal command syntax
setlocal [enableextensions | disableextensions] [enabledelayedexpansion | disabledelayedexpansion]
Setlocal command argument
Argument
Describe
enableextensions
Activate the command extensions until connected to the appropriate endlocal command, regardless of the settings that were applied before the setlocal command was run.
disableextensions
Turn off the command extensions until you connect to the appropriate endlocal command, regardless of the settings that were applied before the setlocal command was run.
enabledelayedexpansion
Allow to extend the delayed environment variable until it is connected to the appropriate endlocal command, regardless of the settings that were applied before the setlocal command was run.
disabledelayedexpansion
Disable the extension of the delayed environment variable until it connects to the appropriate endlocal command, regardless of the settings that were applied before the setlocal command was run.
/?
Show help at thecommand prompt.
Comment setlocal command
Use the setlocal command
- When you use the setlocal command outside the script or batch file, it will be completely invalid.
Change environment variables
- Use the setlocal command to change environment variables when you run a batch file. The environmental changes that will be made after you run the setlocal command are local changes to the batch file. The Cmd.exe program will restore previous settings when it is paired with the appropriate endlocal command or the batch file end.
Nested commands
- You can use the integration of multiple setlocal or endlocal commands in a batch program (ie nested commands).
Check command extensions in batch files
- The setlocal command will set the ERRORLEVEL variable.disabledelayedexpansion}, variable ERRORLEVEL will be set to 0 (zero).Otherwise, it will be set to 1. You can use this information in batch scripts to determine if extensions are available, as in the following example:
setlocal enableextensions
verify other 2> nul
if errorlevel 1 echo Unable to enable extensions
- Because cmd does not set the ERRORLEVEL variable when the command extension is disabled, the verify command will initialize ERRORLEVEL to a value other than 0 when you use it with an invalid argument.disabledelayedexpansion} and it does not set the ERRORLEVEL variable to 1, the command extensions will not be available.
The setlocal command example
You can differentiate environment variables in a batch file, as shown in the following sample script:
rem ******* Begin Comment **************
rem chương trình này bắt đầu các chương trình superapp chương trình trên mạng,
đang ghi lại các kết quả vào một tập tin, và hiển thị tập tin
rem in Notepad.
rem ******* End Comment **************
@echo off
setlocal
path = g: programssuperapp;% path%
superapp call> c: superapp.out
endlocal
start notepad c: superapp.out
See more:
- Check and fix hard drive errors with CMD on Windows
- 21 Command Prompt tricks you may not know
- How to create disk full BAT file repair Windows 10
You should read it
May be interested
- Rem command in Windowsthe rem command helps write comments (comments) in a batch file or config.sys.
- Instructions for using Command Promptdeep in windows is a command-line world that is obscure. in this article, we will show you how to solve errors and make your computer more secure.
- Rd command in Windowsthe rd command helps delete a directory
- Cmd command in Windowsthe cmd command starts a new version of the command interpreter, cmd.exe. if used without parameters, cmd will display copyright information and the version of the operating system.
- How to use the command history function in Command Promptcommand prompt is an extremely familiar command for anyone using windows operating system. besides, a lot of current software also supports the command line to perform actions on the command prompt window, instead of on the screen.
- The sfc command in Windows(applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
- Reg command copy in Windowsto copy an entry to the location specified on the local pc or the remote computer we will use the reg copy command. to learn more about how to use the reg copy command, you can track the syntax, explain the command parameters and the example tipsmake.com has compiled below.
- Fc command in Windowsthe fc command compares two files or a collection of files and displays the differences between them.
- The echo command in Windowsthe echo command displays the message or turns on / off the command repeat feature. if used without parameters, the echo command will display the current echo setting.
- Xcopy command in Windowsthe xcopy command is a command prompt command used to copy one or more files or folders from one location to another. with many options and the ability to copy entire directories, the xcopy command is similar to, but much more powerful, than the copy command.