I searched and couldn't find anything on concatenating strings and numbers in a windows command script. I am trying to get create a log file with the date and time in the name and alls I get is the month of the date, not the whole date.
The code I have looks like this:
for /f "tokens1-4 delims=/ " %%i in ("%date%") do (
set dow = %%h
set /A month = %%j
set /A day = %%k
set /A year = %%l
)
set datestr = %month%_%day%_%year%
set pathstr = c:\startup\logs\
set filename = %pathstr%BootUplog-%datestr%-%time%.log
echo ==LOG FILE %datestr% == > %filename%
the set datestr part does not work and the log file is never created. %filename% becomes C:\startup\logs\BootUplog-4-11:43:14.50(time).log
I do not understand how the datestr does not become the date 4-13-2023. Instead it is just 4. Also, why is the log file never created?
SETstatement.SET FLAG = Nsets a variable named "FLAG " to a value of " N". Remove the spaces from both sides of the=. Yourformetavariable is%%iso the data elements picked by thetokensoption will assign the first four values to%%i,%%j,%%k,%%l,%%hwill have no value. Any assignment of a value that has0as its first character is assumed byset/ato be OCTAL, so will fail for08and09. Use a stringsetinstead.%time%will probably contain colons which are illegal in a filename.setsyntax isset "var=value"for setting string values - this avoids problems caused by trailing spaces. Don't assign"or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. It's normal practice to have asetlocalcommand directly after the initial@echo off. This discards any changes made to the environment when the batch ends, so variables established by one batch file do not affect any further batches that may be run in the same session.