CMD batch-creating folder on beginning of the month and subfolders for every day of the month

Posted: June 9, 2015 in Scripts
Tags: , ,

Let’s say you must create backup every day,but backups must distinguish themeslves by months.Our goal is,for every month to create separate folder,and within that folder,create subfolder for evrery day of the month (C:\Backup1January1.01)

Please note that in my example i used dd.mm.yyyy date format !!

Let’s begin:

Open notepad and write these lines:

:: variables
:: setting v to equal the current numeric month
set v=%date:~3,2%
set y= %date:~6,98%
set folder=%date:~0,5%
:: putting all the possible months into a variable which will be parsed in the for loop
set month_text=”01 JanuarY” “02 February” “03 March” “04 April” “05 May” “06 June” “07 July” “08 August” “09 September” “10 October” “11 November” “12 December”

:: for loop is parsing the month_text variable and using the find command with the numeric month to set the current_month variable
for %%I in (%month_text%) do echo %%I |find “%v%” &&set current_month=%%~I
set drive=H:\%current_month%%y%\

:: checking if current month folder exists. If not create it.
IF NOT EXIST “%drive%” mkdir “%drive%”
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
%backupcmd% “” “%drive%\%folder%”

save a file with .bat extension,and double click it to see results.

Also,note that destination and source path path must be surrounded by quotes

I copied to H:\,but you can change the destination

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s