CREATE MACRO TO IMPORT STOCK MODEL

CREATE MACRO TO IMPORT STOCK MODEL

diegoverciano85
Enthusiast Enthusiast
2,043 Views
11 Replies
Message 1 of 12

CREATE MACRO TO IMPORT STOCK MODEL

diegoverciano85
Enthusiast
Enthusiast

Due to the wide variety of projects in various situations, we export stock models for other programmers to import if necessary.

Instead of looking in windows explorer, I would like to create a macro where I would type in the name of the project (first 4 digits of the file) and it would list only the files with these conditions where I would choose the one and import it.

 

 

Accepted solutions (1)
2,044 Views
11 Replies
Replies (11)
Message 2 of 12

TK.421
Advisor
Advisor

try this:

 

STRING filePath = "C:/Users/David/Desktop/test"
STRING LIST $stockFiles = list_files('files', $filePath, '.stkmd')
STRING fileName = INPUT "Enter File Name"

 

//create a new list stockNames
STRING LIST $stockNames = {}
FOREACH file IN stockFiles {
     IF position(file, fileName) > -1 {
          INT $Ok = add_last($stockNames, TOKENS($File, "/")[SIZE(TOKENS($File, "/"))-1])
     }
}

 

//display desired in pulldown list
INT $C = INPUT CHOICE $stockNames "Select Stock File"


the numbers never lie
0 Likes
Message 3 of 12

diegoverciano85
Enthusiast
Enthusiast
Message 4 of 12

TK.421
Advisor
Advisor

Great! you figured out the error? What was it?


the numbers never lie
0 Likes
Message 5 of 12

diegoverciano85
Enthusiast
Enthusiast

I had edited the wrong way. Then I saw the error and fixed it and then it worked. So I edited the response where it said it was wrong to correct. To import the chosen file as a stock model should I insert a command in the macro?

0 Likes
Message 6 of 12

TK.421
Advisor
Advisor

sorry, yes. put this at the end of your macro:

IMPORT STOCKMODEL ${$stockFiles[$C]}

 


the numbers never lie
0 Likes
Message 7 of 12

diegoverciano85
Enthusiast
Enthusiast

@TK.421 

 

Now testing the macro at work, I report the following problem. In the list that is created after typing, for example, 1404, I chose the fourth item in this list, but accepting it does not matter this item, but the fourth file in the folder in question (in alphabetical order).

 

Attached image with the reported error

0 Likes
Message 8 of 12

TK.421
Advisor
Advisor

Yes, I see the error in my logic. Give me a few minutes to fix it

 


the numbers never lie
0 Likes
Message 9 of 12

TK.421
Advisor
Advisor
Accepted solution

ok, try this. I cleaned it up a little and added some better comments for you.

 

//---------- initialize variables: ---------//
// -the filepath of the stock model folder  //
// -a list of all the .stkmd files only     //
// -the file name prefix                    //
// -declare empty variable to hold the path //
//------------------------------------------//
STRING filePath = "C:/Users/David/Desktop/test"
STRING LIST $stockFiles = list_files('files', $filePath, '.stkmd')
STRING fileName = INPUT "Enter File Name"
STRING stockModelPath = ""

// create a new list for only the 
// names with the input prefix
STRING LIST stockNames = {}

// loop through the directory and add 
// those to the list 
FOREACH file IN stockFiles {
	// if the name contains the prefix, add
	// it to the list
	IF position(file, fileName) > -1 {
        	INT $Ok = add_last(stockNames, TOKENS(file, "/")[SIZE(TOKENS(file, "/"))-1])
	}
}

// display desired in pulldown list
INT i = INPUT CHOICE $stockNames "Select Stock File"

// create the file path for the 
// selected stock model
$stockModelPath = $filePath + "/" + $stockNames[$i]

// import the stock model
IMPORT STOCKMODEL $stockModelPath

the numbers never lie
0 Likes
Message 10 of 12

diegoverciano85
Enthusiast
Enthusiast

Now it's perfect. You are awesome. Thank you very much.

0 Likes
Message 11 of 12

TK.421
Advisor
Advisor

great! sorry I messed up earlier, I should've paid more attention when I was testing it.


the numbers never lie
0 Likes
Message 12 of 12

diegoverciano85
Enthusiast
Enthusiast

Don't worry, your willingness to help those who are not easy with macros is the most important.

0 Likes