Code Scraps: Command Line manual logging

I have a collection of eBooks that I’ve bought over the years, and thanks to sites like HumbleBundle, my collection is growing on a fairly regular basis. I keep them nicely organised in a network share. Each book (often with multiple formats – pdf, mobi, epub, etc) is stored in an individual directory with a cover image for neatness as I’m that fancy.

I have such a number of eBooks now, that I struggle sometimes to remember what I have, which proves equally annoying when in a book store. There has been occasions when I have gone and bought a copy of a book I already had, either in digital or paperback form.

So, I decided I needed to expose a list of eBooks I have via my home server to the outside world.  My home server already serves a personal cloud instance, so it wouldn’t be difficult to do.

My goal:

  1. Produce a list of directories on an internal server (my home desktop with my eBook collection).
  2. Get the list from the internal server to the external server (my personal cloud).

Step 1:

A quick batch script to index the eBooks directory and write out the content to a file is what I needed, so firstly I created a batch file containing:

@ECHO off
SET Library=L:\eBooks
SET IndexFile=C:\TEMP\LibraryIndex.txt
CD %Library%
DIR /b > %IndexFile%
ECHO Exported library to %IndexFile%
PAUSE

This simple batch scripts makes sure the running context is within my eBooks directory, then outputs a list of files to a text file in my C:\TEMP directory* (*A directory that you would need to create yourself, as C:\TEMP isn’t usually created with Windows Installs any more – alternatively you could use the system temp variable to specify this).

On running the above code against my eBooks directory, I would get a text file containing something along the lines of:

Springer – Archaeoastronomy
Springer – Earths of Distant Suns
Springer – Planetary Vistas
Springer – The Great Canoes in the Sky
Springer – The Lost Constellations
Springer – Weird Astronomy
O’Reilly – 21st Century Robot
O’Reilly – Atmospheric Monitoring with Arduino
O’Reilly – Vintage Tomorrows
Callisto Media – Philosophy
Callisto Media – Psychology – Essential Thinkers Classic Theories and How They Inform Your World
Callisto Media – Real BBQ – The Ultimate Step-by-Step Smoker Cookbook
Callisto Media – Sushi at Home – A Mat-to-Table Sushi Cookbook

Each line in the text file being the name of a directory in my eBooks directory. Using the /b switch on the DIR command meant that the command only output just the names of the files/directories in my eBooks directory and nothing else. This is a handy tip should you ever need a list of files/folders in a directory quickly.

Step 2:

I now have a list of the books in my eBooks directory, but I need at list visible to the outside world. I already have a web server in my home network I can use, so now I just need to add a process to get the file to the server. I accomplished this by using an SMB network share that both servers could reach. The share itself resided on a secure Linux server within the household network that both my desktop PC and the personal cloud server could reach.

The modifications to my batch file looked like:

@ECHO off
SET FileServer=192.168.1.2
SET Library=L:\eBooks
SET IndexFile=C:\TEMP\LibraryIndex.txt
CD %Library%
DIR /b > %IndexFile%
ECHO Exported library to %IndexFile%
XCOPY %IndexFile% \\%FileServer%\share\library.txt* /Y
PAUSE

The XCOPY command copies the file to the Samba share on *.2 on my home network. The personal cloud then has a cron job which runs periodically to copy the file from the share to the web directory, at which point I can then access the file via web browser in or out of the house.

Important Safety Advice

Please be aware though, that you should take great care when copying files programmatically from a Samba share.

If anyone in my household network tampered with the library.txt file on the share it would be copied to the web server without any safety checks, potentially opening up a way to compromise the personal cloud server.  If you plan on doing something similar, consider using SFTP or a similar secure transfer protocol to get your files onto the server as a way to reduce risk.

I personally am not overly concerned with this risk in my home network. The share being copied to is user account protected, authentication is required to access the share and I’m willing to accept the risk. Whenever you do something like this remember to stop and consider the possibility that someone else might have access to your network and what they could do.

Final Addition

I created this entry because I had been running this index for over a year now, and today I decided I needed to add something additional to my library listing – a date time stamp. This hadn’t occurred to me before, but usage of a date time stamp allows for usage of this code for logging purposes too.

So here is my final code:

@ECHO off
SET FileServer=192.168.1.2
SET Library=L:\eBooks
SET IndexFile=C:\TEMP\LibraryIndex.txt
CD %Library%
ECHO The Library as of %date% %time% > %IndexFile%
DIR /b >> %IndexFile%
ECHO Exported library to %IndexFile%
XCOPY %IndexFile% \\%FileServer%\share\library.txt* /Y
PAUSE

There is now an additional ECHO command in the middle of the script, which writes out the sentence:

The Library as of 04/07/2017 11:06:40.27

The %date% and %time% variables are system variables – these print out in your regional localisation. So for myself they print in UK date format (Day, Month, Year). On a US localisation it would display the standard format for the US (Month, Day, Year).

You will notice the ECHO command uses a single greater than > chevron to output to the text file, but the DIR command now uses two greater than’s >> chevrons. A single greater than chevron will erase the existing content of the file if it exists already before writing. A double chevron appends to the content. We don’t want the DIR command erasing the time stamp we just created, so we append to the content.

So that is quite a long winded write up on a fairly simple task. Hopefully you may find some of this useful.

Leave a comment if you have any thoughts or questions below!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.