Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching a MKDIR failure

Status
Not open for further replies.

morecowbel

IS-IT--Management
Nov 4, 2005
4
I have a FoxPro program that is responsilbe for creating a subdirectory then moving some file into that subdirectory.

Every now and then for some unknown reason the subdirectory is not made (the subdirectory is being created on the local systems hard drive -- not across the network).

When the subdirectory is not created my program does not crash. Instead the program crashes when it trys to copy file into the folder stating "The specified file does not exist".

I can cancel the program then restart it and eveything works fine for another few days.

Does anyone know how I can capture a return value fro mteh MKDIR command to determine when a problem creating the subfolder happens so I can code around it? Also, does anyone have any ideas why FoxPro (version 9) would have problems in creating a subdirectory like this every now and then?
 
There's no way of "returning" an error indication from MKDIR (it's a command, not a function). The best you can do is to error-trap the operation, using TRY / CATCH / FINALLY. However, that won't trap all the possible MKDIR problems.

If MKDIR does not report an error, the best bet is to use DIRECTORY() immediately after MKDIR to see if the directory you just created now exists.

As to why the operation failed, there are several possibilities: lack of disk space, lack of user permissions, directory name already exists, invalid directory name, disk is write-protected .... and no doubt several others.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Mike,

My inital thought was to use the DIRECTORY() after the creation to test that the directory was indeed created. I just wanted to make sure there was not a better method.

Thanks for the quick response!

Richard
 
Probably your best approach would be to encapsulate the mkdir in a UDF that returned whether or not the folder was made:

Code:
Function myMkDir
  Parameter m.FolderToMake
  Private m.FolderToMake,m.Flag
  m.Flag = .f.
  md (m.FolderToMake)
  if Directory(m.FolderToMake)
    m.Flag = .t.
  endif
Return(m.flag)


You should probably include some error handling to avoid the creation of invalid folders from throwing the function:

Code:
Function myMkDir
  Parameter m.FolderToMake
  Private m.FolderToMake,m.Flag
  m.Flag = .f.
  on error to fileerr
  md (m.FolderToMake)
  if Directory(m.FolderToMake)
    m.Flag = .t.
  endif
Return(m.flag)

Procedure FileErr
Return


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top