krishna pushpak
krishna pushpak

Reputation: 11

Passing a string parameter as a runtime Argument

I want to open the particular path through the batch file. And I want to pass one node in path at run time. If I give input Google, I want to pass the string argument here. It will open the path

want to pass one node in path at run time . if i give input Google.i
want to pass the string argument here. it will open the path
C:\Program Files (x86)\google\Common

How can I achieve this?

My batch file:

@echo off
echo test variables 
set input = 
set /p input ="Choice"
echo C:\Program Files (x86)\%input%\Common
cd C:\Program Files (x86)\"%input%"\Common
pause

Upvotes: 0

Views: 1391

Answers (2)

aschipfl
aschipfl

Reputation: 34909

There are the following issues in your code:

  • there is a SPACE between the variable name and the = sign at the set commands, so it becomes a part of the variable name; hence the variable input remains empty/undefined;
  • there are quotes within the path at the cd command, which are forbidden characters there;

Here is the corrected version:

@echo off
echo test variables 
set "input="
set /p "input=Choice"
echo "C:\Program Files (x86)\%input%\Common"
cd "C:\Program Files (x86)\%input%\Common"
pause

I put quotes around the path at the cd command. Although not necessary for cd, many other commands cannot handle paths containing spaces if the "" are missing.


To use the first command line argument instead of user input, remove the set commands and replace %input% by %~1. The ~ ensures that any surrounding quotes are removed. Type call /? in the command prompt for details on this.

Upvotes: 1

Squashman
Squashman

Reputation: 14290

Are you saying you want to run your batch file like this.

C:\>mybatch.bat Google

If so then your batch file just changes to this.

@echo off
echo test variables 
echo C:\Program Files (x86)\%1\Common
cd C:\Program Files (x86)\%1\Common
pause

Upvotes: 0

Related Questions