Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Hi and thanks in advance. I'm new to batch scripting and would like to perform the following action daily in my server. I need to make sure the directory path year/month/day exists in a given subfolder, if not create it.

I researched online but I could only find scripts that make yyyymmdd type directory names and create only one level in the directory tree structure.

I found the following example in this blog.

:: Auto directory date batch (MMDDYYYY format)
:: First parses month, day, and year into mm , dd, yyyy formats and then combines to be  DDMMYYYY
:: Setups %date% variable
:: @author Deepu Mohan Puthrote www.deepumohan.com
@echo off
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%dd%%mm%%yyyy%
echo New folder name %date%
MKDIR %date%
share|improve this question
    
Please post such example, so we can try to modify it –  Voitcus Aug 23 '13 at 17:35
    
If you mean is creating a folder named "YYYY/MM/DD", Its not possible. because you cant use Slash/BackSlash characters in folder name, if not so, please tell the details. –  Amirreza Nasiri Aug 23 '13 at 18:30
    
I don't mean one folder with slash characters, I mean a path of directories. –  circuitlego Aug 23 '13 at 18:44

1 Answer 1

You can use variables to hold the info then call it to make the directory structure. I've used the following as part of a script to backup some basic files to a similar directory structure as you described.

set drive=C:\temp
set year=%date:~10,4%
set month=%date:~4,2%
set day=%date:~7,2%

if not exist "%drive%\%year%\%month%\%day%" MD "%drive%\%year%\%month%\%day%"

For instance, today this will check to see if the directories exist, then create the directory structure C:\temp\2013\08\23 if it doesn't.

share|improve this answer
    
It created this directory structure: C:\temp\8\01 –  circuitlego Aug 24 '13 at 7:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.