Page 1 of 3

How to check if a drive exists

Posted: Mon Mar 31, 2008 5:51 pm
by Otto
How to check if a drive exists.

Thanks in advance

Otto

Posted: Mon Mar 31, 2008 6:07 pm
by James Bott
Try:

lisDir( cDrive +":\" )

Or, make it into a function:

function isDrive( cDrive )
return lisDir( cDrive +":\")

James

Posted: Mon Mar 31, 2008 6:33 pm
by Robert Frank
Otto

Sometimes before you want to check network mapped drive you should reconnect to it.

In this situation in Windows I use:
hWINDG:=WinExec('explorer.exe o:',2)
but it opens new window.

Does somebody know how to check that network drive is connected?

Posted: Mon Mar 31, 2008 6:47 pm
by James Bott
Robert,

>Does somebody know how to check that network drive is connected?

Try this:

//--- Is drive ready?
FUNCTION ISREADY( cDrive )
LOCAL cCurPath := CURDRIVE() + ":\" + CURDIR()
LOCAL lReady := LCHDIR( cDrive + "." )
lSuccess:=.f.
IF lReady
LCHDIR( cCurPath )
lSuccess:= .T.
ENDIF
RETURN lSuccess

Posted: Mon Mar 31, 2008 7:39 pm
by Otto
Hello James,
thank you for your answer. With your ISREADY( cDrive ) I get on one of my VISTA PC if DRIVE is not ready this error on the 2nd the same exe works:

Image

Posted: Mon Mar 31, 2008 8:27 pm
by Antonio Linares
Otto,

Please modify James function this way to check where the GPF is coming from:

Code: Select all

FUNCTION ISREADY( cDrive ) 

   local cCurPath, lReady, lSuccess := .F.

   MsgInfo( CurDrive(), "CurDrive()" )
   MsgInfo( CurDir(), "CurDir()" )
   cCurpath = CURDRIVE() + ":\" + CURDIR() 

   MsgInfo( lReady := lChDir( cDrive + "." ), "lChDir()" )

   if lReady 
      MsgInfo( lSuccess := LCHDIR( cCurPath ), "LCHDIR( cCurPath )" )
   endif
 
return lSuccess

Posted: Mon Mar 31, 2008 8:45 pm
by Otto
Antonio, the error comes
if I insert for example msginfo(lCHDir("m:\"). M is no valid drive.
I get also an exception with LIsDir("").
Regards,
Otto

Posted: Mon Mar 31, 2008 8:51 pm
by Otto
I forgot to mention if I click on abort then the LCHDir is executed correctly.
The program does not fail – the exception is only a kind of a msgbox I from WINDOWS.

Regards,
Otto

Posted: Mon Mar 31, 2008 10:14 pm
by Antonio Linares
Otto,

> The program does not fail – the exception is only a kind of a msgbox I from WINDOWS

Is it the screenshot that you have posted previously in this thread ?

If it is not an error, maybe we can bypass it using a technique that I described here recently (C language).

Posted: Mon Mar 31, 2008 11:54 pm
by James Bott
Antonio,

Here is a VB example that checks to see if the drive exists and if it is ready. Maybe this will help.

http://www.freevbcode.com/ShowCode.Asp?ID=70

James

Posted: Tue Apr 01, 2008 8:19 am
by Antonio Linares
James,

Thanks! Here it is adapted to FWH:

Code: Select all

#include "FiveWin.ch"

function Main() 

   if GetDriveType( "C:\" ) != 1 // the drive exists
      if GetDiskFreeSpace( "C:\" ) // the drive is ready
         MsgInfo( "the drive is ready" )
      endif   
   else
      MsgAlert( "drive not exist" )   
   endif   

return nil 

//------------------------------------------------ 

#pragma BEGINDUMP

#include <windows.h>
#include <hbapi.h>

HB_FUNC( GETDISKFREESPACE ) // cRootPathName, @nSectorsByCluster, @nBytesPerSector,
                            // @nNumberOfFreeClusters, @nTotalNumberOfClusters --> lResult
{ 
   LPSTR lpRootPathName = hb_parc( 1 ); 
   DWORD SectorsPerCluster; 
   DWORD BytesPerSector; 
   DWORD NumberOfFreeClusters; 
   DWORD TotalNumberOfClusters; 

   hb_retl( GetDiskFreeSpace( lpRootPathName, &SectorsPerCluster, 
                              &BytesPerSector, &NumberOfFreeClusters, 
                              &TotalNumberOfClusters ) ); 
   hb_stornl( SectorsPerCluster, 2 );
   hb_stornl( BytesPerSector, 3 );
   hb_stornl( NumberOfFreeClusters, 4 );
   hb_stornl( TotalNumberOfClusters, 5 );
} 

#pragma ENDDUMP

Posted: Tue Apr 01, 2008 10:13 am
by Otto
Thank you Antonio.
First screen with a dive that exists second screen with a drive letter
which is disconected from the lan.

Image

Image

Posted: Tue Apr 01, 2008 10:31 am
by Otto
Hello Antonio,
J:\ in my case is a cardreader. The test for other letters which do not
exist is working.

i found this msg on the internet:

>Here's a tip to save you hunting for the solution to fix this "Windows no disk" problem in Windows XP (UPDATE: a commenter says changing the drive letters works in Vista too), at least if it's to do with your card reader, or CD or DVD drive.

more
http://www.consumingexperience.com/2007 ... ssing.html

Posted: Tue Apr 01, 2008 1:49 pm
by Antonio Linares
Otto,

Please try it this way:

Code: Select all

#include "FiveWin.ch" 

function Main() 

   if GetDriveType( "C:\" ) != 1 // the drive exists 
      if GetDiskFreeSpace( "C:\" ) // the drive is ready 
         MsgInfo( "the drive is ready" ) 
      endif    
   else 
      MsgAlert( "drive not exist" )    
   endif    

return nil 

//------------------------------------------------ 

#pragma BEGINDUMP 

#include <windows.h> 
#include <hbapi.h> 

HB_FUNC( GETDISKFREESPACE ) // cRootPathName, @nSectorsByCluster, @nBytesPerSector, 
                            // @nNumberOfFreeClusters, @nTotalNumberOfClusters --> lResult 
{ 
   LPSTR lpRootPathName = hb_parc( 1 ); 
   DWORD SectorsPerCluster; 
   DWORD BytesPerSector; 
   DWORD NumberOfFreeClusters; 
   DWORD TotalNumberOfClusters; 

   __try 
   { 
      hb_retl( GetDiskFreeSpace( lpRootPathName, &SectorsPerCluster, 
                              &BytesPerSector, &NumberOfFreeClusters, 
                              &TotalNumberOfClusters ) ); 
   } 
   __except ( ( hb_retl( FALSE ), TRUE ) ) 
   {} 
   
   hb_stornl( SectorsPerCluster, 2 ); 
   hb_stornl( BytesPerSector, 3 ); 
   hb_stornl( NumberOfFreeClusters, 4 ); 
   hb_stornl( TotalNumberOfClusters, 5 ); 
} 

#pragma ENDDUMP 

Posted: Tue Apr 01, 2008 3:09 pm
by Otto
Antonio, the result is the same.


Regards,
Otto