简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM