Tuesday, April 5, 2011

Cmd.exe - Practical examples

Cmd.exe - Practical examples
This is a collection of commands that I use and ways that I use cmd.exe on windows systems to get things done. I'll keep adding more stuff as we go.
The first thing I like to do is set up the command line interpreter with my preferences.
As I have a network I want my console to be portable so I use a shortcut file and edit the preferences to my liking.
Then I can just put the shortcut where I need it (like in the startup, userprofile, windir or thumbdrive).
The shortcut (console.lnk) launches cmd.exe (%CompSpec%) and runs doskey (check out doskey /?) with my custom macro file.


Here's how that can be done...

Create a shortcut named "console" with the following properties...


Target: %ComSpec% /k doskey /macrofile="%UserProfile%\macros.txt"
Start in: %UserProfile%
Quick Edit Mode: Checked
Insert Mode: Checked
Discard old duplicates: Checked
Screen Buffer Size- width: 9999
Let system position window: Unchecked

Create a macro file in the user folder...

1. Hit WinKey+R and type: notepad macros.txt
2. Type your favorite doskey macros and save the file.


Example "macros.txt" file...
(note: some lines have wrapped, spaces added to improve readability)
Code:
7z="c:\Program Files\7-Zip\7z.exe" $*
gvi="C:\Program Files\Vim\vim73\gvim.exe" $*
ff="c:\Program Files\Mozilla Firefox\firefox.exe" $*
goo="c:\Program Files\Mozilla Firefox\firefox.exe" "http://www.google.com/search?
btng=google+search&q=$*"
gool="c:\Program Files\Mozilla Firefox\firefox.exe" "http://www.google.com/search?
btnI=i'm+feeling+lucky&q=$*"
goom="c:\Program Files\Mozilla Firefox\firefox.exe" "http://www.google.com/search?
btnI=i'm+feeling+lucky&q=$*+imdb"
halt=shutdown -s -t 00
ie="c:\Program Files\Internet Explorer\IEXPLORE.EXE" $*
kmp="c:\Program Files\The KMPlayer\KMPlayer.exe" $*
macros=notepad "%userProfile%\etc\macros.txt"
md5="%userProfile%\bin\md5sum.exe" $*
unzip="c:\Program Files\7-Zip\7z.exe" -o%userProfile%\tmp\7z e $1
restart=shutdown -r -t 00
vi="C:\Program Files\Vim\vim73\vim.exe" $*
wget="%userProfile%\bin\wget.exe" $*


Now you should have a shortcut in your user folder (%userprofile%) called "console" (or what ever you like to use). The shortcut launches your customised version of cmd.exe with your favorite doskey macros loaded from the file "macros.txt" (also in your %userprofile% folder).

Now some cmds that I use...

View cookies
This will echo the name of the cookie, pause until a key is pressed then use "more" to display the contents of the cookie. It does this for all cookies in the "%userprofile%\cookies" directory (Ctrl+C to Abort).

Code:

for /f %i in ('dir/b "%userprofile%\cookies"') do @echo %i & pause>nul & @more "%userprofile%\cookies\%i"


Create a list of files by extension and open it in notepad.

Generating an alphabetized list of specific file types and exporting it as a text file is very easy on the command line...

Code:

dir/b  *.avi *.mkv *.mpg *.mp4|sort>>Movies.txt && notepad Movies.txt


Copy multiple filetypes to multiple locations

Actually this command will only list the files that would be copied to each location because I have included the "/l" switch in the xcopy command. The reason for this is that you will need to customize the locations (\\nas\public\movies d:\video\movies z:\movies) to suit your own needs, those 3 locations are just examples. Once you have edited the locations and get the desired ouput (files listed for copying), remove the "/l" switch from the xcopy command and run the new command.

Code:
for /f %e in ('dir/b  *.avi *.mkv *.mpg *.mp4') do @for %d in (\\nas\public\movies d:\video\movies z:\movies) do @xcopy/v/y/f/c/l %e %d