Programming4us
         
 
 
Windows

Windows 7 : Working at the Command Line (part 1)

12/5/2010 11:45:25 AM
When you have your command-line session up and running, you can run commands and programs, create and launch batch files, perform file maintenance, and so on. If you haven’t used the Command Prompt since the days of DOS, you’ll find that the Windows 7 Command Prompt offers a few extra command-line goodies. The next few sections highlight some of the more useful ones.

Caution

When you’re working in the Command Prompt, be warned that any files you delete aren’t sent to the Recycle Bin but are purged from your system.


Running Commands

Although many of the Windows 7 accessories provide more powerful and easier-to-use replacements for nearly all commands, a few commands still have no Windows 7 peer. These include the REN command, as well as the many Command Prompt–specific commands, such as CLS, DOSKEY, and PROMPT.

Note

Command-line commands that exist as separate executable files—such as CHKDSK, DEFRAG, and XCOPY—are called external commands; all other command-line commands—such as DIR, CD, and CLS—are part of the CMD shell and are known as internal commands.


How you run a command depends on whether it’s an internal or external command and on what you want Windows 7 to do after the command is finished. For an internal command, you have two choices: You can either enter the command at the Command Prompt, or you can include it as a parameter with CMD. As you saw earlier, you can run internal commands with CMD by specifying either the /C switch or the /K switch. If you use the /C switch, the command executes, and then the command-line session shuts down. This is fine if you’re running a command for which you don’t need to see the results. For example, if you want to redirect the contents of your user profile folder to the text file profile.txt, entering the following command in the Run dialog box (for example, press Windows Logo+R to open Run) will do the job:

cmd /c dir %userprofile% > %userprofile%\profile.txt

On the other hand, you might want to examine the output of a command before the Command Prompt window closes. In that case, you need to use the /K switch. The following command runs DIR on your user profile folder and then drops you off in the Command Prompt:

cmd /k dir %userprofile%

For an external command, you have three choices: Enter the command at the Command Prompt, enter the command by itself from within Windows 7, or include it as a parameter with CMD.

Note

When you use the Command Prompt or the Run dialog box to start an external Command Prompt command, you don’t need to use the command’s full pathname. For example, the full pathname for mem.exe is %SystemRoot%\System32\mem.exe, but to run this command, you need only enter mem. The reason is that the %SystemRoot%\System32 subfolder is part of the PATH statement for each command-line session.


Entering a command by itself from within Windows 7 means launching the command’s file in Explorer, entering the command in the Run dialog box, or creating a shortcut for the command. For the latter two methods, you can embellish the command by adding parameters and switches.

Working with Long Filenames

If you want to use long filenames in a command, you need to be careful. If the long filename contains a space or any other character that’s illegal in an 8.3 filename, you need to surround the long name with quotation marks. For example, if you run the following command, Windows 7 tells you this: The syntax of the command is incorrect:

copy Fiscal Year 2009.doc Fiscal Year 2010.doc

Instead, you need to enter this command as follows:

copy "Fiscal Year 2009.doc" "Fiscal Year 2010.doc"

Long filenames are, of course, long, so they tend to be a pain to type in the Command Prompt. Fortunately, Windows 7 offers a few methods for knocking long names down to size:

  • In Explorer, drag a folder or file and drop it inside the Command Prompt window. Windows 7 pastes the full pathname of the folder or file to the end of the prompt.

  • In Windows Explorer, navigate to the folder you want to work with and then select and copy the folder path in the address bar. (To see the address instead of the folder breadcrumb path, click an empty section of the address bar.) Return to the Command Prompt window, type the command up to the point where you want the path to appear, right-click the tile bar, and then select Edit, Paste.

  • If you’re trying to run a program that resides in a folder with a long name, add the folder to the PATH. This technique enables you to run programs from the folder without having to specify the full pathname.

    Tip

    To edit the PATH environment variable, you have two choices. At the command line, enter the following command (where folder is the path of the folder you want to add to the PATH variable):

    path %path%;folder

    Alternatively, select Start, right-click Computer, click Properties, and then click Advanced System Settings. In the Advanced tab, click Environment Variables. In the System Variables list, click Path, click Edit, and then append the folder to the end of the Variable Value string. Be sure to separate each folder path with a semicolon (;).


  • Use the SUBST command to substitute a virtual drive letter for a long pathname. For example, the following command substitutes the S: drive for the Start menu’s System Tools folder:

    subst s: "%AllUsersProfile%\Start Menu\Programs\Accessories\System Tools"

Changing Folders Faster

At the command line, you use the CD command to change to a different folder on the current drive. However, the Command Prompt has a few short forms you can use to save time.

You might know that both the Command Prompt and Windows 7 use the dot symbol (.) to represent the current folder, and the double-dot symbol (..) to represent its parent folder. You can combine the CD command and the dot notation to jump immediately to a folder’s parent folder, or even higher.

To make this more concrete, suppose that the current folder is D:\Animal\Mammal\Dolphin. Table 1 demonstrates the techniques you can use to navigate to this folder’s parent, grandparent (two levels up), and great grandparent (three levels up) folders.

Table 1. Combining the CD Command with Dot Notation
Current FolderCommandNew Folder
D:\Animal\Mammal\DolphinCd..D:\Animal\Mammal
D:\Animal\Mammal\DolphinCd..\..D:\Animal
D:\Animal\Mammal\DolphinCd..\..\..D:\
D:\Animal\Mammal\DolphinCd..\BaboonD:\Animal\Mammal\Baboon

Tip

If you want to return to the root folder of any drive, type cd\ and press Enter.


Other -----------------
- Windows 7 : Getting to the Command Line (part 2) - Running CMD
- Windows 7 : Getting to the Command Line (part 1)
- Windows Azure : Programming Access Control Service (part 10) - Deploying the Web Service in Windows Azure
- Windows Azure : Programming Access Control Service (part 9) - Configuring a Web Service Client to Acquire and Send SAML Tokens
- Windows Azure : Programming Access Control Service (part 8)
- Windows Azure : Programming Access Control Service (part 7) - Integrating ACS with a SAML Token Provider
- Windows Azure : Programming Access Control Service (part 6)
- Windows Azure : Programming Access Control Service (part 5)
- Windows Azure : Programming Access Control Service (part 4)
- Windows Azure : Programming Access Control Service (part 3)
- Windows Azure : Programming Access Control Service (part 2)
- Windows Azure : Programming Access Control Service (part 1)
- Windows 7 : Working with Registry Entries (part 3)
- Windows 7 : Working with Registry Entries (part 2)
- Windows 7 : Working with Registry Entries (part 1) - Changing the Value of a Registry Entry
- Windows 7 : Keeping the Registry Safe
- Windows 7 : Getting to Know the Registry (part 2)
- Windows 7 : Getting to Know the Registry (part 1) - Understanding Registry Settings
- Windows 7 : Firing Up the Registry Editor
- Windows Azure : Managing Access Control Service Resources (part 2)
 
 
Most View
- Windows 7 : Creating a Network Location for a Remote Folder
- Exchange Server 2007 : Create Exchange Administrative Roles
- Windows Vista - Sharing Files and Folders : Accessing a Shared Folder
- Developing Applications for Windows Phone 7 : XAML Styling (part 1)
- Performing SharePoint 2010 Installations (part 1) - SharePoint 2010 Standalone Installation
- Windows Azure : Exploring Full-Text Search (part 2) - Building an FTS Engine on Azure
- Text Tags and a Little CSS3 - Adding Style to Text with CSS3 (part 2) - Creating CSS3 classes and IDs
- Windows Phone 7 : Searching Marketplace
- Microsoft ASP.NET 4 : Ajax - Extender Controls (part 2) - A Modal Pop-up Dialog-Style Component
- Upgrading to SQL Server 2008 : Upgrading Other SQL Server Components
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS