@echo off
setlocal EnableDelayedExpansion
REM Duplicate a file for as many days there are in the current month
REM And rename each file with a Julian date infront of the file name
pushd "%~dp0"
call :GetCurrDate
call :SetFebruary
call :GetMonthName %MM%
call :GetJulianDay %MM% %DD%
call :GetMaxDaysInMonth %MM%
call :GetMonthRange %MM%
REM Check that only one other file is in the folder
for /f "delims=" %%I in (' dir /b /a:-d "*.*" ^| find /v "%~nx0" ^| find /c /v "" ') do (
if "%%I" NEQ "1" echo.& echo Error: Expected exactly 1 other file in directory.& timeout 2 >NUL& goto :eof
)
REM Copy files n number of times for days in current month
set counter=0
for /f "delims=" %%F in (' dir /b "*.*" ^| find /v "%~nx0" ') do (
for /l %%I in (1,1,%DD_MaxDaysInMonth%) do (
set /a counter+=1
copy "%~dp0\%%~nxF" "%~dp0\tmp!COUNTER!%%~xF" >NUL
)
set FileName=%%~nF
set FileExtn=%%~xF
del "%%~fF"
goto :break
)
:break
REM Rename files in folder
set counter=%MIN_JulianDayInMonth%
for /f "delims=" %%F in (' dir /b "*.*" ^| find /v "%~nx0" ') do (
ren "%%~fF" "!COUNTER! %FILENAME%%FILEEXTN%"
if !COUNTER!==%MAX_JulianDayInMonth% goto :break
set /a counter+=1
)
:break
:End
popd
echo.&echo Success.
goto :eof
REM End
REM Functions below
:GetCurrDate
setlocal
for /f "delims=" %%I in (' date /t ') do (
set currDate=%%I
set currDate=!CURRDATE: =!
set currDate=!CURRDATE:~4!
)
for /f "tokens=1-3 delims=/" %%A in ("%CURRDATE%") do (
set DD=%%A
set MM=%%B
set YYYY=%%C
set Index=!MM!
)
endlocal & set CurrDate=%CURRDATE%& set DD=%DD%& set MM=%MM%& set YYYY=%YYYY%&
goto :eof
:SetFebruary
setlocal
set /a Leapyear=YYYY %% 4
if %LEAPYEAR%==0 ( set Feb=29) else ( set Feb=28)
endlocal & set Feb=%FEB%
goto :eof
:GetMonthName MM
setlocal
set Index=%1
for /f "tokens=%INDEX%" %%M in (
" January February March April May June
July August September October November December "
) do set MM_Name=%%M
endlocal & set MM_MonthName=%MM_Name%
goto :eof
:GetJulianDay MM DD
setlocal
set Index=%1
set /a Index-=1
for /l %%I in (1,1,%INDEX%) do call :GetJulianDay_Sub %%I
set /a JulianDay+=%2
endlocal & set DD_JulianDay=%JULIANDAY%
goto :eof
:GetJulianDay_Sub
for /f "tokens=%1" %%M in (
" 31 %Feb% 31 30 31 30 31 31 30 31 30 31 "
) do ( set /a JulianDay+=%%M)
goto :eof
:GetMonthRange MM
setlocal
set Index=%1
call :GetMaxDaysInMonth %INDEX%
set DD=%DD_MaxDaysInMonth%
call :GetJulianDay %INDEX% %DD%
set MAX_JulianDayInMonth=%DD_JulianDay%
set DD=1
call :GetJulianDay %INDEX% %DD%
set MIN_JulianDayInMonth=%DD_JulianDay%
set MonthRange=%MIN_JulianDayInMonth%-%MAX_JulianDayInMonth%
endlocal & set MIN_JulianDayInMonth=%MIN_JulianDayInMonth%& set MAX_JulianDayInMonth=%MAX_JulianDayInMonth%& set MonthRange=%MONTHRANGE%&
goto :eof
:GetMaxDaysInMonth MM
setlocal
set Index=%1
for /f "tokens=%INDEX%" %%M in (
" 31 %Feb% 31 30 31 30 31 31 30 31 30 31 "
) do set DD_MaxDays=%%M
endlocal & set DD_MaxDaysInMonth=%DD_MaxDays%
goto :eof