Solved

Windows or linux script to create multiple directories

Posted on 2010-08-16
8
482 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
  • 5
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
t0t0 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Which code are you using:

   CODE 1 - hard-coded categories

or

   CODE 2 - category.txt file method

?
0
 
LVL 18

Author Comment

by:Sanga Collins
Comment Utility
I used code 2
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Expert Comment

by:t0t0
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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 …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

744 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

12 Experts available now in Live!

Get 1:1 Help Now