Jump to content

Recommended Posts

Posted

Sorry guys, this is driving me crazy.

I've written (borrowed/copied/made up) a script that will monitor a directory for new files being created, and it works fine. Now I would like to have an argument added which determines that directory at executing of cscript (so I can type cscript.exe monitor.vbs c:\scripts for instance). I can't get it to work.

Here's what I've tried:

strComputer = "."

fold = wscript.arguments.item(0)

foldr = replace(fold,"\","\\\\")

wscript.echo foldr

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & _

strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _

("SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE " _

& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _

& "TargetInstance.GroupComponent= " _

& "'Win32_Directory.Name="& foldr & "'")

Do While TRUE

Set objEventObject = colMonitoredEvents.NextEvent()

Select Case objEventObject.Path_.Class

Case "__InstanceCreationEvent"

etc etc...

no matter how hard I try to massage the & "'Win32_Directory.Name="& foldr & "'") line, I always get either an unparsable or invalid query. It works fine with c:\\\\scripts (etc). Can anyone please help me out? Much appreciated!

Adrian


Posted (edited)

your folder name needs to be in double quotes in the query string.

this:

..."'Win32_Directory.Name="& foldr & "'"

should really end up looking like:

...'Win32_Directory.Name="C:\\\\scripts"'

you can either add the appropriate number of quotes or add CHR(34) to the string.

..."'Win32_Directory.Name="" & foldr & ""'"

or

..."'Win32_Directory.Name=" & chr(34) & foldr & chr(34) &"'"

@ gunsmokingman - his loop will work, rereview your Do..loop

do [{while | until} condition]

[statements]

[exit do]

[statements]

loop

Edited by IcemanND
Posted

You are wrong as the script he posted produced this error, this is with the correction you made.

Why do you think I posted those links, I do have a pretty good understanding of VBS and the code

posted does not work, because the loop is wrong.

DoesNotWork.PNG

Posted (edited)

@gunsmokingman

For your information the code is incomplete. There is a etc. etc... at the end of acovich code which mean you can't do a test from what you can see only.

It's funny that you made a test to be sure if you have the VBS knowledge you pretend :P

--------------------------------------

Just a little reminder

"While... Wend" is used when you don't need to a conditional exit point and with "Do... Loop" you can use "Exit Do" as an exit point.

In the code of acovich the hidden part of the loop is something like that...

Do While TRUE

Set objEventObject = colMonitoredEvents.NextEvent()

Select Case objEventObject.Path_.Class

Case "__InstanceCreationEvent"

If Something Is True Then Exit Do

Loop

A "Do While True" loop implies an "Exit Do", otherwise it would be an infinite loop.

The problem of acovich is something about WMI which I have never used so my help worth nothing. Sorry.

Edited by jdoe
Posted

@gunsmokingman - sorry I was assuming the end of the loop was in the etc, etc.

since the error was before the loop I didn't test anything from the loop on.

Posted
@gunsmokingman

For your information the code is incomplete. There is a etc. etc... at the end of acovich code which mean you can't do a test from what you can see only.

It's funny that you made a test to be sure if you have the VBS knowledge you pretend :P

--------------------------------------

Just a little reminder

"While... Wend" is used when you don't need to a conditional exit point and with "Do... Loop" you can use "Exit Do" as an exit point.

In the code of acovich the hidden part of the loop is something like that...

Do While TRUE

Set objEventObject = colMonitoredEvents.NextEvent()

Select Case objEventObject.Path_.Class

Case "__InstanceCreationEvent"

If Something Is True Then Exit Do

Loop

A "Do While True" loop implies an "Exit Do", otherwise it would be an infinite loop.

The problem of acovich is something about WMI which I have never used so my help worth nothing. Sorry.

I do not pretend that is how he posted the code and it does not work. Me if I posted a code that was not working it would be complete as it sits the loop will not work.

Here a example of a loop at work and it code complete.

Dim fVar1, Ln1, SFolder
Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Act : Set Act = CreateObject("Wscript.Shell")
Dim CD, CD1, CD2, CD3, CD4: CD = Act.CurrentDirectory
Dim Dtop : Dtop = Act.SpecialFolders("Desktop")
SFolder = CD
CD1 = Replace(CD,"\","_")
CD2 = Split(CD1,"_")
CD3 = CD2(0) & "_" & CD2(1)
CD4 = Replace(CD3,":","")
Dim MB,GB : MB = 1024 * 1024 : GB = MB * 1024
Dim Ts : Set Ts = Fso.CreateTextFile(Dtop & "\" & CD4 &"_Directory.txt")
Ln1 = Space(3) & Chr(171) & " ----------------------------------- " & Chr(187)
Ts.writeline Space(5) & "Date : " & Now & vbCrLf &_
Space(5) & "User : " & Act.ExpandEnvironmentStrings("%UserName%") & vbCrLf & Ln1
Set objFolder = Fso.GetFolder(SFolder)
Ts.writeline Space(5) & "Start Folder : " & objFolder.Path & vbCrLf & Ln1
Set colFiles = objFolder.Files
For Each objFile in colFiles
Ts.writeline Space(5) & "File Path : " & objFile.Path
Ts.writeline Space(5) & "File Name : " & objFile.Name
ConvertFileSize
Ts.writeline Space(5) & fVar1 & vbCrLf & Ln1
Next

ShowSubfolders Fso.GetFolder(SFolder)

Function ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Ts.writeline Space(5) & "Sub Folder : " & Subfolder.Path
Set objFolder = Fso.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Ts.writeline Space(5) & "File Path : " & objFile.Path
Ts.writeline Space(5) & "File Name : " & objFile.Name
ConvertFileSize
Ts.writeline Space(5) & fVar1 & vbCrLf & Ln1
Next
ShowSubFolders Subfolder
Next
End Function

''''
Function ConvertFileSize
If objFile.Size < 999999 Then
fVar1 = Left(objFile.Size,3)
fVar1 = "File Size : " & fVar1 & " KB"
End If
If objFile.Size > 1000000 Then
fVar1 = objFile.Size /MB : fVar1 = Left(fVar1,4)
fVar1 = "File Size : " & fVar1 & " MB"
End If
If objFile.Size > 1000000000 Then
fVar1 = objFile.Size /GB : fVar1 = Left(fVar1,4)
fVar1 = "File Size : " & fVar1 & " GB"
End If
Exit Function
End Function
''''
Ts.close
Act.Run(Chr(34) & Dtop & "\" & CD4 &"_Directory.txt" & Chr(34)), 1, True

If Fso.FileExists(CD & "\ListDirectory_1a.vbs") Then
Fso.DeleteFile(CD & "\ListDirectory_1a.vbs")
End If

Dim ZZ1
ZZ1 = Act.Popup("Did you want to keep this file" & vbCrLf &_
"Yes To keep the file" & vbCrLf & "No to delete the file" &_
vbCrLf & "After a 5 minute period this " & vbCrLf &_
"will close and delete the file", 301, "Keep Or Delete", 4 + 32)
If ZZ1 = 6 Then Fso.DeleteFile(Dtop & "\" & CD4 &"_Directory.txt") End If
If ZZ1 = -1 Then Fso.DeleteFile(Dtop & "\" & CD4 &"_Directory.txt") End If

Posted
Me if I posted a code that was not working it would be complete as it sits the loop will not work.

For testing purpose it's surely better to have the complete code. ;)

I know you've wrote quantities of VBS on this board, for the benefit of all members and having see some of them I have few suggestions, basically for readability.

Add some "air" in your scripts. Empty lines for separating code sections.

Constant indent by 2 or 4 (not mixed).

Make loops clear from the other lines.

Have naming convention with data type identification variable

In other words, have your coding style but always use the same everywhere.

The reason I'm so strict about coding style is for code maintenance. Few times I've worked where I had to maintain code that was so poorly format that it was twice as hard to understand just for this reason.

This is for MASM Programming Style but the main concepts are valid for any language

Please don't take my comments too personal. You're a valuable member of this board.

@acovich, I'm sorry for this off-topic post, I know it don't help you much.

Posted

I agree my writing style does lack on a consitance format. :thumbup

In other words, have your coding style but always use the same everywhere.

The reason I'm so strict about coding style is for code maintenance. Few times I've worked where I had to maintain code that was so poorly format that it was twice as hard to understand just for this reason.

To quote you, as I seen that you have help alot also.
Please don't take my comments too personal. You're a valuable member of this board.
acovich, here a read this it may help. I have not used Command-Line Arguments in my scripts so that I cannot help on that.

Working with Command-Line Arguments

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...