Dir$ problem - not consistent

Am working on a Zip attachment, detach all files in Zip to folder and reading each file from folder and printing. But am getting problem with Dir$ it is not working consistently, sometime it works sometimes doesn’t… what’s wrong? any workaround for Dir$ to read files from file system…

No access issues, I have all rights on dir.

here is extract of code…

Dim fileName As String, pathName As String

pathName$ = “C:\data\notes\temp\zip*.*”

fileName$ = Dir$(pathName$, 0+2+4)

If fileName$ = “” Then

AttachmentsNotPrinted = AttachmentsNotPrinted + " " + fname

End If

Do While fileName$ <> “”

'now got the first file name process based on extension

If fileName$ <> “” Then

Print ("processing "+fileName$)

End If

'Get the next filename and process

fileName$ = Dir$()

Loop 'Loops thru each file in the directory

Thanks for ur help

Subject: RE: Dir$ problem - not consistent

I haven’t heard of any problem with Dir$ – I have to suspect that whatever you’re doing to unzip the files sometimes isn’t done yet by the time you request the directory; hence you get an incomplete list.

Subject: RE: Dir$ problem - not consistent

Thanks for ur response,

I checked directory before Dir$ statement is executed, files are available seems Dir$ cannot see at first attempt, I tried to issue same Dir$ twice, second time it will get hold of the detached files as follows…

fileName$ = Dir$(“.”)

fileName$ = Dir$(“.”)

Don’t know how, but it works.

Subject: RE: Dir$ problem - not consistent

If you shell out to another program, that starts the other program. Your code does not pause until the other program is finished executing. Doing that would require some fancy coding using OS functions – or inserting a long enough delay that you’re sure the program is done.

What do you mean you “checked directory” first? You didn’t show code for that. If you mean you checked manually, then I’m not surprised all the files were there. We’re talking about seconds or fractions of a second here – not long enough for you to notice and have to wait for.

Using Dir$ an extra time causes a short delay. If this is a timing related issue, as I suspect, then I’m not surprised that doing it twice works better, because the unzip command, which is running in its own process, will have had a little longer to finish its work. That doesn’t mean that doing it twice will work in all cases; a longer delay would be safer.

Subject: RE: Dir$ problem - not consistent

I have a similar situation where some file operations I’m doing aren’t always finished in time - what I did to work around it was to set up an error handler for that situation that sleeps for 3 seconds before trying again. It’s never had to “try again” more than once, although I limit it to 3 attempts before moving on completely.

Subject: RE: Dir$ problem - not consistent

Further update on this… if I use delay loop of 18000 (just increment numbers), still doesn’t work. If I use Message box after Dir$ it works…

Is there any alternate for Dir$ to read files from file system?

Thanks Andre

Subject: RE: Dir$ problem - not consistent

The problem is not, not, not with the Dir$ function. Any function you use will fail to detect files that do not yet exist at the time you use it.

Counting to 18000 is not a very long delay compared to the amount of time it takes to unzip a bunch of files and write them to disk.

Don’t use a delay loop. If you tie up the processor by counting, you’re using CPU power that the system needs to unzip the files. Use the Sleep statement instead.

Subject: RE: Dir$ problem - not consistent

Here is complete code… first I unzip files to local file system then, I will read those files and print. If you look at code… first Messagebox with display no files and second Messagebox will display files. I need solution to avoid messagebox, I want to avoid anykind of UI. Any Suggestions…

Sub PrintZip(fname As String)

%REM

'To process zip file

'1. Copy WZUNZIP.EXE file to C:\Data\Notes\temp

'2. Create Zip directory under C:\Data\Notes\temp

  1. create zipInzip folder under C:\Data\Notes\temp

'Correct from command prompt - 'wzunzip “Claims data.zip” c:\data\notes\temp

%END REM

'UNZIP FILES FROM ZIP FILE AND DETACH TO C:\DATA\NOTES\TEMP\ZIP

Dim zip As Integer       

Dim shellCommand As String

Dim  ToDir As String, fromdir As String

Dim temp As String		

Dim s As New NotesSession



ToDir$ = " C:\\data\notes\temp\zip"  

fromdir = "C:\\data\notes\temp\" &  fname

’ temp$ = Chr$(34) &Cstr(fromdir) & Chr$(34) 'this also works

temp$ = {"} & Cstr(fromdir) & |" | 

'added -o option for overwriting if files already extracted

shellCommand ="c:\\data\notes\temp\wzunzip.exe -o " + temp$ + ToDir$

zip% = Shell( shellCommand , 6)  	

If zip% <> 33 Then

	Print "compression failed.."

Else

'Compression is successfull - Read directory where files detached

	Dim fileName As String, pathName As String, TDir As String

	TDir$ = s.GetEnvironmentString ("Directory", True)

	TDir$ = TDir & "\temp\zip"

	Chdir TDir$         

’ fileName$ = Dir$(“.”)

	fileName$ = Dir$(TDir$ & "\*.*",0)

	Messagebox ("1.  Unzipped files in Zip folder..." & fileName$)		

’ Call delayprocess

	fileName$ = Dir$("*.*")   'recalling same function as first time not getting refreshed	

	Messagebox ("2.  Unzipped files in Zip folder..." & fileName$)

	If fileName$ = "" Then			

		AttachmentsNotPrinted = AttachmentsNotPrinted + " " + fname   'writing into log no attachments found

	End If	

	Do While fileName$ <> ""

		If fileName$ <> "" Then

			Print ("processing.......... "+fileName$)

’ Call ProcessZip("C:\data\notes\temp\zip" + fileName$,fileName$,fname) 'sending filename with path info

		End If

'Get the next filename and process

		fileName$ = Dir$()

		Loop  'Loops thru each file in the directory

	End If  'for fileName$ <> ""

'CALL CLEANUP CODE BEFORE PROCESSING NEW ZIP FILE

	Call CleanUp(fname)  'this process cleans c:\data\notes\temp\zip folder		

End Sub

Subject: Dir$ problem - not consistent

In order to make the Dir$-function work correctly, the external zip-process must be finished before Your code continues to execute - otherwise Dir$() will fail.

This script gives You the option to run an external process and have Your script wait for it to be finished:

Option Public

Option Explicit

Public Const NORMAL_PRIORITY_CLASS = &H20&

Public Const INFINITE = -1&

Public Type STARTUPINFO

cb As Long

lpReserved As String

lpDesktop As String

lpTitle As String

dwX As Long

dwY As Long

dwXSize As Long

dwYSize As Long

dwXCountChars As Long

dwYCountChars As Long

dwFillAttribute As Long

dwFlags As Long

wShowWindow As Long

cbReserved2 As Long

lpReserved2 As Long

hStdInput As Long

hStdOutput As Long

hStdError As Long

End Type

Public Type PROCESS_INFORMATION

hProcess As Long

hThread As Long

dwProcessId As Long

dwThreadID As Long

End Type

Declare Function CreateProcessA Lib “kernel32” (Byval lpAppName As Long, Byval lpCommandLine As String, Byval lpProcessAttributes As Long, Byval lpThreadAttributes As Long, Byval bInheritHandles As Long, Byval dwCreationFlags As Long, Byval lpEnvironment As Long, Byval lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long

Declare Function WaitForSingleObject Lib “kernel32” (Byval hHandle As Long, Byval dwMilliseconds As Long) As Long

Declare Function CloseHandle Lib “kernel32” (Byval hObject As Long) As Long

Public Sub RunProcess(Byval strCommand As String)

Dim proc As PROCESS_INFORMATION

Dim start As STARTUPINFO

Dim r As Long

'Initialize the STARTUPINFO structure by

'passing to start the size of the STARTUPINFO

'type. Setting the .cb member is the only

'item of the structure needed to launch the program

start.cb = Len(start)

'Start the application

Call CreateProcessA(0&, strCommand, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

'Wait for the application to finish

Call WaitForSingleObject(proc.hProcess, INFINITE)

'Close the handle to the process

Call CloseHandle(proc.hProcess)

'Close the handle to the thread created

Call CloseHandle(proc.hThread)

End Sub

’ — CODE END —

So, all You have to do now is to call the sub with RunProcess(“C:\WINNT\Notepad.exe”) or whatever external process you want to start and Your script will halt until the external process has been closed.

hth

Subject: RE: Dir$ problem - not consistent

Thanks Andre and Ken.

Myself tried with Sleep and it seems working, not yet moved into production.

Thanks for all ur help.