Hints and tips on cmd

These are some helpful hints on various topics.

 

Capturing windows command output directly into clipboard

Windows command prompt is famous for plenty of disadvantages. One very annoying "feature" is that it breaks longer lines and if you want to copy them you need to manually glue them back. Here's one convenient way of capturing command output directly into clipboard: just type
| clip 

after the command. For example
netstat -an | clip

Now you can paste it in your favorite text editor.

/dev/null in Windows


Analogue for the /dev/null file on Windows is NUL. So the command
 
./my_prog 2>/dev/null
translates to
 
my_prog.exe 2>NUL

Getting full directory path in bash and cmd prompt

To get the absolute path for the current or any other realtively addressed folder, for example the parent, use the following constructs:

bash

parent_dir=`cd .. && pwd`

cmd


set current_dir=%CD%
cd ..
set parent_dir=%CD%
cd %current_dir%


Printing empty variables in DOS/cmd with echo+

When printing variables on the screen in cmd shell we use echo. However echo has another purpose - it toggles on and off typing output. This however causes strange behavior when printing empty variables. For example the following code does not produce an empty line, but rather "Echo is ON":

(set VAR=)
echo %VAR%

To work around this, put plus sign rigth after echo:

(set VAR=)
echo+%VAR%