Click to See Complete Forum and Search --> : Searching Directories


Styler
April 9th, 2001, 09:23 AM
Hello World

i'm having a small problem with the DIR Command.
Basically i'm running my component in Com+ using MSMQ QC. Running multiple instances of the same component each querying diffient directories for existing files, when it finds a file it does some basic processing. The the thing is the DIR command is failing at all sorts or strange times. When running sequentually it works fine. i woundered if anyone has any ideal why? or maybe my code is wronge (please see below) or if they had another way of doing this, i know about the FileSystem Object and that was going to be my next move, but is there not a lower level win32 call i could use to do this.

Please Help
Thanks Allot
Mark

The basic's of my code.


sFileName = Dir(InputDirectory, vbNormal)
Do While sFileName <> ""
ProcessFile sFileName
sFileName = Dir
Loop

AndyK
April 9th, 2001, 10:13 AM
Hi, when you are processing the file, I hope you are giving the root of the file as well, because by looking at this code you are just processing the file name without it's directory structure. For example, if InputDirectory is "c:\", it will return many files, one of them is Autoexec.bat. Now if you are processing just "Autoexec.bat" and not "c:\Autoexec.bat", you will get an error because program doesn't know where the Autoexec.bat is located so you have to use something like:

ProcessFile InputDirectory & "\" & sFileName




Hope this helps.

Programs and requests for them for FREE.
http://falstok.fly.to

Styler
April 9th, 2001, 10:23 AM
nah nah

The inputdirectory is the full path
The directories are designed for only particualr type of files...

don't suppose u have any other suggestion?

Thanks anyway
Mark

Cimperiali
April 9th, 2001, 11:14 AM
Dir will find Files and Directories. That means, if you have a subfolder with name that match a query you're doing, your code will try to treat it as a file...
You can try to solve this with a secod statement
dir(yourpathname, vbdirectory)
which will tell you if you're looking at a subdir. If so, you can skip processing to the next file.
Hope this may help

Cesare Imperiali


Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.

John G Duffy
April 9th, 2001, 01:42 PM
The problem with DIR is it is not Recursive. When You issue your first DIR such as DIR("C:\WINDOWS|*.*) a structure will be setup by which subsequent DIR's with no parameters will give you the second entry that matched the first
Your Sample is fine as far as it goes but if you issue another DIR command like your first DIR then the results of the first Dir are obliterated and can not be processed.
If, in your code, the ProcessFile subroutine were to issue a DIR then all your processing in your original loop is altered.
Your alternative is to use APIs that create memory blocks. The APIs that easly replace DIR are
"FindFirstFile" and "FindNextFile". Here is a sample that will iterate through the C:\Windows Directory, recursively calling itself using the DIR replacement APIs.
Start a new Project. Add a module. Paste this code into the module

option Explicit


public Const MAX_PATH as Long = 260
public Const FILE_ATTRIBUTE_ARCHIVE = &H20
public Const FILE_ATTRIBUTE_COMPRESSED = &H800
public Const FILE_ATTRIBUTE_DIRECTORY = &H10
public Const FILE_ATTRIBUTE_HIDDEN = &H2
public Const FILE_ATTRIBUTE_NORMAL = &H80
public Const FILE_ATTRIBUTE_READONLY = &H1
public Const FILE_ATTRIBUTE_SYSTEM = &H4
public Const FILE_ATTRIBUTE_TEMPORARY = &H100

public Type FileTime
dwLowDateTime as Long
dwHighDateTime as Long
End Type
public Type WIN32_FIND_DATA
dwFileAttributes as Long
ftCreationTime as FileTime
ftLastAccessTime as FileTime
ftLastWriteTime as FileTime
nFileSizeHigh as Long
nFileSizeLow as Long
dwReserved0 as Long
dwReserved1 as Long
cFileName as string * MAX_PATH
cAlternate as string * 14
End Type


public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (byval lpFileName as string, lpFindFileData as WIN32_FIND_DATA) as Long
public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (byval hFindFile as Long, lpFindFileData as WIN32_FIND_DATA) as Long
public Declare Function FindClose Lib "kernel32" (byval hFindFile as Long) as Long
public Declare Function SearchPath Lib "kernel32" Alias "SearchPathA" (byval lpPath as string, byval lpFileName as string, byval lpExtension as string, byval nBufferLength as Long, byval lpBuffer as string, byval lpFilePart as string) as Long




Add a ListBox and a command button to the form and paste this code into the Forms Declaratin section

option Explicit

private Sub Command1_Click()
DOIt "C:\Windows\"
End Sub
public Function StripNull(byval WhatStr as string) as string
Dim pos as Integer
pos = InStr(WhatStr, Chr$(0))


If pos > 0 then
StripNull = Left$(WhatStr, pos - 1)
else
StripNull = WhatStr
End If
End Function


public Sub DOIt(path)
Dim hFile as Long, ts as string, WFD as WIN32_FIND_DATA
Dim result, szPath
szPath = path
hFile = FindFirstFile(szPath & "\*.*", WFD)
Do
If WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY then
ts = StripNull(WFD.cFileName)
If Not (ts = "." Or ts = "..") then
'Don't look for hidden or system directo
' ries
If Not (WFD.dwFileAttributes And (FILE_ATTRIBUTE_HIDDEN Or FILE_ATTRIBUTE_SYSTEM)) then
List1.AddItem path & WFD.cFileName
DOIt path & StripNull(WFD.cFileName)
End If
End If
End If
WFD.cFileName = ""
result = FindNextFile(hFile, WFD)
Loop Until result = 0
FindClose hFile

End Sub



Run project.
About halfway through the DOIt sub, notice it calls itself. Using DIr this would destroy any previous references created your original DIR command.

John G