Get Yesterday’s date in MS DOS Batch file

A useful snippet of DOS Batch Scripting to get yesterday’s date (or any other historical date for that matter). Very useful to then use with other DOS commands like xcopy or forfiles.

Linux though, it’s a hell of a lot user, just add options to the find command

@echo off
set yyyy=
set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))
if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100
set CurDate=%mm%/%dd%/%yyyy%
set dayCnt=%1
if "%dayCnt%"=="" set dayCnt=1
REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100
:CHKDAY
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1
:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through
:SET31
set /A dd=31 + %dd%
goto CHKDAY
:SET30
set /A dd=30 + %dd%
goto CHKDAY
:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
:SET28
set /A dd=28 + %dd%
goto CHKDAY
:SET29
set /A dd=29 + %dd%
goto CHKDAY
:DONE
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%
REM Set IIS and AWS date variables
set IISDT=%yyyy:~2,2%%mm%%dd%
set AWSDT=%yyyy%-%mm%-%dd%

PowerCram: Get Yesterday’s date in MS DOS Batch file.

BASH Shell: For Loop File Names With Spaces – nixCraft

I’ve been wondering how to do this. I didn’t realise I could do this in so many different ways. Simplest way, I think:

$ ls
2013.doc externalIP.txt Introduction to Compositing in Blender.txz
externalIP.sh Ideas.odt Piano to Learn
$ for a in
ls *; do echo $a; done
2013.doc
externalIP.sh
externalIP.txt
Ideas.odt
Introduction
to
Compositing
in
Blender.txz
Piano
to
Learn
$ SAVEIFS=$IFS
$ IFS=$(echo -en “\n\b”)
$ for a in ls *; do echo $a; done
2013.doc
externalIP.sh
externalIP.txt
Ideas.odt
Introduction to Compositing in Blender.txz
Piano to Learn
$ IFS=$SAVEIFS
$ for a in ls *; do echo $a; done
2013.doc
externalIP.sh
externalIP.txt
Ideas.odt
Introduction
to
Compositing
in
Blender.txz
Piano
to
Learn
$

BASH Shell: For Loop File Names With Spaces – nixCraft.

Snippet: NTP Syncing (Windows/Linux)

Tux, the Linux penguin

English: M in blue square (similar to seen on )

A quick snippet for syncing your date and time via NTP. I have noticed that Windows and Linux do not follow the same convention by standard, and are always an hour out from each other, even though both claim to follow the same time zone. So, what I am having to do is sync via NTP each time I dual boot.

In Linux, this can be done using cron jobs or using the NTP daemon, but that does not do it frequently enough for my liking. So here is a bash snippet for it:

sudo service ntp stop
sudo ntpdate 0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org 2.ubuntu.pool.ntp.org 3.ubuntu.pool.ntp.org 0.uk.pool.ntp.org 1.uk.pool.ntp.org 2.uk.pool.ntp.org 3.uk.pool.ntp.org ntp.ubuntu.com
sudo service ntp start

The first line stops the NTP daemon, since the ntpdate command does not like it when it is running (port in use). The second command uses a server in the selected list to sync with. The final line restarts the NTP daemon.

The Windows (Windows 7) equivalent is very similar. Like with linux, it has an in-built sync facility, but it again does not sync often enough for my liking. Like with the bash script, the commands must be run with elevated rights, so you must “Run as Administrator“, or run from an elevated rights command prompt, which you do as follows:

  1. Click Start, type “cmd” into the command window (do NOT use Windows+R)
  2. Hold down CTRL+SHIFT, then press ENTER
  3. You will be prompted (if you have UAC active), OK it and you will get a command prompt with “Administrator” in the title.

net stop "Windows Time"
net start "Windows Time"
w32tm /config /manualpeerlist:"0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org 2.ubuntu.pool.ntp.org 3.ubuntu.pool.ntp.org 0.uk.pool.ntp.org 1.uk.pool.ntp.org 2.uk.pool.ntp.org 3.uk.pool.ntp.org ntp.ubuntu.com" /syncfromflags:ALL /update
w32tm /resync

This code starts/restarts the Windows Time service then configures it with a pool of NTP servers, before asking the service to update itself and then resync. The resync action is what adjusts the time.

Laptops, TuxOnIce and Hibernation

I’m one of those people who hates having to shutdown machines, then restart them, and start logging into all my sites all over again, so I’m particularly thankful for hibernation functionality.

On Ubuntu (possibly Debian as well, but I haven’t checked), you can install either (or both) of the hibernate package, or the TuxOnIce-enabled kernel.

Hibernate is a script that detects whether or not you have a TOI-enabled kernel, and if you have such a kernel, it will use the TOI routines.

Hibernate worked perfectly for me, until I started using BOINC. Then, hibernation would hang with my laptop in a “limbo” state. Neither fully on, nor fully powered off. Turns out that BOINC must be either hogging the memory, or not releasing it properly. So, instead of doing

sudo hibernate

I do this

sudo service boinc-client stop
sudo hibernate -k
sudo service boinc-client start

So I stop the BOINC service (freeing up memory and CPU cycles), then I do the hibernate (allowing it to kill processes if needed), and then I startup the BOINC service again. The last line only gets executed upon resuming.

Grive & Fedora — Working

Grive is an open source command-line-based sync tool to synchronise a directory with your Google Drive. Grive is not in the Fedora repositories, although it is undergoing review for addition into the repositories.

In the meantime, if you want to use it, I’ve written a script that should pick the latest version from the russianfedora website. Bitbucket repository is here.

Direct link to script: here

Once you install the application, create a blank folder (this will be the sync folder), then run
grive -a
to request an authentication URL. Go to that URL, log into Google if you need to, and you’ll get a response string you need to copy back into the console window. If the authentication was successful, Grive will sync your files into the folder. Each subsequent time you run Grive, it will download and/or upload files to/from your Google Drive.

There is only one limitation that I’m aware of. Documents created from within Google Drive won’t sync, but if you convert them to odt or doc files, they will sync.

Snippet: Chromium Snapshot Download (Cygwin) (Updated 3rd April)

A snippet to download and run the latest build of the Chromium browser. Meant for Cygwin/Windows but can be adapted if needed.

If you need to use a proxy, set your http_proxy and https_proxy variables before using this.

Now updated to kill running Chrome/Chromium processes before running the installer (in case of locking issues). Also created a Bitbucket repo here for this script

if [ -f ./mini_installer.exe ]; then
  echo "WARNING: Previous script run did not clean up"
  rm ./mini_installer.exe
fi

if [ ! -f prev ]; then
  echo "No previous build logged"
  echo "-1" >prev
fi

if [[ `wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-` == `cat prev` ]]; then
  echo "No build change (`wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-` = `cat prev`)"
  exit 1
else
  echo "New build (previous: `cat prev`, new: `wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-`)"
fi
echo Downloading http://commondatastorage.googleapis.com/chromium-browser-snapshots/`wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-`/mini_installer.exe
wget http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/`wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-`/mini_installer.exe

if [[ `ps -W | grep chrome | wc -l` != 0 ]]; then
  echo "`ps -W | grep chrome | wc -l` Chrome Processes Running. Attempting to Kill"
  for a in `ps -W | grep chrome | awk '{ print $1}'`
    do
      echo Killing PID $a
      /bin/kill -f $a #This is the cygwin kill, not the bash kill
    done
else
  echo "No Chrome Processes Running"
fiecho "Running installer"
chmod +x ./mini_installer.exe
./mini_installer.exe
echo Done
wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O prev
echo "New build recorded (`cat prev`)"
rm ./mini_installer.exe

Bash Snippet: basename

I love linux and bash scripting. Whilst I am no expert, I really love the way you can pipe one application’s output into another.

One thing I don’t like to much about linux, however, is the case-sensitivity. “file.ext” is not the same as “file.EXT”, for example. On Windows, it doesn’t much care about the case of the extension, but on linux, it does. And therein is my problem.

My digital camera takes pictures and gives them a .JPG extension, which doesn’t show up on listings on my linux box where the application is looking for .jpg extensions. Sure, I can rename them manually, or macro together a simple bash script, but when you have to do this repeatedly, it gets quite frustrating.

So I did some research, and found out about the basename application. It serves two purposes. It strips out directory information to leave just the file name so that “dir1/dir2/dir3/file” becomes just “file” and optionally, allows you to strip out a suffix from the name, so for example to rename all .JPG files to .jpg in the current folder, I would use this:

for a in *.JPG
 do
   mv $a `basename $a .JPG`.jpg
 done

Here’s an example output

$ ls
file1.JPG file2.JPG file3.JPG

$ for a in *.JPG
> do
> echo mv $a `basename $a .JPG`.jpg
> done
mv file1.JPG file1.jpg
mv file2.JPG file2.jpg
mv file3.JPG file3.jpg

$ for a in *.JPG
> do
> mv $a `basename $a .JPG`.jpg
> done

$ ls
file1.jpg file2.jpg file3.jpg
%d bloggers like this: