Format of string output by date /t
depends on Windows region and language settings.
Instead of date /t
the value of environment variable DATE is used in code below which on access has always the current date. The format of the date string depends also on Windows region and language settings. For example for German countries the format is dd.mm.yyyy
while in other countries the format is dd/mm/yyyy
or mm/dd/yyyy
. Run in a command prompt window echo %DATE%
to see local date format.
Note: Format of date \t
(often with weekday) is quite often different to format of %DATE%
(usually without weekday).
My answer on Find out if file is older than 4 hours in batch file contains more information about date and time on Windows and also demonstrates how to use wmic
to get date string in a region and language independent format with the disadvantage of batch script becomes slower.
The code below works for date formats dd.mm.yyyy
, dd/mm/yyyy
and d/m/yyyy
(no leading 0 for day or month smaller than 10).
Other Windows command processing specifics must be taken also into account like a number with 2 or 3 digits with a leading 0 being interpreted as octal number on calculations.
Here is a commented code which should work for your task.
@echo off
setlocal EnableExtensions
set "source=Folder_Name.txt"
rem Set directory of batch file as current directory.
cd /D "%~dp0"
rem Get month and year from environment variable DATE which has for
rem countries with German as native language the format dd.mm.yyyy.
rem The code below works also for date formats dd/mm/yyyy and d/m/yyyy.
for /F "tokens=2,3 delims=./" %%A in ("%DATE%") do (
set "month=%%A"
set "year=%%B"
)
rem Remove leading zero from month as otherwise the value
rem would be interpreted as octal number resulting in an error
rem for 08 (August) and 09 (September) on next calculation.
if "%month:~0,1%" == "0" set "month=%month:~1%"
rem Subtract 1 from month.
set /A month-=1
rem Decrease year by 1 and set month to December if month is now 0.
rem Otherwise check if month is smaller than 10 and
rem insert a leading 0 if this condition is true.
if %month% == 0 (
set /A year-=1
set "month=12"
) else if %month% LSS 10 set "month=0%month%"
set "exception=%year%%month%"
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Check every folder in the source for pattern matching.
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rem Command DIR with parameter /B (bare format - only file name) returns
rem only file names (parameter /A-D) matching the search pattern ordered
rem by name (parameter /ON) found in specified directory or any subdirectory
rem (parameter /S) with full path because of parameter /S and always without
rem quotes even if file name with path contains 1 or more spaces.
if exist output.txt del output.txt
for /F "usebackq delims=" %%H in ("%source%") do (
for /F "delims=" %%F in ('dir /A-D /B /ON /S "*%exception%*.txt" 2^>nul') do echo %%F>>output.txt
)
rem Restore previous environment table, restore previous
rem states for command extensions and delayed expansion
rem and restore also previous current directory.
endlocal
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.
cd /?
dir /?
echo /?
endlocal /?
for /?
if /?
rem /?
set /?
setlocal /?