Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to check whether drive A is ready or not?
Message
From
05/08/1999 15:48:32
 
 
To
05/08/1999 15:05:14
General information
Forum:
Visual FoxPro
Category:
Troubleshooting
Miscellaneous
Thread ID:
00250330
Message ID:
00250359
Views:
14
>Could anybody tell me how to check whether drive A is ready or not? I have to determine it before read/write.

There is sample code in the Files Section that shows how to use an API call to do this.

You can use the Scripting.FileSystemObject's Drives collection to check, too; Scripting.FileSystemObject is a part of the WIndows Scripting Host:

oFSO = CREATEOBJ('Scripting.FileSystemObject')
oMyADrive = oFSO.GetDrive('A:\')
? oMyADrive.IsReady && true if ready, false if not

Another trick is to use low-level file I/O, and trap an error when you try to create a file. The following code fragment actually tests if the path specified is writable, but it gives you an idea of how to approach the problem using just VFP:
LPARAMETER cPathToTestLOCAL nFileHandle, cRandomName, cErrorHandler
IF TYPE('cPathToTest') # 'C'
   RETURN .F.
ENDIF
cRandomName = SUBST(SYS(2015),3,8)+'.WRT'
cPathToTest = ALLTRIM(cPathToTest)
IF RIGHT(cPathToTest,1) # '\'
	cPathToTest = cPathToTest + '\'
ENDIF
*
*  Certain types of errors such as "Media not present" will present an error back
*  to VFP rather than simply failing the call and returning a -1.  To handle this,
*  we will disable the current error handler for the duration of the FCREATE() and 
*  set the value of nFileHandle to the error condition if an error occurs, and
*  we will restore the error handler after the FCREATE()
*
cErrorHandler = ON('ERROR')
ON ERROR nFileHandle = -1
nFileHandle = FCREATE(cPathToTest+cRandomName)
ON ERROR &cErrorHandler
*
*  FCREATE() returns a -1 if it failed, telling us we don't have to close
*  the handle.  If it succeeds, we have to close the handle and delete the 
*  0 byte file created by the low-level I/O calls.
*
IF nFileHandle # -1
	=FCLOSE(nFileHandle)
	IF FILE(cPathToTest+cRandomName)
		ERASE (cPathToTest+cRandomName)
	ENDIF
	RETURN .T.
ELSE
	RETURN .F.
ENDIF
EMail: EdR@edrauh.com
"See, the sun is going down..."
"No, the horizon is moving up!"
- Firesign Theater


NT and Win2K FAQ .. cWashington WSH/ADSI/WMI site
MS WSH site ........... WSH FAQ Site
Wrox Press .............. Win32 Scripting Journal
eSolutions Services, LLC

The Surgeon General has determined that prolonged exposure to the Windows Script Host may be addictive to laboratory mice and codemonkeys
Previous
Reply
Map
View

Click here to load this message in the networking platform