Rants and raves on (mostly) technology
10 Feb
If you’ve ever worked with DOS batch files then you know how basic (and frustrating) it is compared to other Unix scripting languages like bash.
You would think that creating a timestamp for a filename (e.g., 20080208-1117.log) would be an easy thing to do. After all, a batch file is used for automating tasks like backups or logging errors when problems are encountered.
But over the years, I’ve seen a lot of ugly solutions that either rely on 3rd-party programs or pipe data all over the place. So here’s a relatively clean solution that uses the DOS equivalent of a substring on the output from the date and time commands.
@echo off
cls
rem Timestamp Generatorrem Parse the date (e.g., Fri 02/08/2008)
set cur_yyyy=%date:~10,4%
set cur_mm=%date:~4,2%
set cur_dd=%date:~7,2%rem Parse the time (e.g., 11:17:13.49)
set cur_hh=%time:~0,2%
if %cur_hh% lss 10 (set cur_hh=0%time:~1,1%)
set cur_nn=%time:~3,2%
set cur_ss=%time:~6,2%
set cur_ms=%time:~9,2%rem Set the timestamp format
set timestamp=%cur_yyyy%%cur_mm%%cur_dd%-%cur_hh%%cur_nn%%cur_ss%%cur_ms%rem Do something with it
echo Timestamp: %timestamp%rem Clear the environment variables
set cur_yyyy=
set cur_mm=
set cur_dd=
set cur_hh=
set cur_nn=
set cur_ss=
set cur_ms=
set timestamp=
I say relatively because it’s dependant on the format of the date on your computer. This is usually defined by your Regional Settings in the Control Panel. So if you type ‘date’ on your Command Prompt and it looks like this “Sun 02/10/2008″ then this batch file will work. If not, you’ll need to adjust the substring positions.
21 Sep
If you’re used to using the DOS Prompt or Command Prompt in Windows then this is nothing new.
Command history editing provides quick access to a list of recently executed commands, also known as the history buffer. Commands in this buffer can be re-executed, or recalled to the command line and edited as desired.
Normally, I just use the UP/DOWN arrows to scroll through my recent commands. BUT here’s something I just discovered.
Pressing the F7 key also displays the command history buffer, except a popup window appears containing the contents of the history buffer so you can see your last 10 commands. Cool!