Solved

DOS - Automated Tree Structure Creation

Posted on 2016-08-07
10
79 Views
Last Modified: 2016-08-15
Each month I have to create a new monthly identified folder with several sub-folders:
ie:
Journal Entries  [This folder is the main folder]
CcYy-MmDd [Sub-Folder to be created each Year-MmDd ie 2016-0831 1st tier to create]
     Folder 01  [Sub-Folder under the monthly folder - 2nd tier of folders]
     Folder 02  [Sub-Folder under the monthly folder - 2nd tier of folders]
     et al

It would be nice if I could also create a third tier of folders but I don't want to confuse the issue, because I can not see how to even start
programming such - so I do not know how to express it as a question.  Hope that makes sense.

I took one of my old batch programs and started piecing it together to give you an idea of what I am trying to do.  Hope it helps and not hinders.

Running a Windows 7 environment

Bill

-Bill
0
Comment
Question by:Wildbi11
  • 5
  • 3
  • 2
10 Comments
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Here's a BAT file that should get you started.  It uses a subroutine to get todays date and time formatted into variables, and them makes todays folders.

@echo off
setlocal EnableDelayedExpansion

rem Define base folder
set BaseDir=C:\Journal Entries

rem Get current date and time into local variables
call :GetLocalTime Y

rem Build new daily folder path
set NewFolder=%BaseDir%\%LocalTime.Year%-%LocalTime.Month%%LocalTime.Day%

rem Create todays folders
md "%NewFolder%"
md "%NewFolder%\Folder 01"
md "%NewFolder%\Folder 02"

rem Done
exit /b


:GetLocalTime [zero-pad(Y/N)]
  rem Sets these variables for further reference ("*" items will be zero filled if requested)
  rem LocalTime.Year
  rem LocalTime.Month (*)
  rem LocalTime.Day (*)
  rem LocalTime.Hour (*)
  rem LocalTime.Minute (*)
  rem LocalTime.Second (*)
  rem LocalTime.DayOfWeek
  rem LocalTime.WeekInMonth
  rem LocalTime.Quarter

  rem Execute WMIC command to get date time elements
  for /f "tokens=*" %%A in ('wmic /output:stdout Path Win32_LocalTime Get * /Format:value^|find "="') do (
    rem pass output through another FOR to remove trailing carraige returns
    for /f "tokens=*" %%B in ("%%A") do (
      rem set values into variables prefaced by "LocalTime."
      set "LocalTime.%%B"
    )
  )

  rem If requested, left pad with zero common 2 digit values
  if /i "%~1" EQU "Y" (
    for %%a in (LocalTime.Month LocalTime.Day LocalTime.Hour LocalTime.Minute LocalTime.Second) do (
      if !%%a! LSS 10 set %%a=0!%%a!
    )
  )

  rem Return to caller
  exit /b

Open in new window

~bp
0
 

Author Comment

by:Wildbi11
Comment Utility
Bill,
I am not sure what the "time" elements have to do with building my file structures.  I must not have explained the problem correctly.  I just need a simple routine that takes a list of folder names and then creates the folders underneath the main folder as sub-folders.

I was trying to set up a set of variables (the folder names) go to the base folder (using your terminology) then loop through folder names creating a new folder for each folder name variable.  See the code sample I set up.  My problem is how to create a "md" loop that goes from variable [1] to variable [n].

BK

-BK
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
WildBil

FYI: Points not needed. Just helping Bill in case he's not around...

Are you trying to make the "third tier" folders? If so, maybe this?

Assuming a file named Dirnames.txt with lines:

c:\Journal Entries\Cc16-0807\Folder 01
c:\Journal Entries\Cc16-0807\Folder 02
c:\Journal Entries\Cc16-0808\Folder 01
c:\Journal Entries\Cc16-0808\Folder 02

Open in new window


Make a .bat file of this code:

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('type Dirnames.txt') do (
  md "%%a\3rd Tier"
)

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
@Wildbi11,

You haven't included any code samples, but you keep referencing them?

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
CcYy-MmDd [Sub-Folder to be created each Year-MmDd ie 2016-0831 1st tier to create]
     Folder 01  [Sub-Folder under the monthly folder - 2nd tier of folders]
     Folder 02  [Sub-Folder under the monthly folder - 2nd tier of folders]
So this is exactly what my script did.  If you want adjustments I need more info to go on as far as what is wrong or missing, and what the desired results should be, etc.

~bp
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Wildbi11
Comment Utility
Back into the office - instead of attaching the file I'll copy what I start (and probably messed up):
I have embedded comments stating where I need help and what I am trying to accomplish:

setlocal EnableDelayedExpansion

Echo Off
CLS
Echo ...    Be sure to correct the CcYy-MmDd Designator
Echo ...
Echo ...
Echo ...
Echo ... 		Press Ctrl Break If You Need To Correct
Echo ... 
Echo ...
Echo ...
Pause
CLS

REM Location of Root Folder -----------------------------
Set RootPath="C:\GCH Month End\00 Journal Entries"
REM This is the Name of the Monthly Journal Entry Sub-Folder
REM  ie 2016-0831

Set CcYyMmDd="2016-0831"
Set MakeFolder="%RootPath%\CcYyMmDd"

REM If MakeFolder Exists - Exit  
REM Need help here - I want the program to 
REM Check if MakeFolder exists - if it does
REM Echo %MakeFolder% exists, pause, exit
REM If it does not exist - MD %MakeFolder%

REM -------Identify All Sub-Folder Names to Make -------------------------
Set PrjPath=%MakeFolder%
  Set Prjx[1]="%PrjPath%\Accruals"
  Set Prjx[2]="%PrjPath%\Amortization-Dr Loan Write Offs"
  Set Prjx[3]="%PrjPath%\Amortization-DSH"
  Set Prjx[4]="%PrjPath%\Amortization-MalPrac Ins"
  Set Prjx[5]="%PrjPath%\Amortization-PrePaids"
  Set Prjx[6]="%PrjPath%\Bank CCard Detail"
  Set Prjx[7]="%PrjPath%\Bank Recons"
  Set Prjx[8]="%PrjPath%\Clinic CFwd"
  Set Prjx[9]="%PrjPath%\Clinic Activity"
  Set Prjx[10]="%PrjPath%\Dept Recl Entries"
  Set Prjx[11]="%PrjPath%\GCH - AR Review"  
  Set Prjx[12]="%PrjPath%\H - Home Health Activity"  
  Set Prjx[13]="%PrjPath%\H - Home Medical Activity"  
  Set Prjx[14]="%PrjPath%\H - Hospice Activity"
  Set Prjx[15]="%PrjPath%\Inventory AJE"
  Set Prjx[16]="%PrjPath%\PR - Vac Accrl"
  Set Prjx[15]="%PrjPath%\Statistics"
  Set Prjx[15]="%PrjPath%\Transfers - Food Srvc"
  Set Prjx[15]="%PrjPath%\Transfers - Rx"
  
REM -----------------------------------------------------------------------
REM -----------------------------------------------------------------------
PAUSE
CLS

REM ==========================================================================================================
REM ------------ Make Sub Folders Or Exit ----------------------------------------------

REM Loop over thru the folder names - Cr8 Folder then Cr8 next one
REM FOR /L %%parameter IN (start,step,end) DO command
for /L %%P in (1,1,16) do (

  REM Make sure the Root Path Does Not Exist (if not continue Making Directory and Sub-Directories
  REM 
  if not exist !Prjx[%%P]! (

        Here's the loop I need
        First Make the  "MakeFolder"
        Then the Sub-Folders


Echo...  
Echo...  
Echo...                                  
Echo...  
Echo...  
REM -----------------------------------------------------------------------
REM -----------------------------------------------------------------------


Pause
CLS

Open in new window

0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Try this

@echo off
setlocal enabledelayedexpansion

Set RootPath=C:\GCH Month End\00 Journal Entries
REM This is the Name of the Monthly Journal Entry Sub-Folder
REM  ie 2016-0831

Set CcYyMmDd=2016-0831
Set MakeFolder=%RootPath%\%CcYyMmDd%

REM If MakeFolder Exists - Exit  
REM Need help here - I want the program to
REM Check if MakeFolder exists - if it does
REM Echo %MakeFolder% exists, pause, exit
REM If it does not exist - MD %MakeFolder%

REM -------Identify All Sub-Folder Names to Make -------------------------
Set PrjPath=%MakeFolder%
Set Prjx[1]=%PrjPath%\Accruals
Set Prjx[2]=%PrjPath%\Amortization-Dr Loan Write Offs
Set Prjx[3]=%PrjPath%\Amortization-DSH
Set Prjx[4]=%PrjPath%\Amortization-MalPrac Ins
Set Prjx[5]=%PrjPath%\Amortization-PrePaids
Set Prjx[6]=%PrjPath%\Bank CCard Detail
Set Prjx[7]=%PrjPath%\Bank Recons
Set Prjx[8]=%PrjPath%\Clinic CFwd
Set Prjx[9]=%PrjPath%\Clinic Activity
Set Prjx[10]=%PrjPath%\Dept Recl Entries
Set Prjx[11]=%PrjPath%\GCH - AR Review
Set Prjx[12]=%PrjPath%\H - Home Health Activity
Set Prjx[13]=%PrjPath%\H - Home Medical Activity
Set Prjx[14]=%PrjPath%\H - Hospice Activity
Set Prjx[15]=%PrjPath%\Inventory AJE
Set Prjx[16]=%PrjPath%\PR - Vac Accrl
Set Prjx[17]=%PrjPath%\Statistics
Set Prjx[18]=%PrjPath%\Transfers - Food Srvc
Set Prjx[19]=%PrjPath%\Transfers - Rx
 
REM -----------------------------------------------------------------------
REM -----------------------------------------------------------------------
REM PAUSE
REM CLS

REM ==========================================================================================================
REM ------------ Make Sub Folders Or Exit ----------------------------------------------

REM Loop over thru the folder names - Cr8 Folder then Cr8 next one
REM FOR /L %%parameter IN (start,step,end) DO command
for /L %%P in (1,1,19) do (
  echo "!Prjx[%%P]!"
  REM Make sure the Root Path Does Not Exist (if not continue Making Directory and Sub-Directories
  REM
  if not exist "!Prjx[%%P]!\*." (
      md "!Prjx[%%P]!"
  )
)

Open in new window

0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
Okay, thanks for posting that code.  Based on that here is how I would approach it.  Keep in mind if you don't want to have to manually edit the file to put the date into it for each run we can do that based on the current date when the script is run, as I showed earlier.  Let me know if you want that added back in.

Also, I would strongly suggest that you don't store items in the environment variables with surrounding double quotes.  Rather than doing:
Set RootPath="C:\GCH Month End\00 Journal Entries"
md %RootPath%
do:
Set RootPath=C:\GCH Month End\00 Journal Entries
md "%RootPath%"

This may not look quite as nice on the SET line, but when you reference that variable, and start concatenating it with other variables, or passing it to FOR loops or CALLed subroutines it's better to enclose it in quotes when you reference it.  It may seem trivial, but sooner or later the nesting and concatenation of quoted strings will cause a problem that will take time to sort out.

See how this goes for you:

@Echo Off
setlocal EnableDelayedExpansion

REM CLS
Echo ...    Be sure to correct the CcYy-MmDd Designator
Echo ...
Echo ...
Echo ...
Echo ... 		Press Ctrl Break If You Need To Correct
Echo ... 
Echo ...
Echo ...
Pause
REM CLS

REM Location of Root Folder -----------------------------
Set RootPath=C:\GCH Month End\00 Journal Entries
REM This is the Name of the Monthly Journal Entry Sub-Folder
REM  ie 2016-0831

Set CcYyMmDd=2016-0831
Set MakeFolder=%RootPath%\%CcYyMmDd%

REM If MakeFolder already exists, then exit, else create it now
if exist "%MakeFolder%" (
  echo MakeFolder "%MakeFolder%" already exists, quitting
  pause
  exit /b
) else (
  echo Creating folder "%MakeFolder%"
  md "%MakeFolder%"
)

REM -------Identify All Sub-Folder Names to Make -------------------------
Set PrjPath=%MakeFolder%
Set Prjx[1]=Accruals
Set Prjx[2]=Amortization-Dr Loan Write Offs
Set Prjx[3]=Amortization-DSH
Set Prjx[4]=Amortization-MalPrac Ins
Set Prjx[5]=Amortization-PrePaids
Set Prjx[6]=Bank CCard Detail
Set Prjx[7]=Bank Recons
Set Prjx[8]=Clinic CFwd
Set Prjx[9]=Clinic Activity
Set Prjx[10]=Dept Recl Entries
Set Prjx[11]=GCH - AR Review
Set Prjx[12]=H - Home Health Activity
Set Prjx[13]=H - Home Medical Activity
Set Prjx[14]=H - Hospice Activity
Set Prjx[15]=Inventory AJE
Set Prjx[16]=PR - Vac Accrl
Set Prjx[17]=Statistics
Set Prjx[18]=Transfers - Food Srvc
Set Prjx[19]=Transfers - Rx
  
REM -----------------------------------------------------------------------
REM -----------------------------------------------------------------------
PAUSE
REM CLS

REM ==========================================================================================================
REM ------------ Make Sub Folders Or Exit ----------------------------------------------

REM Loop over thru the folder names - Cr8 Folder then Cr8 next one
for /L %%i in (1,1,19) do (
  if not exist "%PrjPath%\!Prjx[%%i]!" (
    echo Creating folder "%PrjPath%\!Prjx[%%i]!"
    md "%PrjPath%\!Prjx[%%i]!"
  )
)

Echo...  
Echo...  
Echo...                                  
Echo...  
Echo...  
REM -----------------------------------------------------------------------
REM -----------------------------------------------------------------------

Pause
REM CLS

Open in new window

~bp
0
 

Author Comment

by:Wildbi11
Comment Utility
Thanks - I really appreciate the help - its been quite sometime since I have used a batch file and you have given me a great base to use now and in the future - again, thanks a lot.  I have not been on the site for some time and I am not sure how to award any points - if I messed up, let me know and I'll correct.  Again, thanks for the patience and help.

WB
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
It doesn't look like you have accepted any solution yet, so you want to do that and get this question closed.  You can get additional help in the support pages, see if this helps enough, if not let us know and we can help you further.

How do I accept a comment as my solution?

~bp
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now