#155476 - 2006-01-17 05:08 AM
Problems with DirPlus and IDispatch Pointer errors
|
daniel1982
Getting the hang of it
Registered: 2005-03-23
Posts: 77
Loc: Sydney, Australia
|
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
|
|
Top
|
|
|
|
#155477 - 2006-01-17 05:53 AM
Re: Problems with DirPlus and IDispatch Pointer errors
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
I am guessing that this is only part of the whole code...
I am going to guess that the move command is having a problem with the fact that DirPlus returns an array of FSO objects not strings.
Code:
$SHTMFiles = dirplus($path1,'/s /a-d /f shtm')
;print the file name string for each $file in $SHTMFiles ? $file.name next
;move the files... for each $file in $SHTMFiles ? $file.move('c:\new folder\') next
|
|
Top
|
|
|
|
#155478 - 2006-01-17 06:19 AM
Re: Problems with DirPlus and IDispatch Pointer errors
|
daniel1982
Getting the hang of it
Registered: 2005-03-23
Posts: 77
Loc: Sydney, Australia
|
Hi Bryce,
This is all the code apart from your DirPlus UDF which I left out for simplicity.
I actually want to rename the files, not move them. I couldnt find a method in the FSO to do this? is there one?
|
|
Top
|
|
|
|
#155480 - 2006-01-17 11:31 PM
Re: Problems with DirPlus and IDispatch Pointer errors
|
daniel1982
Getting the hang of it
Registered: 2005-03-23
Posts: 77
Loc: Sydney, Australia
|
Seems to have some problems....I'm getting an 'expected expression' error on the line directly following the move file line. Here is a snippet of my code:
Code:
$Dir = dirplus($Directory, "/s /m .shtm") For each $Element in $Dir If GetFileAttr($element) & 16 ;do nothing - the element is a folder Else If Right($element.name,5) = ".shtm" $ShtmFileName = $Element.name ;Rename it to filename.shtm.tmp if it doesn't exist $TmpFileName = $Element.name + ".tmp" If not exist($TmpFileName) $Element.MoveFile($shtmFileName, $TmpFileName) ?"Renaming error: " + @error
|
|
Top
|
|
|
|
#155481 - 2006-01-17 11:56 PM
Re: Problems with DirPlus and IDispatch Pointer errors
|
daniel1982
Getting the hang of it
Registered: 2005-03-23
Posts: 77
Loc: Sydney, Australia
|
Actually by using the >Move< method instead of the MoveFile one, it gets through the script but the method returns an error -21473525622...What am I doing wrong??
|
|
Top
|
|
|
|
#155482 - 2006-01-18 12:32 AM
Re: Problems with DirPlus and IDispatch Pointer errors
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
try this.
Code:
$directory = "c:\temp" $Dir = dirplus($Directory, "/s /a-d /f shtm") For each $Element in $Dir ;Rename it to filename.shtm.tmp if it doesn't exist $TmpFileName = $Element.path + ".tmp" If not exist($TmpFileName) $Element.Move($TmpFileName) ? "Renaming error: " + @error + @serror endif next
|
|
Top
|
|
|
|
#155483 - 2006-01-18 04:03 AM
Re: Problems with DirPlus and IDispatch Pointer errors
|
daniel1982
Getting the hang of it
Registered: 2005-03-23
Posts: 77
Loc: Sydney, Australia
|
That's got it, thanks Bryce!
Cheers, Daniel
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 823 anonymous users online.
|
|
|