Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

unable to set variable in DOS batch file

nirvannirvan Posts: 6Member
I am trying to set a DOS variable like this.

set MPATH = "$MYSQL_PATH"

Here $MYSQL_PATH comes from a different context. The problem is that the variable MPATH never gets set. If I say echo "$MYSQL_PATH", then it properly prints the value contained in $MYSQL_PATH. But when I use echo "%MPATH%" nothing gets printed. Why am I not able to assign the value to MPATH when there is value in $MYSQL_PATH.

regards,
nirvan

Comments

  • anthrax11anthrax11 Posts: 511Member
    Use percent signs to refer to variables and don't put spaces next to the equal sign:
    set MPATH=%MYSQL_PATH%

    Then you can use echo "%MPATH%" or echo %MPATH% to print it.
  • nirvannirvan Posts: 6Member
    Thanks for the reply. That solved my problem. But another problem popped up. Its possible that $MYSQL_PATH won't be defined. So I have to check whether MPATH is empty (null) or not. How do I do that in dos.

    regards,
    nirvan.
  • nirvannirvan Posts: 6Member
    Thanks for the reply. That solved my problem. But another problem popped up. Its possible that $MYSQL_PATH won't be defined. So I have to check whether MPATH is empty (null) or not. How do I do that in dos.

    regards,
    nirvan.
  • anthrax11anthrax11 Posts: 511Member
    There a few different ways to do this:
    [code]
    if defined MYSQL_PATH goto isdefined

    [color=Grey]or:[/color]
    set MPATH=%MYSQL_PATH%
    if defined MPATH goto isdefined

    [color=Grey]or:[/color]
    set MPATH=%MYSQL_PATH%
    if not "%MPATH%"=="" goto isdefined
    [/code]
    If MYSQL_PATH isn't defined, then "set MPATH=%MYSQL_PATH%" is equivalent to "set MPATH=", which undefines MPATH.

    One other thing. Although the principles are the same, a Windows command prompt is not a DOS command prompt.
  • nirvannirvan Posts: 6Member
    I think you have got me wrong. I said in my first post that $MYSQL_PATH is an external variable (not belonging to dos). Before my dos batch file is executed this variable is parsed and replaced by some value. So when the dos batch file runs, $MYSQL_PATH either contains some value (for example "C:Temp") or is null. I am not sure what it contains when its null. So I can't use %MYSQL_PATH% as you have pointed out because its not a dos variable. I think I will have to find out what it is when it is null.

    regards,
    nirvan.
  • anthrax11anthrax11 Posts: 511Member
    There are two practical ways to pass variables to batch files, either by parameters(%1, %2) or by environment variables(%PATH%, etc.), which is what you seem to be doing. In linux scripts and several programming languages(Perl, PHP), variables are referred to with the $ prefix. In batch files(.bat), variables are referred to by double percent signs and the dollar sign has a completely different meaning.

    What's confusing is you're saying it's a DOS batch file. I don't know if you're confusing it for a linux shell script or a windows batch script, but what I know is that there is no MySQL for DOS. You said using dollar signs around MYSQL_PATH fixed the first problem, so why does that suddenly not work now?
  • nirvannirvan Posts: 6Member
    I think I will have to provide more details to make things clear.

    I am using [link=http://izpack.org/features/]IzPack Installer[/link], which is used to package and install java applications on any target computer. Its just a software that helps on distribute custom java applications.

    When using Izpack, Izpack has its own set of [link=http://izpack.org/documentation/installation-files.html#variable-substitution]variables[/link] (or even user defined) which can be used in any file that is marked as parsable. At run time, these variables will be substituted with actual values by the Izpack parsing system. These variables are referred by syntax $VARIABLE ($ sign prefixing the variable name). Note that it has got nothing to do with the unix/linux variables

    So in my case I have a DOS batch file (createDB.bat) marked as parsable,which contains $MYSQL_PATH as an Izpack variable. Now at run time, before the batch file is run, the Iapack variable substitution system, parses the $MYSQL_PATH variable and replaces it with actual value(for example "C: emp"). I don't know what value is substituted when $MYSQL_PATH is empty.

    I said earlier that things worked because, I didn't test it exhaustively. I had to decide what code to execute depending on whether $MYSQL_PATH is empty or has some value. I tested it for empty but not if it had some value. Thus I had a false impression that everything worked well.

    Anyway you have helped me out to solve half the problem. Thanks a lot for that. I hope you have a fair idea of what I am trying to achieve

    regards,
    nirvan.
Sign In or Register to comment.