For a SQL Server guy it sometimes amazes me how often I’m in and out of batch files and how truly useful they can be. To that end here is all that I can remember about batch files (basically a text file that ends with .bat). Everyone seemed to like 15 years of experience with identity columns so I’m doing this in that same format. Also in the same way please feel free to comment with what you can remember and corrections for any mistakes you find.
- To create a batch file type batch commands into a text file and name it with a .bat extension.
- To run a batch file either double click on it in windows explorer, right click and hit Open, or type the name in a command shell (in the correct directory) and hit enter.
- ECHO ON to print the commands to the output.
- ECHO OFF to stop printing commands to the output.
- ECHO <message> will write <message> to the output.
- REM comments out a line. This line will be printed if ECHO is turned on.
- :: Also comments out the line but will not be printed if ECHO is turned on.
- You can pass in parameters by putting %1, %2, %3 etc in your bat file. These will corrispond to the 1st, 2nd, and 3rd parameters passed in to a limit of %9.
- %* is all parameters.
- SHIFT moves the parameters down one. So the second parameter is now %1, third parameter is %2 etc. This let’s you handle more than 9 parameters.
- If you call the parameters like this %~1, %~2, %~3 then the quotes(“”) surrounding the parameter will be stripped off.
- SET var=xxx xxx Declares the variable var and stores “xxx xxx” into it.
- ECHO %var% to print the contents of the variable var.
- Put :<labelname> at the beginning of a line to create a label.
- Use GOTO :<labelname> to jump to that label.
- IF condition (command) Run command if condition is true.
- IF EXIST <filename> command Run command if <filename> exists.
- NOT <condition> Checks if the condition is not true.
- Use == For equal to.
- Use <> or != for not equal to.
- CD <path> change location to <path>.
- <driveletter>: Change location to current directory of <driveletter>.
- . is the current directory.
- .. is the directory one above the current directory.
- COPY <file1> <Location1> Copies the file <file1> to <Location1>.
- COPY <file1> +<file2> Copies the contents of <file2> into <file1>.
- MOVE <file1> <location1> Moves the file <file1> to <location1>.
- DEL <filename> Deletes <filename> if it exists.
- TYPE <file1> prints the contents of <file1> to the output.
- CALL <batchfile> Executes <batchfile>.
- START <command/program> Starts a command or program.
- <command1> | <command2> passes the output of <command1> to <command2>.
- ECHO |TIME or ECHO |DATE prints the time or date to the output.
- FIND “<string>” outputs only the lines with <string> in them from whatever input is passed.
- DIR |FIND “mystring” outputs only the rows with mystring from the DIR command.
- DIR > test.txt Copies the output of the DIR command to the file test.txt overwriting the file if it already exists.
- EXIT <errorlevel> to leave the bat file and return <errorlevel> to the calling code. <errorlevel> is optional.
Filed under: Batch Files, Documentation Tagged: batch files, code language