Solved

Windows or linux script to create multiple directories

Posted on 2010-08-16
8
485 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
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

860 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