0

Why is this code not working?

@echo off
set /p param=Enter Parameters: 
echo %param%

Output:

(Nothing)

I have searched all relative posts, but I can't find what is wrong with it. There is no space problem, or syntax problem that I can identify

Update: As rojo stated, since the code block is working, here is the full code, which is not working.

@echo off

for /f %%j in ("java.exe") do (
    set JAVA_HOME=%%~dp$PATH:j
)

if %JAVA_HOME%.==. (
    echo java.exe not found
) else (
    set /p param=Enter Parameters: 
    echo %param%
    (statement using JAVA_HOME)
)

pause

Output:

Enter Parameters: jfdklsaj
ECHO is off.
...
gagarwa
  • 1,426
  • 1
  • 15
  • 28
  • 1
    Unable to recreate. There's nothing wrong with that code. Must be something wrong with where you're putting it. – rojo Jun 12 '15 at 13:40
  • possible duplicate of [Example of delayed expansion in batch file](http://stackoverflow.com/questions/10558316/example-of-delayed-expansion-in-batch-file) – Laf Jun 12 '15 at 13:52
  • I don't really understand why, but it works, so I will put this as a solution. – gagarwa Jun 12 '15 at 13:56
  • @gagarwal I suggest you remove your question, there's already lots of questions on SO that are answered by the delayed expansion, I don't believe we need another one. I'm glad the link could help you though. – Laf Jun 12 '15 at 13:59
  • @Laf I get your point, but beginners may find this useful, like my self, who didn't know what delayed expansion was. I. Will remove it in a month if it isn't. – gagarwa Jun 13 '15 at 16:12

1 Answers1

0

As Laf indicated, this can be fixed with delayed expansion:

@echo off
setlocal enableDelayedExpansion

for /f %%j in ("java.exe") do (
set JAVA_HOME=%%~dp$PATH:j
)

if %JAVA_HOME%.==. (
    echo java.exe not found
) else (
    set /p param=Enter Parameters: 
    echo !param!
    (statement using JAVA_HOME)
)
gagarwa
  • 1,426
  • 1
  • 15
  • 28