Friday, November 27, 2009

Commands that everybody can use

Since the command prompt is often used by systems administrators, many of the commands are specialized for networks and administration. However, the average PC user is not left out; there are some powerful commands for everyday tasks that anybody will find useful. Here we discuss some examples.
For convenience in the examples below, I will use simple names for files and folders without indicating the full path. Please remember that paths for all commands are relative to the folder indicated in the command prompt itself, unless explicitly indicated otherwise. The default reference folder is usually C:\Documents and Settings\(log on name)\. Thus if a reference is made to a file "somefile.txt", the computer assumes you mean C:\Documents and Settings\(log on name)\somefile.txt. If what you really want is C:\someotherfolder\somefile.txt, then you must enter the entire name with the correct path. Note that paths containing spaces have to be enclosed in quotes. A convenient way to be able to open the command prompt in a folder of your choice can be obtained by installing the Microsoft PowerToy “Command Prompt Here”. The right-click context menu will then contain an entry for opening a command prompt in any selected folder. The accessory can be downloaded here. Another way to get file names into the command line without having to type a long path is by using drag and drop. Open a command window and enter the command you want with a space after it. Then use Windows Explorer to open the folder containing the file you want to use. Drag the file over to the command window and drop it. (Drag and drop does not work in Vista.)
Power deleting with the expanded "Del" command
The delete command “del” now has a switch “/s” that provides for deletions in subfolders. Thus the commanddel /s myfolder\* will delete all files in myfolder and all files in any subfolders of myfolder. Note the convenient asterisk wildcard “*”, which allows for multiple deletions in a single user operation. Together with the switch “/s”, a single “del” command can clean out Temp folders and do other useful housekeeping chores. Another switch "/f" will force the deletion of read-only files. If you are sure about what is being deleted, add the “/q” switch to run in quiet mode so you aren’t asked if you really want to delete for every single file. Because of its power, this extended del command has to be used carefully. Any files deleted this way do not go into the Recycle Bin but are permanently removed.
To delete only files with a particular extension EXT, use the commanddel /s myfolder\*.ext Note that this command will delete all files of a particular type in a folder and all its subfolders. One example of its use is to get rid of all temporary files with the extension TMP.
Enhanced management of folders (directories)
The command "rmdir" or its twin "rd" are also expanded compared to the old DOS version. To remove folders (directories), use “rmdir” (just “rd” will also work). Folders must be empty before they can be deleted. However, there is a switch “/s” for deleting subfolders and with this switch files are also deleted. It is like the old command "deltree". The command rd /s myfolder will remove "myfolder" and everything in it. Adding the switch /q for a "quiet" mode is also possible.
Options have also been added to the command for making folders. The command “mkdir” or “md” can now make a whole tree. For example, consider the commandmd new\new1\new2 This will create a folder “new” with a subfolder “new1” that in turn has a subfolder “new2”.
Xcopy- Powerful and versatile way to copy or back up files
For copying large numbers of files or backing up a large folder, the command line is often the fastest and easiest way to go. The command “xcopy” is a very useful and powerful command for this purpose. (The old DOS also had an “xcopy” command but it sometimes had trouble with long file names. The XP version has no such problems.) “xcopy” comes with an alphabet soup of assorted switches that give it great versatility for use as a file backup utility. Enter “xcopy /?” in a command prompt to see them all. For example with the switch “/d:[mm-dd-yyy]” only files changed after a given date are copied. A command that will copy all files from the folder myfolder that have changed since June 1, 2003 to the folder mybackup is given byxcopy myfolder mybackup /d:06-01-2003 If no date is specified the switch “/d” will copy all files that have changed at any time.As is true in general, if there are spaces in a name, the path and file name have to be enclosed in quotes. Adding the switch "/s" provides that subfolders and their contents will also be copied. Other switches provide for read-only and hidden files. Altogether, there are something like 27 command line options for xcopy. More information is on this page.
How to list files with "dir"
The "dir" command is not new but it remains one of the most useful for average PC users. If you have ever wanted to make a list of all the files contained in a particular folder, you will have discovered that the Windows GUI provides no easy way to do this. It is very straightforward, however, when using the command line. The command “dir myfolder” will list the files and folders contained in myfolder. Again, there are switches that provide for various modifications of the command. For example “/h” will show hidden files and “/s” will list the contents of sub-folders in addition to those of the main folder. Of course, the normal output of the command is to the screen. To output to a file instead of the screen, the command isdir myfolder >listmyfolder.txt where “>” is the redirection symbol. The file "listmyfolder.txt" will be created by the command. Output can also be redirected straight to a printer on the LPT1 port but I think it is better to first create a file and then print from there if hardcopy is desired. These lists can be a lot longer than you might think. (These days most printers are on USB, anyway.) This use of dir is also available in Windows 98/Me.
The dir command can also be used to list only files with a given extension. For exampledir myfolder\*.doc /s >listmyfolder.txt will list all Microsoft Word files in myfolder and its subfolders. This form of the command will also list all the directories and sub-directories. If you want a list of filenames only, add the switch /b. The filename will include the full path.
Renaming files with "ren"
Renaming a a large number of files can be tedious. The command ren (also written rename) is somewhat limited but its ability to use wild cards can sometimes be useful. The basic command is ren file1 file2 The renamed file has to stay in the same folder as the original; this command cannot move files to another folder. The wildcard capability can be used to change the extension of all files of a certain type. Thus ren *.txt *.doc will rename all text files to have a DOC extension.
Moving files
The command "move" takes a file from one folder and puts it in another. Its ability to use wild cards makes it useful for moving all the files of a given type to another location. A command of the typemove /y folder1\*.mp3 folder2\will move all MP3 files from folder1 to folder2. The switch " /y" is used if you want to prevent the system from asking if it should overwrite existing files of the same name. To prevent overwriting, use the switch "/-y".
Changing file attributes with "attrib" In addition to their actual content, files also have a set of properties that characterize them (sometimes called metadata.) One set of these properties are the four attributes read-only, hidden, system, and archive. The archive attribute is primarily used in backup procedures but the other three can be encountered in various contexts. These attributes can be turned on or off with the command "attrib" and its switches. The great utility of this command is that it can act on subfolders with its switch "/s" and can use the wildcard "*". For example, to clear the read-only , system, and hidden attributes from a file use attrib -r -s -h somefile This operation is not uncommon when system files have to be edited. To restore the attributes the command is attrib +r +s +h somefile A common situation where it is desirable to clear the read-only attribute from many files is when files are copied from a CD. By default CD files are normally marked read-only. This can interfere with editing. To clear the read-only attribute from all files in a folder and its subfolders use attrib -r somefolder\*.* /s If you want to process all files of a certain type such as MP3, use attrib -r somefolder\*.mp3 /s
How to make your favorite folder easily accessible in Windows dialog boxes
Another older command that I find handy is "subst". There are certain folders that I use over and over and I like to have ready access to them. One way to do this is to use the command “subst” to assign a drive letter to a folder. Since drives are at the top of My Computer and any folder trees in browse lists, it makes the folder very easy to get to. To map the “Z:” drive to a folder "myfolder", entersubst z: myfolder Unless you are working from the parent folder of "myfolder" you will need the full path for it. The assignment only lasts until the user logs off or the computer is shut down so I have a one-line batch file in my Startup folder that reinstitutes the assignment.
Other commands
There are so many commands that we can only give the briefest sketches here. A list of some that I think might be useful on home computers is given here. I urge you to look into as many as possible since I feel you will be surprised at how useful some can be. For example, there are a whole host of commands for checking network functions and for use on the Internet. (Many have names that begin with “net…”) There are new ones as well as versions of the well-known commands such as “ping” and “tracert”. As more and more people have home networks, these commands are assuming greater significance to the average PC user. Go to this page for more discussion of some networking commands.
Useful command line programs in scripts
Average PC users are not always comfortable using the command line and I have used VBScripts to wrap some of the examples discussed above with an easy-to-use graphical interface. Description of the free scripts and instructions for their use and downloading are at a sister site.

No comments:

Explaining DNS Concepts - DNS Servers-DNS Queries-DNS Records

3 types of DNS queries— recursive, iterative, and non-recursive 3 types of DNS servers— DNS Resolver, DNS Root Server and Authoritative Name...