Using the API to copy files...

I have a small app that copies files using the Win32 API and a timer control.

When the timer event is fired the the app transfers files from location A to location B - however if the folder in location A is empty then the app returns an API error in a MsgBox which just sits there waiting for acknowledgement (as was programmed)

I was wondering if it is possible to test and see if the folder is empty before calling the routine that transfers the files...

Any help would be appreciated
derek_noonanAsked:
Who is Participating?
 
VbmasterConnect With a Mentor Commented:
Here ya go, a special-designed funtion to see if a directory contains at least one file/dir..

Function IsDirEmpty(ByVal Path As String) As Boolean

  Dim MyName  As String
 
  IsDirEmpty = True
  Path = IIf(Right$(Path, 1) = "\", Path, Path & "\")
  MyName = Dir$(Path & "*.*", vbDirectory + vbArchive + vbHidden + vbReadOnly + vbSystem)
  Do While (Len(MyName) > 0)
    If (MyName <> ".") And (MyName <> "..") Then
      IsDirEmpty = False
      Exit Function
    End If
    MyName = Dir$
  Loop
 
End Function
0
 
crazymanCommented:
if len(dir(YourFolder))<1 then
msgbox "empty"
else
"files found"
end if
0
 
VbmasterCommented:
crazyman's code will only check if the folder exist, not if it contains any files/dirs.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
crazymanCommented:
VbMaster if you did my code stating
len(dir(yourDirector,vbDirectory))
then yes it checks if it exists but without the vbdirectory it returns the first file in the dir if there is one or "" if there isnt
0
 
crazymanCommented:
Opps your right, getting confused,.
0
 
inprasCommented:
What API UR Using to copy files? this may helps us more to handle that API functionality itself rather than using explicit comparison..
inpras
0
 
derek_noonanAuthor Commented:
To VBMaster:

Does the Path variable passed to the function need a trailing '\' ?

Respond and the points are yours - this looks damn good!
0
 
derek_noonanAuthor Commented:
To VBMaster:

Does the Path variable passed to the function need a trailing '\' ?

Respond and the points are yours - this looks damn good!
0
 
VbmasterCommented:
If the path does not contain a '\' as the last character the line...

  Path = IIf(Right$(Path, 1) = "\", Path, Path & "\")

....will add a '\' to the path string. If you are absolutely sure that you will always send a path with a '\' as the last character you can safely remove that line.

To make the answer short: No.
0
All Courses

From novice to tech pro — start learning today.