Solved

change kix script to vbs script

Posted on 2004-10-05
25
412 Views
Last Modified: 2010-05-18
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:)
0
Comment
Question by:aslandk
  • 15
  • 10
25 Comments
 
LVL 7

Expert Comment

by:_agj_
ID: 12225311
use this to find if the current user is a  member of grp administrator:

http://hacks.oreilly.com/pub/h/1130
0
 

Author Comment

by:aslandk
ID: 12225384
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
 
LVL 7

Expert Comment

by:_agj_
ID: 12225401
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
 
LVL 7

Expert Comment

by:_agj_
ID: 12225411
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
 

Author Comment

by:aslandk
ID: 12225451
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
 
LVL 7

Expert Comment

by:_agj_
ID: 12225460
-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
 
LVL 7

Expert Comment

by:_agj_
ID: 12225471
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
 
LVL 7

Expert Comment

by:_agj_
ID: 12225477
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
 

Author Comment

by:aslandk
ID: 12225490
testing your "gold script":):)......will respond soon.....but thanks so far
0
 

Author Comment

by:aslandk
ID: 12225592
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
 
LVL 7

Expert Comment

by:_agj_
ID: 12225664
u have missed the earlier:
If fso.FileExists("d:\winnt\test.ini") then

which is the corresponding If for the Else statement
0
 
LVL 7

Expert Comment

by:_agj_
ID: 12225674
the logic is:

if "file 2 exists" then

  if file 1 is newer then
     copy file
  end if

else
  copy anyways
end if
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:aslandk
ID: 12226283
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
 

Author Comment

by:aslandk
ID: 12226324
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
 
LVL 7

Expert Comment

by:_agj_
ID: 12234214
u can create a procedure which takes the group as parameter and runs its all.
0
 
LVL 7

Expert Comment

by:_agj_
ID: 12235101
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
 
LVL 7

Expert Comment

by:_agj_
ID: 12235141
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
 

Author Comment

by:aslandk
ID: 12235178
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
 
LVL 7

Expert Comment

by:_agj_
ID: 12235511
ya...i think my example makes it easier...
0
 

Author Comment

by:aslandk
ID: 12235647
but where do you define the diffirent folders/directories in your script?
0
 
LVL 7

Expert Comment

by:_agj_
ID: 12246171
Function Dostuff(GrpName, Directory)

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

Author Comment

by:aslandk
ID: 12246870
but how do you declare the foldernames?

I did ilike this:


If MemberOf(ObjGroupDict, "Domain Admins") Then
foldername = "Domain Admins"
0
 
LVL 7

Expert Comment

by:_agj_
ID: 12258183
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
 
LVL 7

Accepted Solution

by:
_agj_ earned 250 total points
ID: 12275452
if its through i guess u cud close the qn ;)
0
 

Author Comment

by:aslandk
ID: 12279036
sorry....has been on weekend and away from computer...thanks for your help:):)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now