Link to home
Start Free TrialLog in
Avatar of aslandk
aslandk

asked on

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:)
Avatar of _agj_
_agj_

use this to find if the current user is a  member of grp administrator:

http://hacks.oreilly.com/pub/h/1130
Avatar of aslandk

ASKER

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??
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
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
Avatar of aslandk

ASKER

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:)
-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
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
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...
Avatar of aslandk

ASKER

testing your "gold script":):)......will respond soon.....but thanks so far
Avatar of aslandk

ASKER

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?
u have missed the earlier:
If fso.FileExists("d:\winnt\test.ini") then

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

if "file 2 exists" then

  if file 1 is newer then
     copy file
  end if

else
  copy anyways
end if
Avatar of aslandk

ASKER

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?

Avatar of aslandk

ASKER

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.



u can create a procedure which takes the group as parameter and runs its all.
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

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
Avatar of aslandk

ASKER

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?
ya...i think my example makes it easier...
Avatar of aslandk

ASKER

but where do you define the diffirent folders/directories in your script?
Function Dostuff(GrpName, Directory)

the second arg is the directory name (the part of the directory name that changes)
Avatar of aslandk

ASKER

but how do you declare the foldernames?

I did ilike this:


If MemberOf(ObjGroupDict, "Domain Admins") Then
foldername = "Domain Admins"
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....
ASKER CERTIFIED SOLUTION
Avatar of _agj_
_agj_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aslandk

ASKER

sorry....has been on weekend and away from computer...thanks for your help:):)