Solved

Windows or linux script to create multiple directories

Posted on 2010-08-16
8
487 Views
Last Modified: 2012-05-10
Hi EE,

I have a client that is finally going to digitize all his paper work and put it on a file server. Basically they have about 1200 clients with each having several different categories of documents (eg billing, invoices etc...)

i am not very good at scripting, but think this would be the way to go.

I would like a script example that would a) create all the client directories from a spreadsheet or CSV. and b) create several predefined folders in each client directory.

The script can be windows vbs, bat or linux. i have no preference either way.

any help would be much appreciated
0
Comment
Question by:Sanga Collins
[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
  • 5
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
t0t0 earned 500 total points
ID: 33451239
Easy!

Have a list of clients. This could be a simple text file - one client on each line. Call this file CLIENT.TXT

If all the clients have the same set of sub-folders then there are two approaches.

   1) if there are just a handful of sub-folders then these can be hard-coded into the batch file.

   2) if there are more than a handful of sub-folders then list them in a separate file. Call this file CATEGORY.TXT.

NOTE: Make sure you save all three files in the same folder.

Finally, choose where all your client folders are 'rooted' from. For now, let's say they are rooted from the C:\CLIENTS\ folder.

Then, copy and paste this code into Notepad and save it as a batch file. Call this batch file MAKECLIENTS.BAT.

NOTE: The 'title=...' statement displays a visual indication that the batch file is running. This can be removed if you so wish.

NOTE: In the 'set Category...' statement, use double-quotes around categories which include spaces in their names (see code for example).



CODE 1 - hard-coded categories
--------------------------------------

@echo off
set Clients=C:\CLIENTS
set Categories=Billing, Invoices, "VAT Returns", Estimates, Transactions

mkdir "%Clients%\" 2>nul

for /f "tokens=*" %%a in (client.txt) do (
   for %%b in (%Categories%) do (
      title %Clients%\%%a\%%b
      mkdir "%Clients%\%%a\%%~b\"
   )
)
exit /b



CODE 2 - uses 'category.txt' file
--------------------------------------
@echo off
set Clients=C:\CLIENTS

mkdir "%Clients%\" 2>nul

for /f "tokens=*" %%a in (client.txt) do (
   for /f "tokens=*" %%b in (category.txt) do (
      title %Clients%\%%a\%%b
      mkdir "%Clients%\%%a\%%b\"
   )
)
exit /b


0
 
LVL 18

Author Closing Comment

by:Sanga Collins
ID: 33451539
Simple but brilliant ... saved me 5 hours of reasearch!!!

one question, after intial run, what modification can i make so that script checks for root directory and just adds new clients folders + sub folders?
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33452566
Which code are you using:

   CODE 1 - hard-coded categories

or

   CODE 2 - category.txt file method

?
0
Technology Partners: 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!

 
LVL 18

Author Comment

by:Sanga Collins
ID: 33453583
I used code 2
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33472708
The simplest way is to add a new client name to your CLIENT.TXT file and re-run the following slightly modified code.


@echo off
set Clients=C:\CLIENTS

mkdir "%Clients%\" 2>nul

for /f "tokens=*" %%a in (client.txt) do (
   if not exist "%Clients%\%%a\" (
      for /f "tokens=*" %%b in (category.txt) do (
         title %Clients%\%%a\%%b
         mkdir "%Clients%\%%a\%%b\"
      )
   )
)
exit /b
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33472782
Another way is to run this code instead. If the batch file is named MAKECIENTS.BAT then you need to run it like this:

   MAKECLIENTS /add "JohnDoe"

where 'JohnDoe' is the new client name.


@echo off
set Clients=C:\CLIENTS

if not "%~1"=="" (
   if /i "%~1"=="/add" (
      for /f "tokens=*" %%b in (category.txt) do (
         mkdir "%Clients%\%~2\%%b\" 2>nul
      )
      exit /b 0
   ) else (
      echo SYNTAX: %0 [/add ^"client_name^"]
      exit /b 1
   )
   exit /b 0
)

for /f "tokens=*" %%a in (client.txt) do (
   for /f "tokens=*" %%b in (category.txt) do (
      title %Clients%\%%a\%%b
      mkdir "%Clients%\%%a\%%b\" 2>nul
   )
)
exit /b



This can easily be modified to add / remove / rename categories or clients.
0
 
LVL 18

Author Comment

by:Sanga Collins
ID: 33474288
THank you very much!

i wish there was a way to add more points to the question ... you have certainly made me look good infront of my client :)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33477765
Great stuff! The points don't matter as much as the 'ACCEPTED' award when closing the question.

If you need to modify the code in the future say, to add more functionality ie, remove or rename a client, then post a new question and place a link to it in a comment in this thread so that I can be alerted.

An archive and synchronise function is also possible.

Finally, if your client needs the occasional message output confirming actions then they can easily be added with almost no efforts whatsoever.

Thank you.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Utilizing an array to gracefully append to a list of EmailAddresses
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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