?
Solved

Windows or linux script to create multiple directories

Posted on 2010-08-16
8
Medium Priority
?
489 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 2000 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

719 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