How do I run two commands in one line in Windows CMD?

后端 未结 19 1097
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 00:45

I want to run two commands in a Windows CMD console.

In Linux I would do it like this

touch thisfile ; ls -lstrh

How is it done on

相关标签:
19条回答
  • 2020-11-22 00:53

    You can use & to run commands one after another. Example: c:\dir & vim myFile.txt

    0 讨论(0)
  • 2020-11-22 00:55

    When you try to use or manipulate variables in one line beware of their content! E.g. a variable like the following

    PATH=C:\Program Files (x86)\somewhere;"C:\Company\Cool Tool";%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
    

    may lead to a lot of unhand-able trouble if you use it as %PATH%

    1. The closing parentheses terminate your group statement
    2. The double quotes don't allow you to use %PATH% to handle the parentheses problem
    3. And what will a referenced variable like %USERPROFILE% contain?
    0 讨论(0)
  • 2020-11-22 01:00

    A quote from the documentation:

    • Source: Microsoft, Windows XP Professional Product Documentation, Command shell overview
    • Also: An A-Z Index of Windows CMD commands

    Using multiple commands and conditional processing symbols

    You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing symbols, the commands to the right of the conditional processing symbol act based upon the results of the command to the left of the conditional processing symbol.

    For example, you might want to run a command only if the previous command fails. Or, you might want to run a command only if the previous command is successful.

    You can use the special characters listed in the following table to pass multiple commands.

    • & [...]
      command1 & command2
      Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

    • && [...]
      command1 && command2
      Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

    • || [...]
      command1 || command2
      Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

    • ( ) [...]
      (command1 & command2)
      Use to group or nest multiple commands.

    • ; or ,
      command1 parameter1;parameter2
      Use to separate command parameters.

    0 讨论(0)
  • 2020-11-22 01:01

    You can use call to overcome the problem of environment variables being evaluated too soon - e.g.

    set A=Hello & call echo %A%
    
    0 讨论(0)
  • 2020-11-22 01:02

    A number of processing symbols can be used when running several commands on the same line, and may lead to processing redirection in some cases, altering output in other case, or just fail. One important case is placing on the same line commands that manipulate variables.

    @echo off
    setlocal enabledelayedexpansion
    set count=0
    set "count=1" & echo %count% !count!
    
    0 1
    

    As you see in the above example, when commands using variables are placed on the same line, you must use delayed expansion to update your variable values. If your variable is indexed, use CALL command with %% modifiers to update its value on the same line:

    set "i=5" & set "arg!i!=MyFile!i!" & call echo path!i!=%temp%\%%arg!i!%%
    
    path5=C:\Users\UserName\AppData\Local\Temp\MyFile5
    
    0 讨论(0)
  • 2020-11-22 01:03

    It's simple: just differentiate them with && signs. Example:

    echo "Hello World" && echo "GoodBye World".
    

    "Goodbye World" will be printed after "Hello World".

    0 讨论(0)
提交回复
热议问题