There are at least two possible solutions to get current year without century and month with two digits.
The first one is independent on region and language settings of used account.
@echo off
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocaleDateTime=%%I"
set "FileNameDate=%LocaleDateTime:~2,4%"
for %%I in (
infdb.dat infdb.ix kunde.DAT kunde.ix
L%FileNameDate%01.DAT L%FileNameDate%01.DIA L%FileNameDate%01.IX
perso.dat perso.ix TI*FR.DAT TI*FR.IX TILKA.DAT TILKA.IX
) do copy %%I C:\bbsud1\ >nul 2>nul
This variant is similar to solution posted by LotPings, but is a little bit faster because of FINDSTR is not needed. Please read answer on Why does %date% produce a different result in batch file executed as scheduled task? for full explanation of the FOR loop and used WMIC command.
The disadvantage is that WMIC requires more than a second to output the local date/time.
Much faster is the second solution using environment variable DATE:
@echo off
set "FileNameDate=%DATE:~-2%%DATE:~-7,2%"
for %%I in (
infdb.dat infdb.ix kunde.DAT kunde.ix
L%FileNameDate%01.DAT L%FileNameDate%01.DIA L%FileNameDate%01.IX
perso.dat perso.ix TI*FR.DAT TI*FR.IX TILKA.DAT TILKA.IX
) do copy %%I C:\bbsud1\ >nul 2>nul
The disadvantage is that this solution depends on date format defined in Windows Region and Language settings by selected country respectively the date format settings for the country.
The above code works if echo %DATE%
outputs for example 20.07.2017
or Thu, 20/07/2017
.
The string substitution must be modified if date format is MM/dd/yyyy
without or with weekday at beginning:
set "FileNameDate=%DATE:~-2%%DATE:~-10,2%"
But a different code is necessary like the one below if a month < 10 is output without a leading 0 like Thu, 20/7/2017
:
for /F "tokens=2,3 delims=./" %%I in ("%DATE:~-10%") do set "FileNameDate=%%J0%%I"
set "FileNameDate=%FileNameDate:~2,2%%FileNameDate:~-2%"
So the advantage of faster execution on using environment variable DATE requires knowledge of date format which is the reason why DATE based code is not often posted on Stack Overflow.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
echo /?
for /?
set /?
Please read also the Microsoft article about Using command redirection operators for an explanation of >nul
and 2>nul
to suppress success and error messages output by COPY by redirecting both to device NUL.