Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Windows or linux script to create multiple directories

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
Sanga Collins
Asked:
Sanga Collins
  • 5
  • 3
1 Solution
 
t0t0Commented:
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
 
Sanga CollinsSystems AdminAuthor Commented:
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
 
t0t0Commented:
Which code are you using:

   CODE 1 - hard-coded categories

or

   CODE 2 - category.txt file method

?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
Sanga CollinsSystems AdminAuthor Commented:
I used code 2
0
 
t0t0Commented:
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
 
t0t0Commented:
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
 
Sanga CollinsSystems AdminAuthor Commented:
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
 
t0t0Commented:
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now