April 19, 2024, 03:20:03 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Minor problem with availability of a file

Started by billhsln, November 29, 2008, 08:46:27 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

billhsln

I have written a program for my wife, where she picks a subdirectory and the program starts at that subdirectory and then parses thru each file and subdirectory under that one.  It takes this information and writes it to a file, which I then have the program load into Excel.  The problem comes in when Excel reads the file.  It seems to think that the file is still busy, even after the close.  Do I need to do a wait or some other command to release the file so it can be accessed by Excel as writable?

Here is my code:

' Select default start directory and then write files and subdirectories
' to a file to be loaded into Excel and then sorted and saved (must do SAVE AS)

DEF version$:string
version$="1.0"

AUTODEFINE "off"

DECLARE IMPORT,SHBrowseForFolder(param1:uint),int
DECLARE IMPORT,SHGetPathFromIDList(param1:uint, param2:uint),int
DECLARE IMPORT,CoTaskMemFree(param1:int),int
DECLARE IMPORT,RtlZeroMemory(dat:uint,length:int),int
DECLARE IMPORT,SendMessageA(hWnd:uint, message:uint, wParam:int, lparam:uint),uint

TYPE BROWSEINFO
   UINT hOwner
   UINT pidlRoot
   POINTER pszDisplayName
   POINTER lpszTitle
   UINT ulFlags
   UINT lpfn
   UINT lParam
   UINT iImage
ENDTYPE

CONST WM_USER = 0x400
CONST BFFM_SETSELECTION = WM_USER + 102
CONST BFFM_INITIALIZED = 1
CONST BFFM_VALIDATEFAILED = 3
CONST BIF_RETURNFSANCESTORS = 8
CONST BIF_RETURNONLYFSDIRS = 1
CONST BIF_NEWDIALOGSTYLE = 0x40
CONST BIF_DONTGOBELOWDOMAIN = 2

FILE oFile
ISTRING mdir[261]
INT i, l, mdirl, mdirs, t = 2

OPENCONSOLE

IF FolderRequest(0, "Select Folder to start from", mdir, GETSTARTPATH, 0x441)
IF (OPENFILE(oFile,"C:\\!#!Files.CSV","W") = 0)
mdirl = LEN(mdir)
l = mdirl - 2
FOR i = l TO 1 STEP -1
IF mdir[i] = "\\"
mdirs = i + 2
i = 0
ENDIF
NEXT i
WRITE oFile, MID$(mdir,mdirs,mdirl - mdirs) + ",.."
PRINT "File Open"
parsedir(mdir)
PRINT "After Parse"
CLOSEFILE oFile
SYSTEM "excel.exe", "C:\\!#!Files.CSV"
ELSE
PRINT "Not able to open output file - C:\\!#!Files.TXT"
ENDIF
ELSE
PRINT "No Folder chosen"
ENDIF

DO
UNTIL INKEY$ <> ""

CLOSECONSOLE
END

'________________________________________________________________________________________
SUB FolderRequest(hWnd:uint, title:pointer, dir:pointer, initial:pointer, flags:uint),int
'________________________________________________________________________________________
INT r : r = FALSE
ISTRING buffer[261]
UINT item_list
BROWSEINFO bi
RtlZeroMemory(&bi, LEN(bi))
bi.hOwner = hWnd
bi.lpszTitle = title
bi.ulFlags = flags
IF flags = 0 THEN bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_DONTGOBELOWDOMAIN
bi.lpfn = &BrowseFolderCallback
bi.lParam = initial
' Display the browser.
item_list = SHBrowseForFolder(&bi)
IF item_list
IF SHGetPathFromIDList(item_list, &buffer)
#<STRING>dir = buffer + "\\"
r = TRUE
ENDIF
CoTaskMemFree(item_list)
ENDIF
RETURN r
ENDSUB

'___________________________________________________________________________
SUB BrowseFolderCallback(hWnd:uint, uMsg:uint, lParam:uint, lpData:uint),int
'___________________________________________________________________________
SELECT uMsg
CASE BFFM_INITIALIZED
' Set start directory
SendMessageA( hWnd, BFFM_SETSELECTION, TRUE, lpData )
CASE BFFM_VALIDATEFAILED
RETURN 1
ENDSELECT
RETURN 0
ENDSUB

'________________________
SUB parsedir(path:STRING)
'________________________
DEF dir, attrib:INT
DEF filename[261]:ISTRING
DEF fullname[261]:ISTRING
dir = FINDOPEN(path + "\\*.*")
IF (dir)
DO
filename = FINDNEXT(dir,attrib)
IF LEN(filename)
IF attrib & @FILE_DIRECTORY
IF (filename <> ".") AND (filename <> "..")
fullname = path + filename + "\\"
WRITE oFile, "." + MID$(fullname,mdirl + 1) + "," + MID$(fullname,mdirl + 1)
t += 2
parsedir(fullname)
t = t - 2
ENDIF
ELSE
        fullname = path + filename
WRITE oFile, SPACE$(t) + filename + "," + MID$(path,mdirl + 1) + "."
ENDIF
ENDIF
UNTIL filename = ""
FINDCLOSE dir
ENDIF
RETURN
ENDSUB
When all else fails, get a bigger hammer.

Ionic Wind Support Team

Try using the FlushFileBuffers API function after writing to it:

Declare Import, FlushFileBuffers(uint handle),int

parsedir(mdir)
PRINT "After Parse"
FlushFileBuffers(oFile)
CLOSEFILE oFile
Ionic Wind Support Team