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:
  • 750 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Defining and using a variable in batch file

#1
I'm trying to define and use a variable in a batch file. It looks like it should be simple:

<!-- language: lang-dos -->
@echo off

set location = "bob"
echo We're working with "%location%"

The output I get is the following:

We're working with ""

What's going on here? Why is my variable not being echo'd?
Reply

#2
The space before the `=` is interpreted as part of the name, and the space after it (as well as the quotation marks) are interpreted as part of the value. So the variable you’ve created can be referenced with `%location %`. If that’s not what you want, remove the extra space(s) in the definition.
Reply

#3
input `location.bat`

@echo off
cls

set /p "location"="bob"
echo We're working with %location%
pause

output

We're working with bob

(mistakes u done : `space` and `" "`)
Reply

#4
The spaces are significant. You created a variable named `'location '`
with a value of
`' "bob"'`. *Note - enclosing single quotes were added to show location of space.*

If you want quotes in your value, then your code should look like

set location="bob"

If you don't want quotes, then your code should look like

set location=bob

Or better yet

set "location=bob"

The last syntax prevents inadvertent trailing spaces from getting in the value, and also protects against special characters like `&` `|` etc.
Reply

#5
Consider also using [`SETX`][1] - it will set variable on user or machine (available for all users) level though the variable will be usable with the next opening of the cmd.exe ,so often it can be used together with `SET` :

::setting variable for the current user
if not defined My_Var (
set "My_Var=My_Value"
setx My_Var My_Value
)

::setting machine defined variable
if not defined Global_Var (
set "Global_Var=Global_Value"
SetX Global_Var Global_Value /m
)

You can also edit directly the registry values:

>User Variables: HKEY_CURRENT_USER\Environment

>System Variables: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Which will allow to avoid some restrictions of SET and SETX like the variables containing `=` in their names.

### 3rd party edit

> SETX.exe
> Set environment variables permanently,
>
> SETX can be used to set Environment Variables for the machine (HKLM) or currently logged on user (HKCU):

Option `/m`

/m
Set the variable in the system environment HKLM.
(The default is the local environment HKCU)

Another example

::setting variable for the current user
if not defined JAVAJDK (
set "JAVAJDK=C:\Program Files\Java\jdk-13\bin"
setx JAVAJDK "C:\Program Files\Java\jdk-13\bin"
)

In a command.exe you can use the variable like this `cd %JAVAJDK%`.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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