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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is Directory Folder Empty? 2

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
OK, I've looked at the manuals and could use a little help.<br><br>I think this one is simple.<br><br>How do I check and see if a folder located at &quot;c:/requests&quot; is empty?<br><br>If the folder is not empty, how do I open a file in the folder if I don't know the name of any of the files that could be located in the folder?<br><br>If I have an error while opening the file, what is a good way to handle error processing while doing the file open/close operation.<br><br>&nbsp;I've looked in my &quot;using visual basic&quot; by QUE and another book but can't find a good section on doing this basic file open/read/close command processing with proper error controls.<br><br>thanks,<br><br>Jerry
 
'The easyiest way of checking if a folder is empty and then opening files from that folder waith error checking is to use the Filelistbox<br><br>'Place the following Controls on your form<br><br>'2 * Command Buttons (command1 & command2)<br>'1 * FilelistBox (File1)<br>'2 * TextBox (Text1 & text2)<br><br><br><br>'Place the following code on your form<br><br>Option Explicit<br><br>Dim strPath As String<br><br>Private Function CheckEmpty() As Boolean<br>'Checks the listcount property of the filelistbox -1 means empty<br>If File1.ListCount &lt;&gt; -1 Then<br>&nbsp;&nbsp;&nbsp;CheckEmpty = False 'Files exist<br>Else<br>&nbsp;&nbsp;&nbsp;CheckEmpty = True<br>End If<br><br>End Function<br><br>Private Sub Command1_Click()<br>'Populates the filelist box with the files from the directory entered<br><br>On Error GoTo ErrHandle<br><br>'Enter the required path to search in the text box<br>strPath = Text1.Text<br><br>File1 = strPath<br><br>'Check if the selected folder is empty<br>If CheckEmpty = True Then<br>&nbsp;&nbsp;&nbsp;MsgBox &quot;This directory is empty&quot;<br>End If<br><br><br>Exit Sub<br><br>ErrHandle:<br><br>If Err.Number = 76 Then 'Path not found<br><br>MsgBox &quot;The Path You Entered Does Not Exist&quot; & vbCrLf & &quot;Please Enter A Valid Path&quot;, vbOKOnly + vbInformation, &quot;Find Files&quot;<br><br>End If<br><br>End Sub<br><br><br><br>Private Sub File1_DblClick()<br>'Place code to do operation on file here<br>'The path for the file = strpath and the selection from the list<br>'example: OPening the file for a read/write operation<br>'You must reference the Microsoft Scripting Runtime from your references section<br><br>Dim ofs As FileSystemObject<br>Dim ots As TextStream<br><br>Set ofs = New FileSystemObject<br><br>Set ots = ofs.OpenTextFile(strPath & File1.List(File1.ListIndex), ForAppending, False)<br><br>'This will open the file selected and insert text into ot<br>With ots<br><br>&nbsp;&nbsp;&nbsp;&nbsp;.Write &quot;Insert Text Here&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;.Close<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>End With<br><br><br>End Sub<br><br>'The only way that I know of to handle the specific errors that this may throw is to find out the error numbers and trap them from that<br><br><br>Hope this helps
 
Vince,<br><br>Thanks!.&nbsp;&nbsp;I'll try it a bit later in the day and let you know.&nbsp;&nbsp;It looks like it will work.<br><br>Again,&nbsp;&nbsp;Thank you.
 
Vince,

I'm close,,,very close. But I'm having a slight problem getting the code to work. I've reviewed your code, looked up a few things that I didn't know, and have worked the problem. Something I should have included was that I need the code to work in the background.

Here is the code that I'm using:

Dim ofs As FileSystemObject
Dim ots As TextStream

Set ofs = New FileSystemObject
Set ots = ofs.OpenTextFile(strFolderPath & File1.List(File1.ListIndex), ForReading, False)

Dim strInputLine As String
TextStream.Read (80)

I have checked off &quot;Microsoft Scripting Runtime&quot; from the reference section of the project. strFolderPath is a valid path to a folder.

The error that I'm getting is &quot;Run-Time&quot; error '70' which is &quot;permission denied&quot;.

When I take a look at the values it appears that the string variable 'strFolderPath' point correctly to the folder that has been determined to be non-empty; however the value of &quot;File1.List(File1.ListIndex)&quot; appears to be -1. I suspect it should point to the correct name of the file in the folder.

Help?

jerry
 
Use the filesystem object (scripting runtime), and check the collection size. I've Pasted this code from MSDN. This would get you the list of files. (If the collection f.count in this example is zero, then your directory is empty):
------------------

Function ShowFolderList(folderspec)
Dim fso, f, f1, fc, s
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & &quot;<BR>&quot;
Next
ShowFolderList = s
End Function

 
In case for some reason you are not using filesystem objects the older way of checking is as follows. This is a hardcoded example

Dim strDir as string
dim strFile as string
Dim MyPath as string

MyPath = &quot;C:\TestDir\&quot;
'Verify the directory exists
strDir = Dir(MyPath,vbDirectory)

If strDir <> &quot;&quot; then
strFile = dir(MyPath & &quot;*.*&quot;)
If strFile <> &quot;&quot; then
'a file is there so do something
Elseif strFile = &quot;&quot; then
'The folder is empty
Endif
Endif



 
To expand on the last post, to go through all files in the folder, use the following:

Dim strDir as string
dim strFile as string
Dim MyPath as string

MyPath = &quot;C:\TestDir\&quot;
'Verify the directory exists
strDir = Dir(MyPath,vbDirectory)

If strDir <> &quot;&quot; then
strFile = dir(MyPath & &quot;*.*&quot;)
Do Until strFile = &quot;&quot;
' do something to the file (name held in strFile)
strfile = dir ' moves to the next file
Loop
Endif

Simon [sig][/sig]
 
Jerry,

File1.List(File1.ListIndex) returns List property of currently selected item in list box. If none is selected ListIndex is still -1.

zoran [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top