gthm
gthm

Reputation: 1948

Whats the difference in executing .bat file by double click and from command prompt

I was running the following batch file (.bat) file from command prompt and also by double clicking, but it gives different output in both the cases.

@echo off

echo The user name is %USERNAME% > log.txt

set instDir=%cd%

set Prop_TXT="%instDir%\bin\packages\sometextfile.txt"

findstr /C:StringToFind %Prop_TXT% >> log.txt


for /F "usebackq  tokens=1,2,3 delims=/" %%i in (`findstr javavm %Prop_TXT%`) do (

set DIRE=%%j


"%instDir%\bin\%DIRE%\bin\java.exe" -version 2>> log.txt

)

In command prompt, the log.txt gives the proper output with the version of Java. By double clicking, the log.txt shows "The system cannot find the path specified."

Please help me. I did a lot of googe search but could not find the solution.

Upvotes: 3

Views: 1691

Answers (1)

ElektroStudios
ElektroStudios

Reputation: 20464

first: set instDir=%cd%

If you have the current path saved in variable "CD" why you want to store it again in "instdir" var?

Second: you need to expand the variable inside a FOR, you can use the setlocal enabledelayedexpansion command.

Third: One difference is in command prompt you need to use one % symbol, when you use two %% in a script, so a "FOR %%i" or "SET DIRE=%%j" can't go on directly in a command prompt.

Try this:

@echo off
echo The user name is %USERNAME% > log.txt

set Prop_TXT=".\bin\packages\sometextfile.txt"

findstr /C:StringToFind %Prop_TXT% >> log.txt

for /F "usebackq tokens=1,2,3 delims=/" %%i in (`findstr javavm %Prop_TXT%`) do (
    set "DIRE=%%j"
    Call ".\bin\%%DIRE%%\bin\java.exe" -version 2>> log.txt
)

Upvotes: 1

Related Questions