61

I received a line of code from someone who answered one of my questions, but I am confused: what do the "&&" do in this batch file.

@echo off
set /p Quest="How are you today? "
echo %Quest% > Results.txt
findstr /r /i "not.*good not.*well" Results.txt >nul && echo Sorry && goto pause
findstr /i "good well" Results.txt >nul && echo My day is doing good as well
:pause
pause
ZygD
  • 22,092
  • 39
  • 79
  • 102
Kit
  • 1,071
  • 2
  • 10
  • 14
  • 2
    Wow, http://www.robvanderwoude.com/condexec.php – Mathemats Mar 05 '15 at 23:42
  • @Mathemats Thanks for linking me to website, it has everything I need. – Kit Mar 05 '15 at 23:47
  • It's definitely a good beginning site, but take note that a good chunk of it is outdated or obsolete - everything on the `debug` page, for example. – SomethingDark Mar 05 '15 at 23:51
  • @SomethingDark Okay, I will keep that in mind while I learn from it. – Kit Mar 06 '15 at 00:34
  • The "eqivalent to" column on that page is not eqivalent when output redirection is used, see this post: http://stackoverflow.com/a/10358437/1941779 – CrouZ Dec 25 '15 at 14:25

2 Answers2

164
  • & separates commands on a line.

  • && executes this command only if previous command's errorlevel is 0.

  • || (not used above) executes this command only if previous command's errorlevel is NOT 0

  • > output to a file

  • >> append output to a file

  • < input from a file

  • | output of one command into the input of another command

  • ^ escapes any of the above, including itself, if needed to be passed to a program

  • " parameters with spaces must be enclosed in quotes

  • + used with copy to concatenate files. E.G. copy file1+file2 newfile

  • , used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

  • %variablename% an inbuilt or user set environmental variable

  • !variablename! a user set environmental variable expanded at execution time, turned with SetLocal EnableDelayedExpansion command

  • %<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.

  • %* (%*) the entire command line.

  • %<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Serenity
  • 1,708
  • 2
  • 9
  • 3
  • +1 for putting all that in one place. I don't understand the comma description. copy /b file1,, returned "The file cannot be copied to itself." I ran it from the command line. Would it do something different from within a batch file? – riderBill Jul 29 '17 at 03:34
  • 14
    Where it comes from? – Deilan Feb 07 '18 at 14:31
  • 1
    a clarification about one ampersand & "Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command. " So I understand, that commands separated with & runs sequential like with &&, but they run always (no matter the errorlevel of a previous command) – – Dawid Ohia May 29 '19 at 13:36
  • 7
    Great info, but what is the source? – cowlinator Jul 08 '19 at 18:17
79

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0). The opposite of && is ||, which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0).

Mike R
  • 329
  • 2
  • 11
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • 5
    Here's a reference page for Win XP (lol) but still contains helpful information. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true – user4317867 Mar 06 '15 at 06:56
  • 1
    @user4317867 the link is broken – Spandyie Nov 02 '18 at 20:58
  • 6
    @Spandy: https://web.archive.org/web/20060412075633/https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true – Parsa Feb 26 '19 at 16:53
  • 2
    All this stuff is on ss64.com https://ss64.com/nt/syntax-conditional.html and https://ss64.com/nt/syntax-redirection.html – JGFMK Oct 07 '22 at 22:12
  • 1
    Watch out for a single `&`, which works, but doesn't check whether the previous command was successful. – js2010 Jul 25 '23 at 15:44