How to check if a drive exists

User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

How to check if a drive exists

Post by Otto »

How to check if a drive exists.

Thanks in advance

Otto
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post by James Bott »

Try:

lisDir( cDrive +":\" )

Or, make it into a function:

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

James
User avatar
Robert Frank
Posts: 95
Joined: Fri Nov 23, 2007 4:43 am
Location: Gdynia-Poland
Contact:

Post 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?
Robert Frank
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Post 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
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Post 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
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Post 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
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post 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).
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Post 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
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Post 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
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post 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 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Post by Otto »

Antonio, the result is the same.


Regards,
Otto
Post Reply