How-to
How to Create Folders From WinCC Professional Input Fields
Short answer
Bind each input field to an internal tag, read the three tags with SmartTags or the HmiRuntime.Tags read method, and join the values with backslashes under a fixed root that the script sets. Validate every segment as a Windows folder name, then create the hierarchy one level at a time with FolderExists and CreateFolder from Scripting.FileSystemObject.

On this page
- How do I read WinCC Professional input fields in a VB script?
- How do I assemble three field values into one folder path?
- Which characters must I reject in a folder name?
- How do I create the folder hierarchy level by level?
- How do I test the script at runtime?
- Why can the runtime fail to create folders even with a valid path?
- Questions asked
Bind every input field to an internal tag, read those tags in the button's script, join the values with backslashes under a fixed root, and create the folders one level at a time with FolderExists and CreateFolder from Scripting.FileSystemObject. With the example entries name, age and phone_number the result is C:\name\age\phone_number. The screen fields and your script share no variable: the HMI tag each field writes is the only data boundary between them. The steps below read the tags, reject anything that is not a plain Windows folder name, assemble the path, create each level in order and prove the result with a repeat run.
How do I read WinCC Professional input fields in a VB script? #
Bind each input field to an internal HMI tag and read that tag by name with SmartTags: SmartTags("Tag_1") returns the value the field wrote, and HmiRuntime.Tags("Tag_1").read returns the same value through the runtime tag object.
Both notations are shown for WinCC Professional, and the difference is what you get back: SmartTags hands you the value directly, HmiRuntime.Tags hands you a tag object whose value you fetch with .read. Pick one notation and stay with it for the whole script; a comparison of the HMIRuntime.Tags and SmartTags API in WinCC works through the differences.
Tag_1, Tag_2 and Tag_3 are example tags: three internal tags you create and bind to the three fields, one per field. A field's text reaches its tag only when the entry is committed, so a script that fires too early reads the previous content. Read every tag by its literal name; a tag name assembled at runtime is what stands behind concatenated SmartTags names returning zero in WinCC Advanced.
Dim segment1, segment2, segment3
segment1 = SmartTags("Tag_1")
segment2 = SmartTags("Tag_2")
segment3 = SmartTags("Tag_3")
segment1 to segment3 are my example variables: VBScript declares them with Dim as Variant, empty until the assignment line, and you rename them freely.
How do I assemble three field values into one folder path? #
Concatenate a fixed root and the three tag values with backslashes, keeping every level in its own variable so the creation step can test each one: the example entries build C:\name, C:\name\age and C:\name\age\phone_number.
Dim rootPath, level1, level2, level3
rootPath = "C:\"
level1 = rootPath & segment1 ' "C:\" + "name" = C:\name
level2 = level1 & "\" & segment2 ' "C:\name" + "\" + "age" = C:\name\age
level3 = level2 & "\" & segment3 ' "C:\name\age" + "\" + "phone_number"
That is the whole arithmetic: two & operators per line, one to attach the backslash and one to attach the field text. Keep the root inside the script and out of the fields, so operator text can only ever name levels below a root you approved; a typed drive letter with its colon is rejected in validation before it reaches the disk. Before the first creation run, write level3 to the trace, which the script reaches through the HmiRuntime object, and compare it with what you expect: a doubled or missing backslash means a concatenation error, an unexpected segment sends you back to the tag bindings.
Which characters must I reject in a folder name? #
Reject an empty value, the entries . and .., and any segment containing a backslash, forward slash, colon, asterisk, question mark, double quotation mark, less-than sign, greater-than sign or vertical bar, before a single CreateFolder runs.
Those nine characters are either path separators or illegal in ordinary Windows folder names, and . or .. does not even fail: it silently changes the destination instead of creating the requested hierarchy. Also refuse the reserved device names Windows will not accept as ordinary folder names: CON, PRN, AUX, NUL, COM1 through COM9 and LPT1 through LPT9. Validation belongs ahead of creation, not after it: with checks placed after the first CreateFolder, a valid level one lands on disk and an invalid level three then stops the script, leaving a partial tree for the next run to trip over. This check is the safety element of the script, because it is the one place operator input touches the file system.
| What the tag returns | What it means | Next action |
|---|---|---|
Plain text such as name |
A valid segment | Build the level |
| Empty string | Field not committed, or wrong tag bound | Stop and fix the binding |
| A separator or one of the nine characters | Input could redirect or break the path | Reject before creation |
. or .. |
The destination would silently change | Reject before creation |
Function SegmentOk(s)
Dim i, ch
SegmentOk = True
If Len(s) = 0 Then SegmentOk = False
If s = "." Or s = ".." Then SegmentOk = False
For i = 1 To Len(s)
ch = Mid(s, i, 1)
' backslash, slash, colon, asterisk, question mark, quote, bar
If InStr("\/:*?""|", ch) Then SegmentOk = False
' Chr(60) and Chr(62) are the less-than and greater-than signs
If ch = Chr(60) Or ch = Chr(62) Then SegmentOk = False
Next
End Function
SegmentOk is my example function: it returns True for a usable segment and False otherwise, and you add the reserved device names as further comparisons on the uppercased segment.
How do I create the folder hierarchy level by level? #
Create the file-system object once with CreateObject("Scripting.FileSystemObject"), then for each of the three level variables call FolderExists first and call CreateFolder only where that test returns False.
- Read the three bound tags into
segment1tosegment3. - Run
SegmentOkon each segment and stop on the first failure. - Build
level1,level2andlevel3from the fixedrootPath. - Create the object with
CreateObject("Scripting.FileSystemObject"). - Test and create the levels in order, parents first.
- Read
FolderExistsonlevel3once more before you report success to the operator.
Dim oFSO
If SegmentOk(segment1) And SegmentOk(segment2) And SegmentOk(segment3) Then
rootPath = "C:\"
level1 = rootPath & segment1
level2 = level1 & "\" & segment2
level3 = level2 & "\" & segment3
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FolderExists(level1) Then
oFSO.CreateFolder level1
End If
If Not oFSO.FolderExists(level2) Then
oFSO.CreateFolder level2
End If
If Not oFSO.FolderExists(level3) Then
oFSO.CreateFolder level3
End If
End If
FolderExists returns True when the folder exists and False when it does not, which is what makes a second run harmless. When the script later goes on to write a file inside the finished tree, the same parent-by-parent discipline applies, as shown for splitting a path and building folders in WinCC VBScript. Which calls the runtime accepts from a script at all is a separate line, drawn by what Visual Basic can and cannot do inside a WinCC project.
How do I test the script at runtime? #
Enter name, age and phone_number, trigger the script once, and expect the final path to equal C:\name\age\phone_number with FolderExists returning True for all three levels.
- Enter the three values and confirm in the trace that each tag returns exactly the visible field text.
- Delete any leftover test tree, run the creation once and expect all three levels on disk.
- Run the same event again and expect no duplicate hierarchy and no creation attempt, because every
FolderExiststest is already True. - Clear one field, or type a colon into one, and expect the script to stop in validation with nothing created.
- Point
rootPathat a scratch folder for the bench runs and set the production root only after the first four steps pass.
Five passes and the pattern is ready to deploy: the root from the script, the levels from validated field text, creation parents first.
Why can the runtime fail to create folders even with a valid path? #
The Windows account running WinCC Runtime needs create permission on the root, and rights you exercised interactively under your engineering login do not transfer to that runtime account.
A string-perfect path still fails when that account cannot write below the chosen root; it is the same class of fault as why a WinCC runtime VBScript runs but does not copy a CSV, where the script executes and the file operation quietly never happens. Prove the rights under the runtime account itself: create a folder by hand while logged in as that account, or confirm which account the runtime runs under and what create rights it holds on the root. Where writing directly below C:\ is restricted at the installation, choose an approved writable parent and change rootPath; the final location is decided by the string and by that account's permissions together. The decision for every case stays the same: the root comes from the script, the three levels come from validated field text, and creation proceeds only after the runtime account has proven write access to the root.
Questions asked
Why does SmartTags return an old value from an input field?
WinCC has a setting named SmartTags reads values from the cache; with it active, a script can see the previous field content instead of the committed text. Turn it off for commissioning, or trigger the script only after all three fields are committed, then confirm in the trace that each tag returns exactly the visible field text.
Can I create all three folders with a single CreateFolder call?
Only when the parent of the deepest folder already exists. CreateFolder makes exactly one folder and fails when its parent is missing, so one call on the full three-level path stops while the first level is still absent. Keep the three FolderExists and CreateFolder pairs: they cost nothing once the levels exist.
Which event should trigger the folder script?
The button the operator presses after filling all three fields. An event on a single input field fires while the other two tags still hold old or empty content, so validation stops the run or the path gets built from mixed entries. The button press is the one moment all three values are committed together.

