How to check if a drive exists

How to check if a drive exists

Postby Otto » Mon Mar 31, 2008 5:51 pm

How to check if a drive exists.

Thanks in advance

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

Postby James Bott » Mon Mar 31, 2008 6:07 pm

Try:

lisDir( cDrive +":\" )

Or, make it into a function:

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

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

Postby Robert Frank » Mon Mar 31, 2008 6:33 pm

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
Robert Frank
 
Posts: 95
Joined: Fri Nov 23, 2007 4:43 am
Location: Gdynia-Poland

Postby James Bott » Mon Mar 31, 2008 6:47 pm

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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Otto » Mon Mar 31, 2008 7:39 pm

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
Otto
 
Posts: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Postby Antonio Linares » Mon Mar 31, 2008 8:27 pm

Otto,

Please modify James function this way to check where the GPF is coming from:
Code: Select all  Expand view
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
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Otto » Mon Mar 31, 2008 8:45 pm

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: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Postby Otto » Mon Mar 31, 2008 8:51 pm

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
Otto
 
Posts: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Postby Antonio Linares » Mon Mar 31, 2008 10:14 pm

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

Postby James Bott » Mon Mar 31, 2008 11:54 pm

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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Antonio Linares » Tue Apr 01, 2008 8:19 am

James,

Thanks! Here it is adapted to FWH:
Code: Select all  Expand view
#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
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Otto » Tue Apr 01, 2008 10:13 am

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: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Postby Otto » Tue Apr 01, 2008 10:31 am

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
Otto
 
Posts: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Postby Antonio Linares » Tue Apr 01, 2008 1:49 pm

Otto,

Please try it this way:
Code: Select all  Expand view
#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
Antonio Linares
Site Admin
 
Posts: 41314
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Otto » Tue Apr 01, 2008 3:09 pm

Antonio, the result is the same.


Regards,
Otto
User avatar
Otto
 
Posts: 6005
Joined: Fri Oct 07, 2005 7:07 pm

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 58 guests