change kix script to vbs script

Hello there
I have a small script written in kix syntax. Is it possible to write it in vbs scritpt instead?

Here is the script:

IF INGROUP("administrator")
      $Result = CompareFileTimes("\\server\folder\test.ini", "d:\winnt\test.ini")
            If $Result = 1 or $Result = -3
            copy \\server\administrator\test.ini d:\winnt
            Endif
ENDIF

I hope someone can help me:)
aslandkAsked:
Who is Participating?
 
_agj_Connect With a Mentor Commented:
if its through i guess u cud close the qn ;)
0
 
_agj_Commented:
use this to find if the current user is a  member of grp administrator:

http://hacks.oreilly.com/pub/h/1130
0
 
aslandkAuthor Commented:
looks like something i can use:):)

something like this:


If MemberOf(ObjGroupDict, "domain admins") Then


but what about the rest of the script...dont know anything about kix(:

agj << Can you help??
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
_agj_Commented:
The copy file code:

Dim FSO
Dim mFile1
Dim mFile2

Set fso = CreateObject("Scripting.FileSystemObject")
Set mFile1 = fso.GetFile("\\server\folder\test.ini")

If fso.FileExists("d:\winnt\test.ini") then
Set mFile2 = fso.GetFile("d:\winnt\test.ini")
If mFile1.DateLastModified > mFile2.DateLastModified Then
         fso.CopyFile "\\server\administrator\test.ini", "d:\winnt\test.ini", True
end if
else
        fso.CopyFile "\\server\administrator\test.ini", "d:\winnt\test.ini", True
end if
0
 
_agj_Commented:
Dim WSHNetwork
Dim strUserName ' Current user
Dim strUserDomain ' Current User's domain name
Dim ObjGroupDict ' Dictionary of groups to which the user belongs

Set WSHNetwork = WScript.CreateObject("WScript.Network")
strUserName = WSHNetwork.UserName
strUserDomain = WSHNetwork.UserDomain

' Read the user's account "Member Of" tab info across the network
' once into a dictionary object.
Set ObjGroupDict = CreateMemberOfObject(strUserDomain, strUserName)
If MemberOf(ObjGroupDict, "Administrators") Then  'use the relevant group name
'the copy file code here...
End If
0
 
aslandkAuthor Commented:
thanks.....:):)

Just to check if i have understand you correct:

$Result = CompareFileTimes("\\server\folder\test.ini", "d:\winnt\test.ini")

equal to

If mFile1.DateLastModified > mFile2.DateLastModified Then


The DateLastModified function only checks the datetime?
         


so the full convertion to vbs script would be:



If MemberOf(ObjGroupDict, "Domain Admins") Then

Dim FSO
Dim mFile1
Dim mFile2

Set fso = CreateObject("Scripting.FileSystemObject")
Set mFile1 = fso.GetFile("\\server\folder\test.ini")

If fso.FileExists("d:\winnt\test.ini") then
Set mFile2 = fso.GetFile("d:\winnt\test.ini")
If mFile1.DateLastModified > mFile2.DateLastModified Then
         fso.CopyFile "\\server\administrator\test.ini", "d:\winnt\test.ini", True
end if
else
        fso.CopyFile "\\server\administrator\test.ini", "d:\winnt\test.ini", True
end if


One last question:

the condition:

If $Result = 1 or $Result = -3

has and "or" parameter but in the script you wrote you only used > "If mFile1.DateLastModified > mFile2.DateLastModified Then"

Is that the same?


I know I have asked you a lot of question but you seems like you know what you are talking about:):)


You have been a GREAT help so far:)
0
 
_agj_Commented:
-3  is the return code for 2nd file not existing and 1 for first file newer...

-3 is taken care by the FileExists things

1 by checking DateModified....

simply using isgroup wont work...use the code i jus sent
0
 
_agj_Commented:
Dim WSHNetwork
Dim strUserName ' Current user
Dim strUserDomain ' Current User's domain name
Dim ObjGroupDict ' Dictionary of groups to which the user belongs

Set WSHNetwork = WScript.CreateObject("WScript.Network")
strUserName = WSHNetwork.UserName
strUserDomain = WSHNetwork.UserDomain

' Read the user's account "Member Of" tab info across the network
' once into a dictionary object.
Set ObjGroupDict = CreateMemberOfObject(strUserDomain, strUserName)
If MemberOf(ObjGroupDict, "Administrators") Then  'use the relevant group name

'the copy file code here...
  Dim FSO
  Dim mFile1
  Dim mFile2

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set mFile1 = fso.GetFile("\\server\folder\test.ini")

  If fso.FileExists("d:\winnt\test.ini") then
  Set mFile2 = fso.GetFile("d:\winnt\test.ini")
  If mFile1.DateLastModified > mFile2.DateLastModified Then
         fso.CopyFile "\\server\administrator\test.ini", "d:\winnt\test.ini", True
  end if
  else
        fso.CopyFile "\\server\administrator\test.ini", "d:\winnt\test.ini", True
  end if
'Copy file code ends

End If
0
 
_agj_Commented:
the above post is jus the consolidated code incase u found the prev. unclear...

i guess u shud put the group as "administrator" and not Administrators as I put, since ur kix script seems to be so...
0
 
aslandkAuthor Commented:
testing your "gold script":):)......will respond soon.....but thanks so far
0
 
aslandkAuthor Commented:
it seems to ork fine...but i have a question about the if/else statement:

If mFile1.DateLastModified > mFile2.DateLastModified Then
         fso.CopyFile "\\server\administrator\test.ini", "d:\winnt\test.ini", True
  end if
  else
        fso.CopyFile "\\server\administrator\test.ini", "d:\winnt\test.ini", True
  end if


looks like the script do the same no matter if the "if" or "else" condition is true....am i wrong?
0
 
_agj_Commented:
u have missed the earlier:
If fso.FileExists("d:\winnt\test.ini") then

which is the corresponding If for the Else statement
0
 
_agj_Commented:
the logic is:

if "file 2 exists" then

  if file 1 is newer then
     copy file
  end if

else
  copy anyways
end if
0
 
aslandkAuthor Commented:
ahhh i can see what you mean..:):)

I have about 10 of the same if statements:

If MemberOf(ObjGroupDict, "Administrators") Then  'use the relevant group name

just with different groupnames of course. Is there a smarter way to optimize the script or do i hvae to make 10 if statement almost the same?

0
 
aslandkAuthor Commented:
for eksample:

the Domain Admins looks like this:

If MemberOf(ObjGroupDict, "Domain Admins") Then  'use the relevant group name

'the copy file code here...
  Dim FSO
  Dim mFile1
  Dim mFile2

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set mFile1 = fso.GetFile("\\server\domainadmin\test.ini")

  If fso.FileExists("d:\winnt\test.ini") then
  Set mFile2 = fso.GetFile("d:\winnt\test.ini")
  If mFile1.DateLastModified > mFile2.DateLastModified Then
         fso.CopyFile "\\server\domainadmin\test.ini", "d:\winnt\test.ini", True
  end if
  else
        fso.CopyFile "\\server\domainadmin\test.ini", "d:\winnt\test.ini", True
  end if
'Copy file code ends

End If


the power userlooks like this:

If MemberOf(ObjGroupDict, "Power User") Then  'use the relevant group name

'the copy file code here...
  Dim FSO
  Dim mFile1
  Dim mFile2

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set mFile1 = fso.GetFile("\\server\poweruser\test.ini")

  If fso.FileExists("d:\winnt\test.ini") then
  Set mFile2 = fso.GetFile("d:\winnt\test.ini")
  If mFile1.DateLastModified > mFile2.DateLastModified Then
         fso.CopyFile "\\server\poweruser\test.ini", "d:\winnt\test.ini", True
  end if
  else
        fso.CopyFile "\\server\poweruser\test.ini", "d:\winnt\test.ini", True
  end if
'Copy file code ends

End If


as you can se the only thing that is different is the group name and the directory to copy the file to and from.



0
 
_agj_Commented:
u can create a procedure which takes the group as parameter and runs its all.
0
 
_agj_Commented:
here's a sample:

DoStuff("admin")
DoStuff("normal user")
DoStuff("domain user")

function DoStuff(param1)
msgbox param1
'write your code out here with param1 as parameter.
end function

0
 
_agj_Commented:
Dostuff("Domain Admins", "domainadmin")
Dostuff("Power User", "poweruser")

Function Dostuff(GrpName, Directory)

Dim WSHNetwork
Dim strUserName ' Current user
Dim strUserDomain ' Current User's domain name
Dim ObjGroupDict ' Dictionary of groups to which the user belongs

Set WSHNetwork = WScript.CreateObject("WScript.Network")
strUserName = WSHNetwork.UserName
strUserDomain = WSHNetwork.UserDomain

' Read the user's account "Member Of" tab info across the network
' once into a dictionary object.
Set ObjGroupDict = CreateMemberOfObject(strUserDomain, strUserName)
If MemberOf(ObjGroupDict, GrpName) Then  'use the relevant group name

'the copy file code here...
  Dim FSO
  Dim mFile1
  Dim mFile2

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set mFile1 = fso.GetFile("\\server\" & Directory & "\test.ini")

  If fso.FileExists("d:\winnt\test.ini") then
  Set mFile2 = fso.GetFile("d:\winnt\test.ini")
  If mFile1.DateLastModified > mFile2.DateLastModified Then
         fso.CopyFile "\\server\" & Directory & "\test.ini", "d:\winnt\test.ini", True
  end if
  else
        fso.CopyFile "\\server\" & Directory & "\test.ini", "d:\winnt\test.ini", True
  end if
'Copy file code ends

End If

End Function
0
 
aslandkAuthor Commented:
looks good....

I have worked on the syntax myself....:


Dim path
Dim file
Dim folder
Dim foldername

path       = "\\server\c$"
file     = "test.txt"
folder   = "testfolder"

If MemberOf(ObjGroupDict, "Domain Admins") Then

foldername = "Domain Admins"
 

Dim mFile1
Dim mFile2

Set fso = CreateObject("Scripting.FileSystemObject")
Set mFile1 = fso.GetFile(path & "\" & foldername & "\" & file)

If fso.FileExists(path & "\" & folder & "\" & file) then
Set mFile2 = fso.GetFile(path & "\" & folder & "\" & file)
If mFile1.DateLastModified > mFile2.DateLastModified Then
         fso.CopyFile path & "\" & foldername & "\" & file, path & "\" & folder & "\" , True
end if
else
        fso.CopyFile path & "\" & foldername & "\" & file, path & "\" & folder & "\" , True
end if


End If


But in my example i need and if statement for each group....can i make it smarter?

Is your example better than mine?
0
 
_agj_Commented:
ya...i think my example makes it easier...
0
 
aslandkAuthor Commented:
but where do you define the diffirent folders/directories in your script?
0
 
_agj_Commented:
Function Dostuff(GrpName, Directory)

the second arg is the directory name (the part of the directory name that changes)
0
 
aslandkAuthor Commented:
but how do you declare the foldernames?

I did ilike this:


If MemberOf(ObjGroupDict, "Domain Admins") Then
foldername = "Domain Admins"
0
 
_agj_Commented:
i think its quite clear in teh code...

Dostuff("Domain Admins", "domainadmin") 'the SECOND PARAM is used to construct the directory name
Dostuff("Power User", "poweruser")

the functioon definition was:
Function Dostuff(GrpName, Directory)

the following line has the Directory parameter used.
 Set mFile1 = fso.GetFile("\\server\" & Directory & "\test.ini")

if u feel like it use ur method itself....
0
 
aslandkAuthor Commented:
sorry....has been on weekend and away from computer...thanks for your help:):)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.