- /* steve jansen */
- // another day in paradise hacking code and more
- Guide to Windows Batch Scripting
- Why Windows?
- Why DOS-style Batch Files?
- Series Parts
- Comments
- Guides
- Recent Posts
- Social Stuff
- Batch Script — Commands
- /* steve jansen */
- // another day in paradise hacking code and more
- Windows Batch Scripting: Getting Started
- Getting Started with Windows Batch Scripting
- Launching the Command Prompt
- Editing Batch Files
- Viewing Batch Files
- Batch File Names and File Extensions
- Saving Batch Files in Windows
- Running your Batch File
- Comments
- Silencing Display of Commands in Batch Files
- Debugging Your Scripts
- Comments
- Guides
- Recent Posts
- Social Stuff
/* steve jansen */
// another day in paradise hacking code and more
Guide to Windows Batch Scripting
I love shell scripting – it’s the duct tape of programming to me. Low cost, high benefit. And it feels like art, where one can learn to do increasingly complex tasks with greater simplicity.
Sadly, I feel like it’s a developer skill on the decline. Maybe new developers feel it’s “not real programming”. Perhaps the growing dominance of Java as the lingua franca of academic comp sci courses has made shell scripting less relevant in university settings.
True, shell scripting feel a bit “vocational”, maybe even a bit unsexy compared to Python/Ruby/LISP/blah/blah/blah. Nevertheless, it’s a skill that becomes invaluable as you gain seniority and start doing more DevOps in you day job, or if you want to do some high-speed, low drag stuff to tailor your development environment like this.
Why Windows?
This series will share some of the tips and tricks I’ve picked up through the years of working with Windows professionally. I’ll be the first to admit the Unix shells of the world are far superior to the Windows command prompt (or even Windows PowerShell). Windows is a fact of life for most professionals writing code for coporate customers; this series aims to make life with Windows a little easier.
Why DOS-style Batch Files?
This series will share some conventions I picked up along the way for scripting in Windows via command prompt batch files. The Windows PowerShell is definitely sweet, but, I still like batch files for their portability and low friction. The Windows command line is very stable – no worrying about the PowerShell interpreter path, which version of PowerShell the server is running, etc.
Series Parts
Posted by Steve Jansen Mar 1 st , 2013 batch, scripting, shell, windows
Comments
Hi, I’m Steve. I’m a software developer loving life in Charlotte, NC, an (ISC) 2 CSSLP and an avid fan of Crossfit.
And, no, I’m not Steve Jansen the British jazz drummer, though that does sound like a sweet career.
Guides
Recent Posts
Social Stuff
- @steve-jansen on GitHub
- @steve-jansen on StackOverflow
- @steve-jansen ProTips on Coderwall
- @steve-jansen on Microsft Connect
- @steve-jansen on ASP.NET User Voice
- Subscribe via RSS
Copyright © 2015 — Steve Jansen — Powered by Octopress
Batch Script — Commands
In this chapter, we will look at some of the frequently used batch commands.
S.No | Commands & Description |
---|---|
1 | VER This batch command shows the version of MS-DOS you are using. This is a batch command that associates an extension with a file type (FTYPE), displays existing associations, or deletes an association. This batch command helps in making changes to a different directory, or displays the current directory. This batch command clears the screen. This batch command is used for copying files from one location to the other. This batch command deletes files and not directories. This batch command lists the contents of a directory. This batch command help to find the system date. This batch command displays messages, or turns command echoing on or off. This batch command exits the DOS console. This batch command creates a new directory in the current location. This batch command moves files or directories between directories. This batch command displays or sets the path variable. This batch command prompts the user and waits for a line of input to be entered. This batch command can be used to change or reset the cmd.exe prompt. This batch command removes directories, but the directories need to be empty before they can be removed. Renames files and directories This batch command is used for remarks in batch files, preventing the content of the remark from being executed. This batch command starts a program in new window, or opens a document. This batch command sets or displays the time. This batch command prints the content of a file or files to the output. This batch command displays the volume labels. Displays or sets the attributes of the files in the curret directory This batch command checks the disk for any problems. This batch command provides a list of options to the user. This batch command invokes another instance of command prompt. This batch command compares 2 files based on the file size. This batch command converts a volume from FAT16 or FAT32 file system to NTFS file system. This batch command shows all installed device drivers and their properties. This batch command extracts files from compressed .cab cabinet files. This batch command searches for a string in files or input, outputting matching lines. This batch command formats a disk to use Windows-supported file system such as FAT, FAT32 or NTFS, thereby overwriting the previous content of the disk. This batch command shows the list of Windows-supplied commands. This batch command displays Windows IP Configuration. Shows configuration by connection and the name of that connection. This batch command adds, sets or removes a disk label. This batch command displays the contents of a file or files, one screen at a time. Provides various network services, depending on the command used. This batch command sends ICMP/IP «echo» packets over the network to the designated address. This batch command shuts down a computer, or logs off the current user. This batch command takes the input from a source file and sorts its contents alphabetically, from A to Z or Z to A. It prints the output on the console. This batch command assigns a drive letter to a local folder, displays current assignments, or removes an assignment. This batch command shows configuration of a computer and its operating system. This batch command ends one or more tasks. This batch command lists tasks, including task name and process id (PID). This batch command copies files and directories in a more advanced way. This batch command displays a tree of all subdirectories of the current directory to any level of recursion or depth. This batch command lists the actual differences between two files. This batch command shows and configures the properties of disk partitions. This batch command sets the title displayed in the console window. Displays the list of environment variables on the current system. /* steve jansen */// another day in paradise hacking code and moreWindows Batch Scripting: Getting StartedGetting Started with Windows Batch ScriptingWindows batch scripting is incredibly accessible – it works on just about any modern Windows machine. You can create and modify batch scripts on just about any modern Windows machine. The tools come out of the box: the Windows command prompt and a text editor like Notepad.exe. It’s definitely far from the best shell scripting langauge, but, it gets the job done. It’s my “duct tape” for Windows. Launching the Command PromptWindows gurus launch the command prompt using the keyboard shortcut Windows Logo Key + R (i.e., “Run”) > Type cmd.exe then Enter . This is way faster than navigating the Windows Start Menu to find the Command Prompt. Editing Batch FilesThe universal text editor for batch files is Notepad ( Windows Logo Key + R > Type notepad then Enter ). Since batch files are just ASCII text, you can probably use just about any text editor or word processor. Very few editors do anything special for Batch files like syntax highlighting or keyword support, so notepad is good enough fine and will likely be installed on just about every Windows system you encounter. Viewing Batch FilesI would stick with Notepad for viewing the contents of a batch file. In Windows Explorer (aka, “My Computer”), you should be able to view a batch file in Notepad by right clicking the file and seleting Edit from the context menu. If you need to view the contents within a command prompt window itself, you can use a DOS command like TYPE myscript.cmd or MORE myscript.cmd or EDIT myscript.cmd Batch File Names and File ExtensionsAssuming you are using Windows XP or newer, I recommend saving your batch files with the file extension .cmd . Some seriously outdated Windows versions used .bat , though I recommend sticking with the more modern .cmd to avoid some rare side effects with .bat files. With the .cmd file extension, you can use just about filename you like. I recommend avoiding spaces in filenames, as spaces only create headaches in shell scripting. Pascal casing your filenames is an easy way to avoid spaces (e.g., HelloWorld.cmd instead of Hello World.cmd ). You can also use punctuation characters like . or — or _ (e.g. Hello.World.cmd , Hello-World.cmd , Hello_World.cmd ). Another thing with names to consider is avoiding names that use the same name of any built-in commands, system binaries, or popular programs. For example, I would avoid naming a script ping.cmd since there is a widely used system binary named ping.exe . Things might get very confusing if you try to run ping and inadvertently call ping.cmd when you really wanted ping.cmd . (Stay tuned for how this could happen.) I might called the script RemoteHeartbeat.cmd or something similar to add some context to the script’s name and also avoid any naming collisions with any other executable files. Of course, there could be a very unique circumstance in which you want to modify the default behavior of ping in which this naming suggestion would not apply. Saving Batch Files in WindowsNotepad by default tries to save all files as plain jane text files. To get Notepad to save a file with a .cmd extension, you will need to change the “Save as type” to “All Files (.)”. See the screenshot below for an example of saving a script named “HelloWorld.cmd” in Notepad.
Running your Batch FileThe easy way to run your batch file in Windows is to just double click the batch file in Windows Explorer (aka “My Computer”). Unfortunately, the command prompt will not give you much of a chance to see the output and any errors. The command prompt window for the script will disappear as soon as the script exits. (We will learn how to handle this problem in Part 10 – Advanced Tricks ). When editing a new script, you will likely need to run the batch file in an existing command window. For newbies, I think the easiest foolproof way to run your script is to drag and drop the script into a command prompt window. The command prompt will enter the full path to your script on the command line, and will quote any paths containing spaces. Some other tips to running batch files:
CommentsThe official way to add a comment to a batch file is with the REM (Remark) keyword: The power user method is to use :: , which is a hack to uses the the label operator : twice, which is almost always ignored. Most power authors find the :: to be less distracting than REM . Be warned though there are a few places where :: will cause errors. For example, a FOR loop will error out with :: style comments. Simply fall back to using REM if you think you have a situation like this. Silencing Display of Commands in Batch FilesThe first non-comment line of a batch file is usually a command to turn off printing (ECHO’ing) of each batch file line. The @ is a special operator to suppress printing of the command line. Once we set ECHO’ing to off, we won’t need the @ operator again in our script commands. You restore printing of commands in your script with: Upon exit of your script, the command prompt will automatically restore ECHO to it’s previous state. Debugging Your ScriptsBatch files invole a lot of trial and error coding. Sadly, I don’t know of any true debugger for Windows batch scripts. Worse yet, I don’t know of a way to put the command processor into a verbose state to help troubleshoot the script (this is the common technique for Unix/Linux scripts.) Printing custom ad-hoc debugging messages is about your only option using the ECHO command. Advanced script writers can do some trickery to selectively print debugging messages, though, I prefer to remove the debugging/instrumentation code once my script is functioning as desired. Posted by Steve Jansen Mar 1 st , 2013 batch, scripting, shell, windows CommentsHi, I’m Steve. I’m a software developer loving life in Charlotte, NC, an (ISC) 2 CSSLP and an avid fan of Crossfit. And, no, I’m not Steve Jansen the British jazz drummer, though that does sound like a sweet career. GuidesRecent PostsSocial Stuff
Copyright © 2015 — Steve Jansen — Powered by Octopress |