Thomas Sampson


Leave a comment

Creating a new Windows Service

Sometimes it’s enough to add a program or script to the startup folder or scheduled a boot-time task, however if you need to setup a fully fledged Windows Service, the freeware NSSM (Non-Sucking Service Manager) tool can be used to quickly and easily install or remove Windows Services from the command line.

Creating a new Windows Service
The following command will pop-up a nice UI for you to select which program/script you want to run and allow you to tweak additional settings such as Automatic/Manual startup etc:

nssm install "YourNewServiceName"

Removing a Windows Service
The following command permanently un-registers and removes a Windows Service by name:

nssm remove "YourServiceName"
Advertisements


Leave a comment

Command Line Aliases/Macros

Preface

Entering long winded commands with many parameters, paths and redirection in the shell/terminal can be time consuming, repetitive and prone to human error. Luckily on both Windows and on Linux you can provide simple abbreviations for long commands.

Windows

On Windows the DOSKEY command (provided by default with the operating system) is pretty versatile and can be used to easily configure macros and command abbreviations. DOSKEY is actually resident during all Windows command line sessions and is the underlying system responsible for maintaining the command history (which can be navigate with the updown/pgup/pgdown keys). Below I have listed some example usages of DOSKEY;

# Note: The $G character is a special doskey escape character for the
#       redirect character '>' which allows it to be specified as part of
#       the command without redirecting the doskey command itself.

doskey /history                                       # Show history of all previously run commands
doskey /reinstall                                     # Clear command history
doskey here=explorer .                                # Add 'here' macro to open an explorer window at the current location
doskey desktop=cd "%USERPROFILE%\Desktop"             # Add 'desktop' macro which takes you to the current user's desktop
doskey mp3list=dir %CD%\*.mp3 /S /O:N $G mp3list.txt  # Add 'mp3list' macro to recursively and alphabetically list all .mp3
                                                      # files below the current directory, inside mp3list.txt

Note: Any macros configured using doskey are only active for the active command line session. To create macros which persist between sessions consider placing all macro creation commands in a batch script, you then have two options:

  1. Create a shortcut which opens the command line and runs your batch script immediately (shortcut location = %comspec% /k %path_to_script%)
  2. Use an alternative command line environment such as Console2 which can be configured to automatically run your batch script on start-up.

Linux/OSX

On Unix-esque systems it’s pretty much the same kind of setup using the alias command. Some example usages are listed below;

# Note: All alias entries are wrapped in single quotes so there is no requirement for special alias
#       escape characters for piping / redirecting streams

alias here='nautilus ${PWD}'                          # Add 'here' macro to open a nautilus window at the current location
type here                                             # Prints the expanded version of the 'here' macro
unalias here                                          # Unregister the 'here' alias
alias blog='firefox https://tomtech999.wordpress.com'  # Add a 'blog' alias to open your blog in firefox
alias dumpenv='export -p | grep "/" > env.txt'        # Dump all bash environment variables containing a path to 'paths.txt'

Note: As with DOSKEY on Windows, any macros configured using ‘alias’ are only active for the active bash shell session. The easiest way to make aliases persist between bash sessions is to add all of your alias commands to the bashrc shell configuration file located in your home directory (~/.bashrc ). This script is executed whenever you open a bash shell and can be used to automatically register aliases for all bash sessions.

References

Microsoft DOSKEY Documentation
10 Handy Bash Aliases
Hak5 Linux Temrinal 101 – Create Your Own Commands
30 Handy Bash Shell Aliases For Linux/Unix/MacOSX


Leave a comment

Visual Studio Post Build Events

I am currently working on a Visual Studio solution that has multiple projects each building an exe or library to a different directory. My aim was to copy some of the output files all into the same folder as the main application is dependent on the other projects residing in the same directory.

Previously, if I had updated and rebuilt one of the projects in the solution I would manually locate the output file and drag it into the right place. However today I discovered the post build events area in Visual studio (right click solution > Build events). Here you can input your own DOS commands and utilise a bunch of VS environmental macros/variables to create a simple batch script which is automatically executed every time you build/rebuild your solution. Any output from your DOS commands (echo) is printed into the ouput window inline with all the other build information Visual Studio provides during the build process.

Here is a simple example which solved my problem but there are much more advanced techniques you could use with this feature.

echo Copying pre loader application to build directory
copy "$(SolutionDir)cwpl_app\bin\Debug\CwplApp.exe" "$(OutDir)"
echo Launching pre loader tool
"$(SolutionDir)cwpl_app\bin\Debug\CwplApp.exe"


Executing multiple executables

I knew this could be achieved easily through a batch file (Windows only). I wanted to launch 3 programs at once, I would use this script at uni to launch a bunch of portable apps I use almost every logon session. I first tried the following, simply putting a different executable path on each line…

F:\MyWork\Apps\PidginPortable\PidginPortable.exe

F:\MyWork\Apps\FirefoxPortable\FirefoxPortable.exe

F:\MyWork\Apps\Notepad++Portable\Notepad++Portable.exe

It turns out this half worked, but wont laucnh the program on line 2 until program 1 closes. I then found that to get all the 3 applications to launch simultaneously the following code is necessary…

start “” “F:\MyWork\Apps\PidginPortable\PidginPortable.exe”
start “” “F:\MyWork\Apps\FirefoxPortable\FirefoxPortable.exe”
start “” “F:\MyWork\Apps\Notepad++Portable\Notepad++Portable.exe”
exit

( the exit command just closes the command file once the script has completed)