Microsoft VBScript compilation error: Syntax error
-
Sunday, June 24, 2012 7:17 PM' Purpuse of the script: It will check for .bkf files in folder D:\My folder(the search will include all subfolders) and will delete .bkf files older than 4 days'it will read server name from servers.txt file and ping each server if not reachable then will write to a text file. It will also write all the files it is deleting.
The code is given belo. Error is Syntex error in the below red line.
Option EXPLICIT
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject" )
Set objTextFile1 = objFSO.OpenTextFile _
("C:\questapp\service_status.txt" , ForAppending, True) 'Output File
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary" )
Set objFSO = CreateObject("Scripting.FileSystemObject" )
Set objTextFile = objFSO.OpenTextFile _
("c:\questapp\servers.txt", ForReading) 'Input File. One DC names(DNS names) shoud be on one line no blank space
i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objPing = GetObject("winmgmts:{impersonationLevel= )._impersonate}"
ExecQuery("select * from Win32_PingStatus where address = '"_
& StrComputer & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
objTextFile1.WriteLine("Server " & StrComputer & " is not reachable")
End If
Next
DIM strExtensionsToDelete,strFolder
DIM objFSO, NumberOfDays, IncludeSubFolders
' ************************************************************
' Setup
' ************************************************************
' Folder to delete files
strFolder = "\\"&strcomputer& "D$\My Folder\"
' Delete files from sub-folders?
includeSubfolders = true
' A comma separated list of file extensions
' Files with extensions provided in the list below will be deleted
strExtensionsToDelete = "bkf"
' Max File Age (in Days). Files older than this will be deleted.
NumberOfDays = 1
' ************************************************************
set objFSO = createobject("Scripting.FileSystemObject" )
DeleteFiles strFolder,strExtensionsToDelete , NumberOfDays, includeSubFolders
wscript.echo "Finished"
Sub DeleteFiles(byval strDirectory,byval strExtensionsToDelete,byval maxAge,includeSubFolders)
DIM objFolder, objSubFolder, objFile
DIM strExt
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
for each strExt in SPLIT(UCASE(strExtensionsToDelete ),",")
if RIGHT(UCASE(objFile.Path),LEN(strExt )+1) = "." & strExt then
If DateDiff("d", objFile.DateLastModified,Now) = NumberOfDays Then 'Use > for testing I am using =
wscript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified
objFile.Delete
exit for
END IF
end if
next
next
if includeSubFolders = true then ' Recursive delete
for each objSubFolder in objFolder.SubFolders
DeleteFiles objSubFolder.Path,strExtensionsToDelete ,maxAge, includeSubFolders
next
end if
end SubNext
Any help highly appreciated...Thanks in advance
- Edited by answernick Sunday, June 24, 2012 7:24 PM
All Replies
-
Sunday, June 24, 2012 7:42 PM
This block
For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objPing = GetObject("winmgmts:{impersonationLevel= )._impersonate}"
ExecQuery("select * from Win32_PingStatus where address = '"_
& StrComputer & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
objTextFile1.WriteLine("Server " & StrComputer & " is not reachable")
End If
Nextrequires a closing "next" (which is actually visible because you use good indentation). For some reason you moved it to the very end of your code.
- Edited by OberwaldMicrosoft Community Contributor Monday, June 25, 2012 5:36 AM
-
Tuesday, June 26, 2012 8:12 PM
Hi Oberwald,
Sorry for late reply...
Thanks. After following your input the error is not comming. but the code is using the last server in server.txt file as a value of "StrComputer". and it is deleting the files from one server only.
I used the next at very end because I want to execute the deletion codes for all the servers. Please let me know if I am making sence.
Any help appreciated. Thanks again.
Can I use a sub () function with for loop?
Regards
Nick
- Edited by answernick Tuesday, June 26, 2012 8:15 PM
-
Tuesday, June 26, 2012 8:14 PM
Hi Takane.ir,
Please answer iff you have some thing to say which is related to this discussion.
Please do not spread spam messages.
Thanks for understanding.
Regards
Nick
-
Tuesday, June 26, 2012 8:33 PM
Hi Takane.ir,
Please answer iff you have some thing to say which is related to this discussion.
Please do not spread spam messages.
Thanks for understanding.
Regards
Nick
Looking at teh code you posted it shows that you are likely to be using a Unicode file. YOu cannott use Unicode with VBScript. It will execute in many very stnage ways if at all.
Open the file in notepad and do a SaveAs to a new file. Be sure ti select ANSI as teh file type.
Repost you code after you have fixed it. It is implossibe to copy and paste it as it is pasted due to teh double byte characters.
You should not place fuction declarations into teh middle of loops. This, also, will cause many odd issues. Functions should be delclared in the beginning or end of the file. The outer control code block should be short and easy to see all at once.
¯\_(ツ)_/¯
- Edited by jrvMicrosoft Community Contributor Tuesday, June 26, 2012 8:59 PM
-
Tuesday, June 26, 2012 8:40 PM
Hi Oberwald,
Sorry for late reply...
Thanks. After following your input the error is not comming. but the code is using the last server in server.txt file as a value of "StrComputer". and it is deleting the files from one server only.
I used the next at very end because I want to execute the deletion codes for all the servers. Please let me know if I am making sence.
Any help appreciated. Thanks again.
Can I use a sub () function with for loop?
Regards
Nick
Your problem is caused by having only a weak structure in your code which makes it hard to gain an overview. Here is how you could have a strong structure. You see at one glance how the script goes through each and every server in the server list. Note also that you do not need a dictionary in this case.
I only rewrote the first part of the script and the function right at the end. You will need to adjust or combine the two subroutines in the middle.
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile1 = objFSO.OpenTextFile _
("C:\questapp\service_status.txt", ForAppending, True) 'Output File
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\questapp\servers.txt", ForReading) 'Input File. One DC names(DNS names) shoud be on one line no blank space
arrServers = Split(objTextFile.ReadAll, VbCrLf)
objTextFile.Close
' ***********
' The main loop
' ***********
For Each strComputer In arrServers
If CanReach(strComputer) Then DeleteFiles(strComputer)
Next' ************************************************************
' Subroutines
' ************************************************************
Sub DeleteFiles(sPC)
Dim strExtensionsToDelete,strFolder
Dim objFSO, NumberOfDays, includeSubfolders
' Folder to delete files
strFolder = "\\"&strComputer& "D$\My Folder\"
' Delete files from sub-folders?
includeSubfolders = True
' A comma separated list of file extensions
' Files with extensions provided in the list below will be deleted
strExtensionsToDelete = "bkf"
' Max File Age (in Days). Files older than this will be deleted.
NumberOfDays = 1
' ************************************************************
DeleteFiles strFolder,strExtensionsToDelete, NumberOfDays, includeSubfolders
WScript.echo "Finished"
End Sub
Sub DeleteFiles(ByVal strDirectory,ByVal strExtensionsToDelete,ByVal maxAge,includeSubFolders)
Dim objFolder, objSubFolder, objFile
Dim strExt
Set objFolder = objFSO.GetFolder(strDirectory)
For Each objFile In objFolder.Files
For Each strExt In Split(UCase(strExtensionsToDelete),",")
If Right(UCase(objFile.Path),Len(strExt)+1) = "." & strExt Then
If DateDiff("d", objFile.DateLastModified,Now) = NumberOfDays Then 'Use > for testing I am using =
WScript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified
objFile.Delete
Exit For
End If
End If
Next
Next
If includeSubfolders = True Then ' Recursive delete
For Each objSubFolder In objFolder.SubFolders
DeleteFiles objSubFolder.Path,strExtensionsToDelete,maxAge, includeSubfolders
Next
End If
End Sub
'************ ***********
'See if the machine can be reached'************ ***********
Function CanReach(strPC)
CanReach = True
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '" & strPC & "'")
For Each objStatus In objPing
If IsNull(objStatus.StatusCode) Or objStatus.StatusCode<>0 Then
objTextFile1.WriteLine("Server " & strPC & " is not reachable")
CanReach = False
Exit Function
End If
Next
End Function- Marked As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Tuesday, July 03, 2012 8:53 PM
-
Tuesday, June 26, 2012 8:57 PM
Start with a clean piece of code. I cleaned out a lot of teh extra and unneeded lines. Get tyour code working with as little extra code as possible then add in features a bit at a time. You had many subtle errors that should be gone now. If you get errors now it is problely because you have permissions or existence issues.
Set fso = CreateObject("Scripting.FileSystemObject") Set inputfile = fso.OpenTextFile("c:\questapp\servers.txt") While Not inputfile.AtEndOfStream sComputer = inputfile.ReadLine() Set wmi = GetObject("winmgmts:\\.\root\CimV2") Set pings = wmi.ExecQuery("select * from Win32_PingStatus where address = '" & sComputer & "'") For Each ping in pings If IsNull(ping.StatusCode) or ping.StatusCode <> 0 Then WScript.Echo "Server " & sComputer & " is not reachable" End If Next DeleteFiles "\\" & sComputer & "D$\My Folder\", "bkf", 1, True wscript.echo "Finished" Wend Sub DeleteFiles(strDirectory,strExtensionsToDelete,maxAge,includeSubFolders) DIM objFolder, objSubFolder, objFile DIM strExt set objFolder = fso.GetFolder(strDirectory) for each objFile in objFolder.Files for each strExt in SPLIT(UCASE(strExtensionsToDelete),",") if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then If DateDiff("d", objFile.DateLastModified,Now) = NumberOfDays Then 'Use > for testing I am using = wscript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified objFile.Delete exit for END IF end If Next Next if includeSubFolders = true then ' Recursive delete for each objSubFolder in objFolder.SubFolders DeleteFiles objSubFolder.Path,strExtensionsToDelete,maxAge, includeSubFolders Next end if End Sub
¯\_(ツ)_/¯
-
Wednesday, June 27, 2012 1:10 PM
Hi,
Did you got the chance to test your code. It is giving error in line " If CanReach(strComputer) Then DeleteFiles(strComputer)" - invalid arguments
-
Wednesday, June 27, 2012 2:12 PM
Hi,
Did you got the chance to test your code. It is giving error in line " If CanReach(strComputer) Then DeleteFiles(strComputer)" - invalid arguments
The code I posted has been tested and works correctly.
¯\_(ツ)_/¯
-
Wednesday, June 27, 2012 3:38 PM
Sorry, can't tell without seeing the current version of your code.Hi,
Did you got the chance to test your code. It is giving error in line " If CanReach(strComputer) Then DeleteFiles(strComputer)" - invalid arguments

