Skip to Content

At times there would be some requirement to create folders dynamically, particularly when the output needs to be split into different files and stored in separate locations.

In this document i am explaining a scenario where there are 10 plant names and we need to create folders for all the 10 plant names to store the corresponding output files. Below is an example table with 10 plant names –

Row_Id Plant_names
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J

I have to create 10 folders with plant names on it, like A, B, C etc..

You have to declare three global variables – $COUNT (for looping from row_id 1 to 10), $MAX_ROW (to end the loop at max row number) and $PLANT_NAME for capturing the 10 plant names in the loop one by one.

Place a script after the dataflow and insert the below code.

$COUNT = 1; # start the count at 1

$MAX_ROW = sql(‘datastorename’,’select max(ROW_ID) from tablename’); # capture the rowid in a variable

print( $MAX_ROW ); # optional to see the row nos

while ( $COUNT <= $MAX_ROW) # initialize the looping

          begin

          $PLANT_NAME = sql(‘datastorename’,’select PLANT_NAME from tablename where ROW_ID = [$COUNT]’); # selecting plant names one by one

          print($PLANT_NAME); # optional capture of plant name

          exec( ‘cmd’, ‘mkdir C:\Users\XYZ\[$PLANT_NAME]’,8 ); # create folder using dos command mkdir

          $COUNT = $COUNT + 1; # looping with counting row id

          end # end of loop

This will generate 10 folders with names A, B, C etc..

Hope this helps.

Arun

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Michael Eaton

    Why don’t you modify the example so that it is dynamic based on the values, rather than the max value?  What if there are gaps in the sequence?

    The modification is simple, and a common pattern for iterating over a dataset in Data Services

    1. get the minimum value for x
    2. loop while x is not null
      1. get minimum x > previous x

    Michael

    (0) 
    1. Arun Kumar Post author

      Yes, that works. My case in the design was for fixed record sequence nos with known values. I was eliminating all the gaps and capturing the plant values in a lookup during run time, which is then iterated in the script. The same can be customized based on the requirement.

      Arun

      (0) 

Leave a Reply