Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 840 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you use SETLOCAL in a batch file?

#1
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.
Reply

#2
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...
Reply

#3
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][1] at Microsoft TechNet.

Direct link to [Setlocal][2]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through