1

I have been trying to figure this issue out for quite some time now and I am just stuck. I am simply trying to easily parse user input on the command line.

here is what I have so far:

set /p "cmd=hk -> "

for %%a in (%cmd%) do (
   echo %%a
)

With the above code, if I put in the following command:

hk > welcome john

It will return the following:

welcome
John

Now, what I am trying to do is have the output look something like this:

Hello John, how are you?

I tried using %%b, which would be the name of the person, however it did not work. I am very new to batch scripting, so I am just having some trouble understanding some things like this, for example. If anyone could help me figure this out, it would be greatly appreciated.

1
  • I strongly suggest you to change the topic title (and the description) to something more descriptive, like "How to get second word from a string?". Your current title and description is confusing... Commented Apr 20, 2015 at 11:17

2 Answers 2

1

Use the following batch file:

set /p "cmd=hk -> "
for /f "tokens=2 delims= " %%a in ("%cmd%") do (
   echo Hello %%a, how are you?
)

Input:

Welcome John

Output:

Hello John, how are you?

See for /f for the details of how to use tokens and delims.

Sign up to request clarification or add additional context in comments.

Comments

0
set /p "cmd=hk -> "

for /f %%a in ("%cmd%") do (
   echo Hello %%a, how are you?
)

to parse a strings you need FOR /F

1 Comment

Thank you @npocmaka for your quick response. I tried the above code and I got the following output when I put in the command 'welcome John': Hello welcome, how are you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.