Windows batch file - calculating filename from current date - command-line

I would like to create an environment variable to hold a filename something like:
PREFIX-2010-AUG-09.zip
I can get close if I use something like this:
SET filename=PREFIX-%date:~-4,4%-%date:~-7,2%-%date:~0,2%.zip
Result:
PREFIX-2010-08-09.zip
but in this case, I get the month as two digits (08).
Is there any easy trick in Windows batch files to get the three-letter month abbreviation from the numeric month (e.g. 08 for "AUG" = August) ??
Update: this needs to be run on a Windows 2008 R2 Server, and yes, if someone can show me a PowerShell solution, that would work, too :-) Thanks!

This is something like a look up table:
set month_01=JAN
set month_02=FEB
set month_03=MAR
#rem ...
set number=02
for %%a in (month_%number%) do call set month_as_text=%%%%a%%
echo %month_as_text%
The value in %number% in the for loop is used to dereference the matching variable name.
Or even shorter:
set number=02
for /f "tokens=%number%" %%m in ("JAN FEB MAR APR ...") do set month_as_text=%m
echo %month_as_text%
EDIT:
Johannes suggests a shorthand for the 1st version:
set month_01=JAN
set month_02=FEB
set month_03=MAR
#rem ...
set number=02
setlocal enabledelayedexpansion
set month_as_text=!month_%number%!
echo %month_as_text%

You could always do the number-to-text translation by hand, like:
if %MM%==01 set MM=Jan
if %MM%==02 set MM=Feb
if %MM%==03 set MM=Mar
if %MM%==04 set MM=Apr
etc.

The first answer is incorrect for August and September. The reason is that the shell interprets a leading zero as octal, and 08 and 09 are not valid octal numbers. You can see this easily for yourself by running this command:
for /f "tokens=08" %a in ("A B C D E F G H I J") do echo %a
If you replace the 08 above with 09 it will also fail. It will work if you use 07 or 10. A working implementation is as follows:
months=JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
:: get the month as a 2-digit number
set number=%date:~4,2%
:: remove leading zero
for /f "tokens=* delims=0" %%a in ("%number%") do set number=%%a
:: index into array to get month name
for /f "tokens=%number%" %%m in ("%months%") do set month_as_text=%%m
:: print month name
echo %month_as_text%

Related

In Conky, how do I convert from Unix time to hours and minutes? (HH:MM)

Given a Unixtime such as 1551996855 (Thu, 07 Mar 2019 22:14:15 +0000), how can convert and extract just HH:MM, (22:14)?
you could use date for the conversion and specify +%H:%M as the format:
date -d #$"1551996855" +%H:%M
note: the option to use with date will be different than -d if you are not on a Linux system (which uses date from GNU coreutils).. so YMMV

Convert "Thu Sep 22 3:50 2016" to "2016-09-22" in Solaris, without GNU date

Could someone please suggest a simple and short approach to convert "Thu Sep 22 3:50 2016" to "2016-09-22" in Solaris, through a shell script?
I do not have GNU date available on Solaris as discussed in below post:
Convert date String to number on Solaris shell script gives No such file or directory
I need to query an sql server db, where date is saved in the format, "2016-09-06", hence I need to convert it
Actually, you do have GNU date available but here is anyway one way to achieve this by scripting:
#!/bin/ksh
a="Thu Sep 22 3:50 2016"
echo $a | nawk '
BEGIN {
m=1
m2m["Jan"]=m++;
m2m["Feb"]=m++;
m2m["Mar"]=m++;
m2m["Apr"]=m++;
m2m["May"]=m++;
m2m["Jun"]=m++;
m2m["Jul"]=m++;
m2m["Aug"]=m++;
m2m["Sep"]=m++;
m2m["Oct"]=m++;
m2m["Nov"]=m++;
m2m["Dec"]=m++;
}
{
printf("%s-%02d-%02d\n",$5,m2m[$2],$3)
}'
output:
2016-09-22
Why not use Oracle's sysdate?
select * from your_table where saved_date >= to_char(sysdate,'yyyy-MM-dd')

Get date of last saturday - BusyBox 1.1.0

Since the date in BusyBox is not as powerful as gnu date, I have problems to calculate the date of last saturday.
last_sat=`date +"%Y-%m-%d" -d "last saturday"`
only works fine with gnu date.
I've found something like this to calculate from Epoch
busybox date -D '%s' -d "$(( `busybox date +%s`+3*60 ))"
but my BusyBox (v1.1.0) doesn't recognize the -D argument.
Any suggestions?
For the last Saturday before today, under busybox 1.16:
date -d "UTC 1970-01-01 $(date +"%s - 86400 - %w * 86400"|xargs expr) secs"
How it works: take the current date in seconds, subtract one day, subtract one day times the number of the current weekday, then convert those seconds back to a date.
EDIT: after hacking together a build of 1.1, this works:
date -d "1970.01.01-00:00:$(date +"%s - 86400 - %w * 86400"|xargs expr)"
This working version is based on code-reading:
} else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
&t.tm_mon, &t.tm_mday,
&t.tm_hour, &t.tm_min,
&t.tm_sec) == 6) {
t.tm_year -= 1900; /* Adjust years */
t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
BusyBox's date command has been the topic of some discussion over the years. Apparently it doesn't always work as documented, and it doesn't always work the same as previous versions.
On a BB system I administer running BusyBox v1.01, I'm able to use the -d option with dates in the format MMDDhhmmYYYY.ss, and in no other format that I've tried. Luckily, output formats work as expected, presumably because date is using a proper strftime() according to comments in the source.
Here's my forward-and-reverse example:
[~] # busybox date '+%m%d%H%M%Y.%S'
090500152016.41
[~] # busybox date -d 090500152016.41
Mon Sep 5 00:15:41 EDT 2016
So .. what can we do with this? It seems that we can't do an arbitrary adjustment of seconds, as it only reads the first two digits:
[~] # busybox date -d 123119001969.65 '+%s'
65
[~] # busybox date -d 123119001969.100 '+%s'
10
Well, it turns out you can load the date fields with "invalid" numbers.
[~] # busybox date 090100002016
Thu Sep 1 00:00:00 EDT 2016
[~] # busybox date 093400002016
Wed Oct 4 00:00:00 EDT 2016
[~] # busybox date 09-200002016
Mon Aug 29 00:00:00 EDT 2016
So let's adjust the "day" field using something based on %w.
today=$(busybox date '+%m%d%H%M%Y')
last_sat=$(busybox date -d "${today:0:2}$( printf '%02d' $(( 10#${today:2:2} - 1 - $(busybox date '+%w') )) )${today:4}" '+%F')
This simply subtracts numbers in the second field (the 3rd and 4th characters of the date string). It obviously requires that your shell either be bash or understand bash-style math notation ($((...))). Math-wise, it should work as long as "last saturday" is within the same month, and it MAY work (I haven't tested it) with rollvers to the previous month (per the last test above).
Rather than jumping through these burning hoops, I recommend you just install a GNU date binary, and don't use busybox for this one binary. :-P
Good luck!

Capture the date into a batch file variable

I'm writing a batch file to copy some data, the issue I have is, I want to capture the date in a variable so that I can use it as the file name. I would like it in the format of dd-mm-yyyy.
I found this example here, but I was unable to get it into the format I need.
this solution is independent of locale settings:
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
It will give you (independent of locale settings!):
20130802203023.304000+120
( YYYYMMDDhhmmss.<fraction>+/-<timedifference to UTC> )
from here, it is easy:
set mydate=%datetime:~6,2%-%datetime:~4,2%-%datetime:~0,4%
echo %mydate%
Solution
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set newdate=%dd%%mm%%yy%
echo %newdate%
:date
set dateyear=%date:~-4%
if %date:~-7,-5%==01 set datemonth=January
if %date:~-7,-5%==02 set datemonth=February
if %date:~-7,-5%==03 set datemonth=march
if %date:~-7,-5%==04 set datemonth=April
if %date:~-7,-5%==05 set datemonth=may
if %date:~-7,-5%==06 set datemonth=June
if %date:~-7,-5%==07 set datemonth=July
if %date:~-7,-5%==08 set datemonth=August
if %date:~-7,-5%==09 set datemonth=September
if %date:~-7,-5%==10 set datemonth=October
if %date:~-7,-5%==11 set datemonth=November
if %date:~-7,-5%==12 set datemonth=December
set dateday=%date:~-10,-8%
echo It is %dateday% %datemonth% %dateyear%, sir.
exit /B 0
:xpdate
echo It is %date%, sir.
%speech% "It is %date%, sir."
exit /B 0
:w7dat
if %time:~0,-9% GTR 12 (set /a timehour=%time:~0,-9%-12) else set timehour=%time:~0,-9%
if %time:~3,1%==0 (set timeminute=%time:~4,1%) else set timeminute=%time:~3,2%
set dateyear=%date:~-4%
if %date:~-7,-5%==01 set datemonth=January
if %date:~-7,-5%==02 set datemonth=February
if %date:~-7,-5%==03 set datemonth=march
if %date:~-7,-5%==04 set datemonth=April
if %date:~-7,-5%==05 set datemonth=may
if %date:~-7,-5%==06 set datemonth=June
if %date:~-7,-5%==07 set datemonth=July
if %date:~-7,-5%==08 set datemonth=August
if %date:~-7,-5%==09 set datemonth=September
if %date:~-7,-5%==10 set datemonth=October
if %date:~-7,-5%==11 set datemonth=November
if %date:~-7,-5%==12 set datemonth=December
set dateday=%date:~-10,-8%
echo It is currently %timeminute% minutes past %timehour% on %dateday% %datemonth% %dateyear%, sir.
exit /B 0
This is what i use in my batch AI just change the :date :w7dat and all those to what you need
Here is a solution using PowerShell, which is pre-installed on nearly all versions of Windows and is far more powerful than batch.
PowerShell code
Get-Date -Format 'yyyy-MM-dd'
Calling from a batch file
for /f %%a in ('powershell -nop -c "Get-Date -Format 'yyyy-MM-dd'"') do set newdate=%%a

Batch file - scheduled run vs cmd run - correct date

I have the following few lines in a batch file:
#echo off
echo Running dump...
"D:\wamp\bin\mysql\mysql5.5.16\bin\mysqldump" --host="localhost" --user="****" --password="****" database> "D:\wamp\backup\mysql\"back.%date:~0,2%.sql
echo Done!
Supposedly, it should create a backup file with today's day as 01, 02, 03, ..., 31, that is,
back.01.sql
back.02.sql
back.03.sql
When I run it from CMD, it actually creates it as expected, but when it is run from the scheduler it looks like:
back.Mo.sql
back.Tu.sql
back.We.sql
What date format should I use to ensure it actually created with number of the day of the month?
Working with dates and times in Windows batch is a pain.
The most robust solution is to use WMIC to get the local date and time. It returns a value in the following format:
YYYYMMDDhhmmss.dddddd-ttt
YYYY = year
MM = month
DD = day
hh = hour in 24 hour format
mm = minutes
ss = seconds
dddddd = fractional seconds
ttt = time zone (minutes difference from Greenwich Mean Time)
So you can use the following to get the current day of the month in a variable
Edit - corrected starting substring location from 7 to 6
set curDate=
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined curDate set curDate=%%x
set day=%curDate:~6,2%
The big advantage of this technique is it will work on any Windows machine in the world, regardless of the locale settings.
Here is your code with the technique inserted
Edit - corrected starting substring location from 7 to 6
#echo off
echo Running dump...
set curDate=
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined curDate set curDate=%%x
set "day=%curDate:~6,2%"
"D:\wamp\bin\mysql\mysql5.5.16\bin\mysqldump" --host="localhost" --user="****" --password="****" database> "D:\wamp\backup\mysql\back.%day%.sql"
echo Done!
Just setup regional settings dd.MM.yyyy for the account under which your task is run. (use control panel or registry HKCU\Control Panel\International\sShortDate for Windows XP at least.