Hello,

I'm having a problem updating a line in a shtm file using the DirPlus() Utility. Here is basically what I want to do:

Use DirPlus() to get each filename in a directory structure
For each element in the array
if it's a file with a .shtm extension
rename in to filename.shtm.tmp
Create a new file filename.shtm
Write each line from the temp file into the new file, filename.shtm
If the current line = $PrinterLine
change the line to something else
EndIf

Delete filename.shtm.tmp
Next

The problem I'm having is that after I use the move command to rename $element, the next time I reference $element (ie in the If not exist($Element) statement), I get an IDispatch pointers not allowed in expression error. I've tried assigning $Element to a different variable, and renaming that instead, but i'm still having the same problem. Does anyone have any ideas?

Here is my code:

Code:


break on
Dim $Directory $Directory = "c:\Test"
Dim $fso $fso = CreateObject("Scripting.FileSystemObject")
Dim $TmpFileHandle
Dim $NewFileHandle
Dim $Dir
Dim $FoundFlag
Dim $PrntLnPath
Dim $PrinterFriendlyLine $PrinterFriendlyLine='Dim $ReplaceStart $ReplaceStart=''
Dim $ReplaceEnd $ReplaceEnd=''
Dim $TmpFileName
Dim $ShtmFileName


$Dir = dirplus($Directory, "/s /m .shtm")
For each $Element in $Dir
If GetFileAttr($element) & 16
?$element + " is a folder"
Else
If Right($element,5) = ".shtm"
$ShtmFileName = $Element
;Rename it to filename.shtm.tmp if it doesn't exist
$TmpFileName = $Element + ".tmp"
If not exist($TmpFileName)
Move $shtmFileName $TmpFileName /h

;Open the temp file for reading
$TmpFileHandle = FreefileHandle()
If @error <> 0
?"Cannot get tmp file handle" + @error
EndIf
Open($TmpFileHandle, $TmpFileName,2)

;Create a new file filename.shtm and open it for writing if it doesn't exist
If not exist($Element)
$NewFileHandle = FreefileHandle()
If @error <> 0
?"Cannot get tmp file handle" + @error
EndIf
Open($TmpFileHandle, $Element,5)
$FoundFlag=0
$i=0
;Get the location of the file from /busservices onwards
$PrintLnPath=Right($Element,Len($Element)-Instr($Element, "/busservices/"))

$Line = Readline($TmpFileHandle)
? "error value: " + @error
While @error = 0
If $Line = $PrinterFriendlyLine
$FoundFlag=1
;Update the line to include the filename
$Line = $ReplaceStart + $PrintLnPath + $ReplaceEnd
$i=$i+1
EndIf

If $i>1
;If more than one line was updated in the file log an error
EndIf

If $FoundFlag=0
;If the line wasn't found at all, log an error
EndIf

Writeline($NewfileHandle, $line + @crlf)
$Line=Readline($TmpFileHandle)
Loop
Close($TmpFileHandle)
Close($NewFileHandle)
If exist($element + ".tmp")
Del $Element + ".tmp" /h
EndIf
Else
;Log error
EndIf
Else
;Log error
EndIf
EndIf
EndIf
Next