Top 3 Products & Services
Dated: Oct. 01, 2011
There are numerous reasons that batch scripting can help users get a more full experience out of Microsoft Windows 7. They can be used to automate tasks just as easily as they can contain long commands that are not easy to work around. By starting with more simple models, a programmer can usually get the feel for what batch scripts are supposed to look like. That being said, anyone who has some experience with the command line can essentially take a group of commands that they've entered and use them as a makeshift batch script with no further editing.
The most basic batch files simply include a command that is too long to constantly type in at the command prompt. For instance, one could use one to create a somewhat executable file to reboot a machine. Creating a text file with the .bat extension that holds the command shutdown -r -t 01 will do this. Likewise the text shutdown -s -t 01 will do the same to turn off a computer. While these are useful solutions for many situations, small bits of code will not create something as sophisticated as an automated backup system. That doesn't make them useless, of course, but many of these types of commands can also be entered into Windows shortcuts that take advantage of the Windows 7 interface.
Still, there is a small trick when coding from the command line that can be a lot of help. Learning the old device names that MS-DOS used is actually more useful in Windows 7 than most people might think. The con device represents the console, and typing copy con filename.bat will allow one to create a batch file straight from the command line. This is a quick and dirty technique, but it is very useful for getting code out quickly. Each line is entered separately when using it, and when one is done coding they add a CTRL+Z to mark the end of file location.
A few other device names can be helpful. Scripts that need to automate printing of various results on older printing devices might find that redirecting output to the prn device is helpful. One could take a command and send it directly to the device, such as dir /w > prn to print a directory listing. Perhaps one of the most useful is the so-called bit bucket. Though using @echo off will suppress most messages, it will not contain output from commands in particular. They can be rendered totally silent by redirecting them with > nul.
For those who are trying to set up some sort of copying algorithm, though, understanding the %homedrive% and %homepath% variables is paramount. The %homedrive% usually points to the main mounted hard disk that the operating system has to deal with. On single user Windows 7 installations, this is generally the C: drive. Likewise, the %homepath% is defined as some particular path that the operating system has designated a user's home. This is often organized into a directory structure under the master Documents and Settings tree, though a system administrator can change it.
To put this sort of variable manipulation to work, the xcopy command is usually invoked. Information from the ACL and file audit settings can be copied with the /o and /x switches. There are several ways that the xcopy command can be used as a way to escape certain CRC errors, and it is generally more robust than the basic internal copy command. Still, that isn't to say that copying or moving files is the only thing that these variables are useful for. In fact, the same wildcard and variables can be used to alter the attributes of files with the attrib command. This might be useful for removing the archive attribute after copying by using the attrib -a switch. A combination of the xcopy and attrib command can thus do the job that a formal backup utility would have otherwise.
One other thing to consider is commenting. Some programmers believe one should comment every line, but this probably is not the best idea when writing batch scripts. Nevertheless, a few comments can provide a guideline when trying to edit code in the future. The rem command, which generally needs to be entered on an individual line, allows people to enter comments into batch files. Everything after the rem command is ignored, and therefore, it can also be a useful method to comment out code. If something appears to be malfunctioning, placing a rem command before it renders it innocuous without deleting it. This can save a lot of time when debugging particularly long scripts that are used in enterprise scenarios.
The robust file copy directory replication command, better known as robocopy, is a good place to start for anyone who is trying to learn advanced scripting techniques. Power users who have worked with previous versions of NT core Windows operating systems might be familiar the command from the Windows resource kit. However, it wasn't a standard feature until Windows Vista. The version provided with Windows 7 does some pretty unique things.
While most users are probably not going to need it, the robocopy command that comes with Windows 7 added multithreading support. More than one thread can be used to copy files, and this is very useful for those that want to automate web server backup. Since most drives that batch scripts will be working with are NTFS, the ability to preserve an access control list and owner information will be appreciated. The /COPYALL code switch is used to preserve this information, and timestamps. With the /DCOPY:T function, one can even copy directory timestamps. Specifying individual files are also possible, and should be coded with the /COPY: switch.
The actual help switches that are included with many commands should never be overlooked as a source of information. Working from the command line is something that just about every power user has done, but they probably forget that many of these commands have help systems built in. This help support vastly predates the graphical user interface help menu programs many users associate with computer help. Usually, adding a /? or -? switch to a command should bring up the loaded help page. It could even be possible to redirect these into files to make a ready command reference.
Many developers who are experienced with working on other operating systems will probably find that some commands no longer work for them. Microsoft has been changing the way that the command line works, and certain commands have been removed. Technically, certain commands have been rewritten to run as 32-bit applications. Those that are still run in 16-bit environments are emulated under the virtual DOS machine. The worst part, though, is that 64-bit processors simply cannot handle 16-bit instruction sets. Therefore, the command lines on these platforms will seem to be anemic at times.
The best way to learn more about batch file programming is to mess around. Find out what activities are the most common chores for a particular computer and write a batch file to speed them up. Necessity, after all, is the mother of invention and what starts as a small pet project can become extremely sophisticated in a short amount of time.
Now that you've gotten free know-how on this topic, try to grow your skills even faster with online video training. Then finally, put these skills to the test and make a name for yourself by offering these skills to others by becoming a freelancer. There are literally 2000+ new projects that are posted every single freakin' day, no lie!