Cancel
Showing results for 
Search instead for 
Did you mean: 

if notexist, create directory in windows

Genius
Genius

Hi community,

 

I am trying to create a template for our team that is as fool-proof as possible. In one of my methods, I use the 'writeExcelFile' method to export data from the model. For organization purposes, I have the file saved into a sub-directory in which the model exists. This works fine as long as that sub-directory was already created before the code is executed.

 

My question is this, is there a way with SimTalk to create a sub-directory in windows if it doesn't already exist? I've combed through the forum and the help file and haven't found any such tool.

 

Any help would be greatly appreciated. Thank you!

 

EMM

5 REPLIES

Re: if notexist, create directory in windows

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

you could try the command system; with system you can send operation system commands

 

So you could try to change in your directory; if this fails system returns 1. In this case you create the directory e.g.

 

if  system("cd d:\\data1") = 1 then 
 system("mkdir d:\\data1")
end

Steffen Bangsow
freelance simulation specialist  
web: www.bangsow.eu
mail: steffen@bangsow.net

Re: if notexist, create directory in windows

Genius
Genius

Awesome! Thanks Steffen!

 

I used this to reference the model's directory: 

 

if  system("cd data1") = 1 then 
 system("mkdir data1")
end

Re: if notexist, create directory in windows

Gears Phenom Gears Phenom
Gears Phenom

Hello

 

You can also do something like the following to do the same without any pop up windows being shown.

 

var saveLocation: String := copy(modelFile, 1, strRpos("\\", modelFile)) + "Data1"
if NOT existsFile(saveLocation)
startExtProc(To_Str("cmd.exe /C mkdir \"", saveLocation, "\""), false, true)
end

JamesM

Re: if notexist, create directory in windows

Genius
Genius

Hi James,

 

I actually didn't get a pop-up when I ran that code, but it's good to know that there is another way to do it.

 

Thanks.

 

EMM

Re: if notexist, create directory in windows

Gears Phenom Gears Phenom
Gears Phenom

Hello

 

Oh ok, normally when running stuff with the System command it shows me a pop up window while it is running. Guess what is running is so quick that you cant see it.

 

JamesM