Siemens Industry Online Support
Siemens AG
Entry type: FAQ Entry ID: 89855157, Entry date: 03/17/2017

How do you use a script to determine whether a storage drive with removable media (USB stick, SD/MM card) is ready for writing on an HMI device?

  • Entry
  • Associated product(s)
This FAQ response includes a sample script which checks whether a storage medium is available on an HMI device and whether or not it is ready for writing. You can change the script to suit your requirements.

Data of an HMI project is to be stored user-triggered on a drive with removable media (USB stick, SD/MM card, etc.) when the system is running. A missing storage medium or a storage medium that is not ready to write can lead to errors in the procedure. You avoid this by checking the status of the drive before starting the write procedure.

Before the planned write procedure you call a function from a Visual Basic Script to check whether the drive is ready for writing. The return value contains the status of the drive based on which you can decide whether to continue or abort the write procedure.
Depending on the operating system used (Windows or WinCE), Visual Basic Script provides different methods for checking whether the removable drive is ready for writing.

No. Procedure
1 Create a script for a function with the following code:
  • For Windows-based devices:

Function MediaCheckWin(ByRef path)

Dim fso, drive, result
Set fso= CreateObject("Scripting.FileSystemObject")

On Error Resume Next
Set drive= fso.GetDrive(fso.GetDriveName(fso.GetAbsolutePathName(path)))

If Err.Number=0 Then
result= 0
result= Err.Number
End If

MediaCheckWin= result

End Function

  • For Windows CE-based devices ("Windows Embedded Compact"):

Function MediaCheckCE(ByRef path)

Dim fso, result

Set fso= CreateObject("Filectl.filesystem")

If fso.dir(path)="" Then
result= 1
result= 0
End If

Set fso= Nothing

MediaCheckCE= result

End Function

The "result" return value can be changed to suit your requirements.


Call the MediaCheckWin() or MediaCheckCE() script each time before you run a write procedure to the medium concerned. As parameter you assign the script the path on which the file to be written is to be located, for example: "e:\" (Windows) or "\Storage Card USB\" (Windows CE).

If you transfer the path as parameter via an input field, make sure that your input is accepted (press Enter key).
If you do not transfer the path as parameter, but define it in the script, then you set the path specification in quotation marks.

3 Depending on the return value you decide whether to continue the write procedure or abort it.
The return value "0" means that the medium was ready for writing when tested; any other return value means that the medium was not ready for writing.
Table 01

TIA Portal sample project KP400 Comfort / PC Runtime with integrated scripts
Registration required storage_media_status.zip (379.4 KB)


  • Note that the scripts only recognize whether an interchangeable medium is available. If, for example, the medium is write-protected or there is no more memory available, the scripts still return the value "0".
  • On Windows CE-based devices it might take a few seconds before the new status is recognized after removing or plugging an interchangeable medium.
Further Information
Basic information on the topic of "Working with system functions and Runtime scripting" is available in the WinCC Professional V15 system manual in Entry ID 109755202.
General information about file operations when scripting with WinCC flexible and devices in Windows CE and Windows is available in entries 26107211 and 26106418.

Additional Keywords
MMC, MMC card, Flash drive, Memory stick, Memory card, Access, Readiness

Security information
In order to protect technical infrastructures, systems, machines and networks against cyber threats, it is necessary to implement – and continuously maintain – a holistic, state-of-the-art IT security concept. Siemens’ products and solutions constitute one element of such a concept. For more information about cyber security, please visit