Create folders named after each day in current month

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSScriptWriter
Posts: 12
Joined: 18 Oct 2010 14:51

Create folders named after each day in current month

#1 Post by DOSScriptWriter » 18 Oct 2010 15:09

If I run the script any day in October, it should generate folders 2010-10-01, 2010-10-02......2010-10-31 in "C:\". So far I have the following which creates a folder named after today's date only.

Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%
mkdir %yyyy%%mm%%dd%
mkdir "C:\%yyyy%-%mm%-%dd%"

Any help would be much appreciated. This is my first post and I look forward to learning from you guys.
Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Create folders named after each day in current month

#2 Post by aGerman » 18 Oct 2010 18:33

I adopted the formulas for :date2jdate and :jdate2date from there to create function :DaysPerMonth. It needs the year and the month as input and returns the number of days of the given month. Now we can use a FOR /L loop to create the directories.

Code: Select all

@echo off &setlocal
Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%

call :DaysPerMonth %yyyy% %mm% x

for /l %%i in (101,1,1%x%) do (
  set "d=%%i"
  call ECHO mkdir "C:\%yyyy%-%mm%-%%d:~-2%%" 2>nul
)
PAUSE
goto :eof

:DaysPerMonth yyyyIn mmIn numOut
setlocal
set "yy=%~1" &set "mm=%~2"
set /a "yy=10000%yy% %% 10000, mm=100%mm% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=31-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
set /a L= JD+68569,            N= 4*L/146097,     L= L-(146097*N+3)/4
set /a I= 4000*(L+1)/1461001,  L= L-1461*I/4+31,  J= 80*L/2447
set /a K= L-2447*J/80,         X=31-K
if %X%==0 set "X=31"
endlocal &set "%~3=%X%"
goto :eof

Remove the ECHO and the PAUSE command if you're sure it would work for you.

Regards
aGerman

DOSScriptWriter
Posts: 12
Joined: 18 Oct 2010 14:51

Re: Create folders named after each day in current month

#3 Post by DOSScriptWriter » 19 Oct 2010 08:51

Great! Works like a charm. Thank you so much.

I need your help doing one more task though. I have an Excel template "GLs.xlt" in "C:\". Just like we created new folders with names of each day in current month, I need to create copies of this template in "C:\" named after each day in this month. So I need 2010-10-01.xls, 2010-10-02.xls.....2010-10-31.xls.

Appreciate your time.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Create folders named after each day in current month

#4 Post by aGerman » 19 Oct 2010 13:17

Generally this shouldn't be a problem because you can copy .xlt files directly to .xls files. It works similar to the code before, but you have to check if the files already exist to protect you from overwriting your data.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%

call :DaysPerMonth %yyyy% %mm% x

for /l %%i in (101,1,1%x%) do (
  set "d=%%i"
  set "filename=C:\%yyyy%-%mm%-!d:~-2!.xls"
  if not exist "!filename!" ECHO copy "C:\GLs.xlt" "!filename!"
)
PAUSE
goto :eof

:DaysPerMonth yyyyIn mmIn numOut
setlocal
set "yy=%~1" &set "mm=%~2"
set /a "yy=10000%yy% %% 10000, mm=100%mm% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=31-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
set /a L= JD+68569,            N= 4*L/146097,     L= L-(146097*N+3)/4
set /a I= 4000*(L+1)/1461001,  L= L-1461*I/4+31,  J= 80*L/2447
set /a K= L-2447*J/80,         X=31-K
if %X%==0 set "X=31"
endlocal &set "%~3=%X%"
goto :eof


Regards
aGerman

DOSScriptWriter
Posts: 12
Joined: 18 Oct 2010 14:51

Re: Create folders named after each day in current month

#5 Post by DOSScriptWriter » 19 Oct 2010 17:33

Couldn't get any better!

Thanks a ton. Glad there are smart people willing to share their knowledge.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Create folders named after each day in current month

#6 Post by ghostmachine4 » 19 Oct 2010 18:06

The date and time command using batch will give you different results when used in different settings due to different regional settings. If you want to play with dates , use vbscript (powershell), not batch.

Code: Select all

Set objFS = CreateObject( "Scripting.FileSystemObject" )
strFolder="c:\test\"
N=Now
yr=Year(N)
mth=Month(N)
For i=1 To 31
    If Len(i)<2 Then
     i="0"&i
    End If
    start= yr&"-"&mth&"-"&i
   If IsDate(start) Then
      WScript.Echo start
      objFS.CreateFolder(strFolder &start)
   End If
Next


save as mycreate.vbs and on command line

Code: Select all

C:\test>cscript //nologo mycreate.vbs

Post Reply