1

I am using listary as quick launch tool . I want to start cmd at some place and enter a python virtualenv, which need extra command activate py3.

I tried

cmd.exe /K cd /d "{query}" & "activate py3"

Here, {query} is the dir path. But it doesn't work. What is the correct way ?

3

Solved,

cmd.exe /K "cd /d {query} && activate py3"

The form is:

cmd.exe /K "command1 && command2"
  • 1
    Please consider adding a bit of explanation as to why this solution works. – Twisty Impersonator Dec 25 '17 at 3:47
2

The answer to Question has already been answered, I'll just explain it a bit further. In Windows Command Line, We can execute multiple commands in just a single line

Using '&' (Ampersand) OR '&&' (Double Ampersand)

Using Single Ampersand & causes Sequential Execution ie. Commands run in the sequence they are entered. There is no condition checking in it. There may be a case when second command can execute successfully Only If the first command is executed successfully.

That introduces Double Ampersand && . This operator is kind of conditional operator and performs an Error Checking ie. Second command will execute only if the first command is executed successfully.

Example :

$ cd Docs & dir

First cd Docs will run & regardless of whether it was successful or not, second command dir will run.

  1. If Docs folder exist then It will change the directory & then list contents of Docs using dir command.
  2. However If Docs folder doesn't exist then It will give an error, but still dir will run to list the contents of current directory.

$ cd Docs && dir

Here, dir command will run only when current Directory is changed to Docs and that will happen only when Docs exist. So, you can interpret the command like - " If folder Docs exists, change the current directory and then list it's contents. "

This is very much useful in the Batch Programming and sometimes at Command-Line too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.