Solved

Need C# Help to create a "Push Button" Subfolder Heirarchy with nested Subfolders

Posted on 2009-03-30
8
804 Views
Last Modified: 2012-05-06
I need to make a file system that goes something like this:
Folder Level 1.) Top Folder (Brands - ex. WidgetCoWidgets, HelliCoWhirrleyGigs, etc.)
Folder Level 2.) Second Level Folder (BlueWidgets, GreyWidgets, RedWidgets)
Folder Level 3.) Are all of the Product SKUs (BW001, BWABC, BW3456D)
<!--  I have done the first three levels so far -- >
Here's where I need help ...
For each level 3 folder, I need to add 3 department styled folders (like, Sales, Marketing, Finance)
Then, within each fourth level folder I need to add another array of folders like (ProductBrochures,PriceLists,MarComMaterials).  I am not sure how to script the creation of the level four and five folders.  Is there some sort of silver bullet "For each subfolder in path X, add level 3 folder, then add level four folder?  The problem is, there is no serialization at the level 3 level.  The product names are largely random.  I would appreciate any help you could gibe to get me a simple ASP.NET solution that uses System.IO to manipulate all of this ...  
0
Comment
Question by:dude727
  • 6
  • 2
8 Comments
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
This will perform the task on the given foldernames:

@echo off
for %%a in (WidgetCoWidgets HelliCoWhirrleyGigs) do (
   if not exist %%a md %%a
   for %%b in (BlueWidgets GreyWidgets RedWidgets) do (
      if not exist %%a\%%b md %%a\%%b
      for %%c in (BW001 BWABC BW3456D) do (
         if not exist %%a\%%b\%%c md %%a\%%b\%%c
         for %%d in (Sales Marketing Finance) do (
            if not exist %%a\%%b\%%c\%%d md %%a\%%b\%%c\%%d
            for %%e in (ProductBrochures PriceLists MarComMaterials) do (
               if not exist %%a\%%b\%%c\%%d\%%e md %%a\%%b\%%c\%%d\%%e
)))))
0
 
LVL 16

Accepted Solution

by:
t0t0 earned 250 total points
Comment Utility
Another way to do it is as follows:

@echo off
for %%a in (WidgetCoWidgets HelliCoWhirrleyGigs) do (
   if not exist %%a md %%a
   cd %%a
   for %%b in (BlueWidgets GreyWidgets RedWidgets) do (
      if not exist %%b md %%b
      cd %%b
      for %%c in (BW001 BWABC BW3456D) do (
         if not exist %%c md %%c
         cd %%c
         for %%d in (Sales Marketing Finance) do (
            if not exist %%d md %%d
            cd %%d
            for %%e in (ProductBrochures PriceLists MarComMaterials) do (
               if not exist %%e md %%e
            )
            cd..
         )
         cd..
      )
      cd..
   )
   cd..
)


Or you could close up the closing brackets as in:

@echo off
for %%a in (WidgetCoWidgets HelliCoWhirrleyGigs) do (
   if not exist %%a md %%a
   cd %%a
   for %%b in (BlueWidgets GreyWidgets RedWidgets) do (
      if not exist %%b md %%b
      cd %%b
      for %%c in (BW001 BWABC BW3456D) do (
         if not exist %%c md %%c
         cd %%c
         for %%d in (Sales Marketing Finance) do (
            if not exist %%d md %%d
            cd %%d
            for %%e in (ProductBrochures PriceLists MarComMaterials) do (
               if not exist %%e md %%e
) cd.. ) cd.. ) cd.. ) cd.. )


0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
To add random product names, you need to run this batch file passing it the product name as a command line parameter. You could also call this as a sub-routine inside an existing batch file or as an external batch file.

Example 1 (Run on the command line):

   ADDITEM BW002


Example 2 (External batch file CALLed from within another batch file):

   call additem BW002


Example 3 (Sub-routine CALL within a batch file):

   call :additem BW002


::ADDITEM.BAT
@echo off
if "%1"=="" exit /b 1
for %%a in (WidgetCoWidgets HelliCoWhirrleyGigs) do (
   for %%b in (BlueWidgets GreyWidgets RedWidgets) do (
      if not exist %%a\%%b\%1 md %%a\%%b\%1
      for %%d in (Sales Marketing Finance) do (
         if not exist %%a\%%b\%1\%%d md %%a\%%b\%1\%%d
         for %%e in (ProductBrochures PriceLists MarComMaterials) do (
            if not exist %%a\%%b\%1\%%d\%%e md %%a\%%b\%1\%%d\%%e
         )
      )
   )
)
0
 

Author Comment

by:dude727
Comment Utility
Is there a way though to wildcard level 3 (product SKUs as above), rather than passing a string (BW001 BWABC BW3456D, etc.), as I have thousands of product folders to make the subfolder structure for? That would also mean I would need to know the level name of every folder, which I don't ...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:dude727
Comment Utility
Another thought, perhaps I have this in the wrong zone ... While batch files are OK, is there a way to do this in C# - please keep in mind, also, the folder strings at level 5 will vary by level 4 .... in other words, the level 4 Marketing folder may have a subfolder called SellSheets, while a level 4 "Finance" folder may have a child called "Cost Analysis".  They aren't all the same ... Any help you can offer would be appreciated ...
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
The ADDITEM.BAt I wrote will create any randomly named folder at the third level and all sub-folders at higher levels.

If it fails to work on your system for some reason then please let me know...

0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
I just did a tree of folders created by the first batch file and then added a product BW002. As can be seen, all appears to be well however, if there needs to be adjustments please confirm them here...


tree.txt
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Is there any particular reason why you would want to use a high level language such as C to perform this task? DOS batch files are more than adequate at doing what you want however, if you later want to, you can compile the batch file to an executable .EXE program with utilities such as BAT2EXE.

I program in C and C++ however, this particular task is best suited to DOS batch files. In C, there is so much to check for whereas DOS is designed to operate directly on file and folder structures.

I haven't yet encountered a problem I couldn't perform using DOS batch files.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

10 Experts available now in Live!

Get 1:1 Help Now