Appending a date to a filename in batch files
I wanted to append today's date to a file name for a file processed by a batch
job on Microsoft Windows systems, specifically a Small Business Server
(SBS) 2003 system and Windows 7 and 8 systems. You can obtain the current
date using the
date /t
command (the
/t
option prevents the command from prompting for a change to the date)
or by using
echo %date%
to display the contents of the
date environment variable. However, both of those display the date in
the form
DDD mm/dd/yyyy
, where
DDD
is the
day of the week represented by a 3-character abbrviation, e.g., "Sat",
mm
is a two-digit representation of the month, e.g., "11"
for November,
dd
is the day and
yyyy
is the
year, on the Windows systems I support.
C:\>date /t
Sat 11/22/2014
C:\>echo %date%
Sat 11/22/2014
You can reformat the representation of the date that is stored in the
%date%
environment variable, however. I wanted the date in the
form yyyymmdd
, so that I could stick that at the end of filenames
to represent the rotation date for a log file. You can use a command like the
one below where a variable, YYYYMMDD
is set to hold the reformatted
date (the variable name can be anything you like, e.g., mydate, etc., but that
name reminds me of the format I'm using for the date.
C:\>set YYYYMMDD=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
C:\>echo %YYYYMMDD%
20141122
The substring arguments to extract the elements of the date string are
in the format %variable:~startposition,numberofchars%
, so if the
"S" in Saturday in the string "Sat 11/22/2014" is at position 0, the
10th character is the "2" of 2014 and I want 4 characters, i.e., "2014", so
%DATE:10,4%
will give me those characters. Or you can also
think of the first number as the numer of characters to be skipped, i.e.,
%variable:~num_chars_to_skip,numberofchars%
. I can then append
%DATE:~4,2%
to get "11" for the month followed by
%DATE:~7,2%
to extract the day, i.e., "22" if the date is
November 22, 2014 represented in the %DATE% variable as "Sat 11/22/2014".
References:
-
How to append a date in batch files
Posted: May 14, 2009
stackoverflow
-
Extracting a Substring from a String under Microsoft Windows
MoonPoint Support
[/os/windows/commands]
permanent link
Items detected by a ClamWin Scan on 2014-11-22
In continuance of an effort to ensure that all malware has been removed
from a Windows 7 Professional system, I scanned the system with
ClamWin Free Antivirus today.
It identified 12 suspect items, but I'm not sure any of those contributed
to excessive memory usage I saw at times on the system yesterday from
explorer.exe consuming more than 1/2 the 6 GB of memory on the system
for prolonged periods.
[ More Info ]
[/security/malware]
permanent link
Determing the location of a Microsoft Windows DNS log file from a command prompt
If you need to determine the location of the log file for a Microsoft Windows
server functioning as a DNS server from the command line, you can do so through
a
reg query
command.
C:\>reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters /v LogFilePath
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters
LogFilePath REG_SZ d:\logs\dns\dns.log
You can reduce the output displayed to just the line containing the
log file location by piping the output of the reg query
command
into the find
command.
C:\>reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters /v LogFilePath | find "LogFilePath"
LogFilePath REG_SZ d:\logs\dns\dns.log
If you wish to see just the log file location and not the other information
returned by the reg query
command, you can use
a FOR /F loop command such
as the following:
C:\>for /f "tokens=3" %g in ('reg query "HKLM\SYSTEM\CurrentControlSet\Services\DNS\Parameters" /v LogFilePath ^| find "LogFilePath"') do @echo %g
d:\logs\dns\dns.log
C:\>
The FOR /F
loop breaks up a line of output from the command
that is being processed into items, called "tokens" that are separated by
space on the lines of output from the command. In this case, I'm only
interested in the third token on the line of output, which is the location
of the DNS log file. The output that is being processed is the result of
piping the output of the reg query
command into the find
command. Since the pipe symbol, i.e., the vertical bar character
|
, has a special meaning for the Windows operating system,
you need to place an
"escape character",
which for Windows is the caret symbol, ^
, immediately before
it. You also need to put the at symbol, @
, before the echo
command to avoid seeing the echo command itself as output.
If you wish to use a batch file to execute the commands to find the log
file location, you need to replace the %g
with %%g
as shown below.
@echo off
FOR /F "tokens=3" %%G IN ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters" /v LogFilePath ^| find "LogFilePath"') DO echo %%G
The registry key and the value to be queried can also be placed in
environment
variables that can be modified, if you wish to query other
registry keys, instead of the one for the DNS log file location, so that it
is easier to see what needs to be changed for such other queries.
@echo off
REM Name: queryreg.bat
REM Version: 1.0
REM Created: November 22, 2014
REM Last Modified: November 22, 2014
REM
REM Description: Displays just the value of a registry key from a
REM "reg query regkey /v regvalue" command omitting the additional
REM information that is output by the command
set regkey="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters"
set regvalue="LogFilePath"
FOR /F "tokens=3" %%G IN ('reg query %regkey% /v LogFilePath ^| find %regvalue%') DO echo %%G
Download:
queryreg.bat
[/network/dns/windows/logging]
permanent link