0

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?

2
  • Batch is sensitive to spaces in an ordinary string SET statement. SET FLAG = N sets a variable named "FLAG " to a value of " N". Remove the spaces from both sides of the =. Your for metavariable is %%i so the data elements picked by the tokens option will assign the first four values to %%i,%%j,%%k,%%l, %%h will have no value. Any assignment of a value that has 0 as its first character is assumed by set/a to be OCTAL, so will fail for 08 and 09. Use a string set instead. %time% will probably contain colons which are illegal in a filename. Commented Apr 13, 2023 at 16:19
  • Preferred set syntax is set "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 a setlocal command 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. Commented Apr 13, 2023 at 16:22

1 Answer 1

0

Your right, batch is sensitive to spaces. I removed the spaces before and after the = sign and it worked fine. I'm not sure what you were referring to with regards to the date variables, but that section works fine.

code that works:

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%.log

echo ==LOG FILE %datestr% == > %filename%
Sign up to request clarification or add additional context in comments.

6 Comments

A locale independent version to set the datestr variable: for /f "tokens=1-3 delims=/ " %%a in ('%__APPDIR__%Robocopy.exe "|" . /njh ^| find ":"') do set "datestr=%%b_%%c_%%a"
You could also set the date and time all in one pass: for /f "tokens=1-6 delims=/: " %%a in ('%__APPDIR__%Robocopy.exe "|" . /njh ^| find ":"') do set "datestr=%%b_%%c_%%a-%%d:%%e:%%f"
Or at the very least, replace lines 1..8 with for /f "tokens=1,*" %%i in ("%date:/=_%") do set "datestr=%%j". Then be aware that %time% will inevitably contain characters which are disallowed in Windows filenames, so in that case, you should parse that variable value and make some modifications to it too!
Since you appear not to be using using dow in your code, you should note that it too, requires the spaces to be removed. And the dayname would likely appear in %%i, not %%h. I made no mention of date in my comment. You have not revealed the format of your %date% string (it is user-dependent) so if the day or month number may appear as 08 or 09, the set /a will fail. You display a log filename as C:\startup\logs\BootUplog-4-11:43:14.50(time).log The character : is illegal in a filename (can only be used after a drive letter)
If the date has 04 for the current month, then set /a month=… will set month to 4 as 04 in octal is the same as 4 in decimal. On May 8th, set /a day=… will fail as set /a will attempt to assign 08 which is invalid octal. Same goes for 09, or August or September.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.