?
Solved

Batch folder creation?

Posted on 2013-01-23
13
Medium Priority
?
251 Views
Last Modified: 2013-01-24
I need to:
1. create three folders eg. Folder 1, 2 and 3 within a folder, where there's 200 folders - all folders within this folder, will need the folders 1,2 and 3 created
2. create a log file confirming success/ failure

I've been playing around with the following command via a DOS batch script:

@echo on
H:
cd H:\
echo Making Directories...
mkdir %1\

What's the simplest/ best technique for doing this - DOS or VB Script? I think VB Script would be the best?

Does anyone have a script that can perform this function?
0
Comment
Question by:Osley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 56

Expert Comment

by:Bill Prew
ID: 38812779
I think this should work pretty well for you, save it as a BAT file, and edit the SET lines at the top as needed. It will locate all folders under the base directory, and add the new folders specified. It logs success or failure to LOG.TXT.

@echo on
setlocal
set BaseDir=H:
set NewDirs="folder 1","folder 2","folder 3"
(
  echo Making Directories...
  for /D %%A in (%BaseDir%\*.*) do (
    for %%B in (%NewDirs%) do (
      md "%%~A\%%~B" >NUL 2>&1 && (
        echo Created: "%%~A\%%~B"
      ) || (
        echo *ERROR*: "%%~A\%%~B"
      )
    )
  )
)>"log.txt"

Open in new window

~bp
0
 

Author Comment

by:Osley
ID: 38812809
Hi. Thanks for the fast response.

I need to ammend the directory from:

set BaseDir=H: to a folder in H, which is H:\Temp Folder\2013. What's the proper syntax? I've added this to the end of BaseDir=H: and one folder was created on the desktop with the three folders contained within.

The log file generated the following:

Making Directories...

C:\Users\Administrator\Desktop>(for %B in ("Excel" "Powerpoint" "Word") do (md "H:\Temp\%~B"   1>NUL 2>&1  && (echo Created: "H:\Temp\%~B" )  || (echo *ERROR*: "H:\Temp\%~B" ) ) )
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 38812835
This should handle that.

@echo off
setlocal
set BaseDir=H:\Temp Folder\2013
set NewDirs="Excel","Powerpoint","Word"
(
  echo Making Directories...
  for /D %%A in ("%BaseDir%\*.*") do (
    for %%B in (%NewDirs%) do (
      md "%%~A\%%~B" >NUL 2>&1 && (
        echo Created: "%%~A\%%~B"
      ) || (
        echo *ERROR*: "%%~A\%%~B"
      )
    )
  )
)>"log.txt"

Open in new window

~bp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Osley
ID: 38812845
I've updated the field and encounter the error:
1. A 'home' folder is created on the desktop, with the 3 folders within
2. The log file is as follows:

C:\Users\Administrator\Desktop>(for %B in ("Excel" "Powerpoint" "Word") do (md "H:\Staff\%~B"   1>NUL 2>&1  && (echo Created: "H:\Staff\%~B" )  || (echo *ERROR*: "H:\Staff\%~B" ) ) )

I updated the field with the following:
set BaseDir=H:\Staff Home Folder\2013

The above is the actual directory path..

Thanks again.
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 1200 total points
ID: 38812858
See my last post and start from that, I also had to change this line (notice quotes):

for /D %%A in ("%BaseDir%\*.*") do (

~bp
0
 

Author Comment

by:Osley
ID: 38812880
Perfect!

Thanks for the script - I should've looked closer at the code update you provided.

Thanks. Much appreciated.
0
 

Author Comment

by:Osley
ID: 38812901
I've requested that this question be closed as follows:

Accepted answer: 0 points for Osley's comment #a38812880

for the following reason:

Script performed exactly as desired.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 38812896
Did you mean to close the question this way.  Normally I would have expected you to select my last script post as the solution and award points?

~bp
0
 

Author Comment

by:Osley
ID: 38812898
Apologies. I didn't mean to select it that way...

Thanks for the solution. It's saved loads of time.
0
 

Author Closing Comment

by:Osley
ID: 38812902
Solution worked as requested. Saved loads of time because of it.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 38812905
Welcome, glad that was useful, and thanks for the feedback.

~bp
0
 

Author Comment

by:Osley
ID: 38813055
Hi billprew. One more question - I need to modify the script, so that it creates one folder called 'documents' and with this folder, 3 more folders.

Apologies -
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 38813962
I posted that in the related question.

~bp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

771 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