From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Get current data file folder name

Solved!
Go to solution

Hi,

 

Is there any way I can get the folder name for the currently loaded data file using scripting language?

 

I am really struggling to understand the object model and exactly what properties are available for what object. The help files reference seemingly useful functions and properties like fullpath, folder, getfolder, etc, but the script examples are never quite useful enough to know how to use them.

 

On a related point, if I have a datafile open, how do I find the properties for that file? I assume that somewhere I can see the name of the file, the path, number of groups, etc, etc.

 

Thanks.

0 Kudos
Message 1 of 11
(2,219 Views)
Solution
Accepted by topic author Simon_Aldworth

Hello Simon,

 

the only one way I know is to get the properties "sourcedatafilename" and "sourcedatafilepath" from one of the loaded channels. No idea why it's not in the property of the loaded file...

 

Sometimes if I don't know exactly the name of a property (we have some additional), a small check helps, something like this:

dim prop

'for each prop in data.Root.Properties
'for each prop in data.Root.ActiveChannelGroup.Properties

for each prop in data.Root.ActiveChannelGroup.Channels(1).Properties
  call msgbox (prop.name & " = " & prop.Value)'
next

 

Just look either the props of data root, group or channel, to find out what you need.

Then you can use this info in your code.

Hope it helps.

 

Greetings,

Vassili

0 Kudos
Message 2 of 11
(2,204 Views)

Thanks Vassili, and thanks for the useful bit of code to get the properties.

 

How do I use properties? For example, I see the following piece of code in the help files:

Function GetFolderObject(sFolder)
  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set GetFolderObject = fso.GetFolder(sFolder)
End Function

This reads as if I can use it to obtain a folder from a path. I can get the path from the channel property list using sourcedatafilepath. However, I assume I would need the path as a string to pass to the above function but I don't seem to be able to extract the sourcedatafilepath property as a string. The following returns NOVALUE.

myfolder = Str(data.Root.ActiveChannelGroup.Channels(1).Properties("sourcedatafilepath"))

 What should I do to correct my code?

Regards.

0 Kudos
Message 3 of 11
(2,193 Views)

Hi Simon_Aldworth,

 

In the DIAdem help you find information about

 

The DIAdem Folder Structure

DIAdem Path Variables

 

The path variables, for example DataReadPath are only changed while working interactively or set via script. When you load a file from a path into a Load dialog box or save a file to a path in a Save dialog box, DIAdem changes the respective user path variables. When loading or saving with a script, the user path variables remain unchanged.

With that said, in a script you are responsible for the paths.

 

Each channel in the Dataportal contains in history properties information about the file type, path, filename, etc. It is necessary to have this information on channel level because it is possible to load channels from different file selective.

Walter_Rick_0-1660812433806.png

 

In script you find the property name easily by D&D the property of interest into the script editor.

 

dim oChn 
set oChn = Data.Root.ChannelGroups(1).Channels("Speed")

msgbox oChn.Properties("sourcedatafilepath").Value

 

Greetings

Walter

0 Kudos
Message 4 of 11
(2,186 Views)

Hi Vassili,

 

I think I may have worked it out using trial and error. How does this look to you:

Option Explicit  'Forces the explicit declaration of all the variables in a script.
dim mypath, myfolder, fso
mypath = data.Root.ActiveChannelGroup.Channels(1).Properties("sourcedatafilepath").Value
Set fso = CreateObject("Scripting.FileSystemObject")
Set myfolder = fso.GetFolder(mypath)
logfilewrite myfolder.Name

It's a bit long winded but perhaps just shorter than trimming the folder name from the path string!

Regards.

0 Kudos
Message 5 of 11
(2,185 Views)

Well, in the initial post you just asked about the different properties and how to get the values.

Nothing about purposes 🙂

I guess, depending on your purpose you select one way from several possibles.

 

When we need to run some corrections on a set of files, we select the files before loading (i.e. use file dialog), load the files separately, run corrections, and then save the files in the same folder again. Since the file was selected, the dialog stored the path, and you can get it immediately.

DlgState = FileDlgShow(MyFolders(0),"*.tdm","File Selection",TRUE)
If DlgState = "IDOk" Then
  For iLoop = 0 to ubound(FileDlgNameList)
    Call DataFileLoad(FileDlgNameList(iLoop),"","")
    '
    'Call any recalculation sub/function
    Call Portal.Refresh
    Call DataFileSave(Namesplit(FileDlgNameList(iLoop), "P") & Data.Root.Name & ".TDM", "TDM")
    Call Data.Root.Clear
    '
  Next
Else
  Call MsgBoxDisp("Data loading has been aborted.@CRLF@Script is finished.")
End If

 

Working with FSO, we sometimes create folders, but sometimes they already exist.

For example, I export the report pictures as PNGs in a folder named as the test run.

In such case I delete the "old" content first, if available:

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(desctop)
if fso.FolderExists(desctop& test) then
	fso.DeleteFolder(desctop& test)
end if
call fso.CreateFolder(desctop& test)	

 

The helpfiles (at least about FSO functionals) could be better...

0 Kudos
Message 6 of 11
(2,175 Views)

Thanks for the update.

 

Yes, I didn't state my purpose, but I did say that I only wanted the folder name, not the path. In that respect, I don't think your first option will help. However, your second option is effectively the same as the one I proposed and is the one I'll use.

 

Regards.

0 Kudos
Message 7 of 11
(2,167 Views)

Hi,

 

I've returned to working on this and discovered that whilst the method I proposed works, in the sense that it gives me the folder from which the data was loaded, the variable sourcedatafilepath does not update after the data file is saved to a different location.

 

The only way I've found of updating sourcedatafilepath is to close and reopen the file. This is somewhat clunky so is there a way of updating it to reflect the actual location of the file loaded in the portal?

 

Thanks.

0 Kudos
Message 8 of 11
(1,740 Views)

Hi Simon_Aldworth,

 

I think it is correct not to change the content of the "sourcedatafilepath" property. As the property name suggests, it is the file path of the source data - that is, where you loaded the data from.

 

Greetings

Walter

0 Kudos
Message 9 of 11
(1,736 Views)

Hi Simon.

 

I agree with Walter; this property is the source of the file, and should not be changed by storage of the data anywhere else, at least unless you load it again.

 

But:

if you need the new path (where you save the file), you can store this information in any additional property. For example, Diadem has "registertxt1" (at least until version 2019), so you can use it.

 

Call DataFileSave(MyFolders(0)&"test_data.tdms", "TDMS", True)
prop = namesplit(datafilename, "P")
Data.Root.ActiveChannelGroup.Channels(1).Properties("registertxt1").Value = prop

call msgbox (prop)

After re-saving of the file (well, better before, but for this example it's easier that way) you update the value of this property, and can use it...

 

Or you just create a new property, even directly for the data.root.

 

Would it work for you? 

 

Greetings,

Vassili

0 Kudos
Message 10 of 11
(1,718 Views)