Google
Showing posts with label VBScript. Show all posts
Showing posts with label VBScript. Show all posts

Thursday, January 3, 2008

VBScript: Search Files Older than 30 Days (including sub folder) and Move to Another Location

Sometime we need an automated task to move some of the files from one location to another, examples are bleow.
· SMTP Logs
· SPAM Product Logs
· Some Application Logs
· Temporary Files

So I have created a script that allows you to search all files in a location (including sub folders) which are older than 30 days, move it to specified location and record it into a log file “Move-Result.txt”.

Give the input path where you want to search the files at below line in the script.
SPath = "c:\temp\"

Give the path of destination location at below line in the script.
objFSO.MoveFile FileName, "d:\test\"


====== Script Code ======
Dim objFSO, ofolder, objStream

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objNet = CreateObject("WScript.NetWork")
Set FSO = CreateObject("Scripting.FileSystemObject")
set outfile = fso.createtextfile("Move-Result.txt",true)
SPath = "c:\temp\"

ShowSubfolders FSO.GetFolder(spath)

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
CheckFolder(subfolder)
ShowSubFolders Subfolder
Next
End Sub

'CheckFolder(objFSO.getFolder(SPath))

Sub CheckFolder(objCurrentFolder)
Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile
Const OverwriteExisting = TRUE
currDate = Date
dtmDate = Date - 30
strTargetDate = ConvDate(dtmDate)
For Each objFile In objCurrentFolder.Files
FileName = objFile
'WScript.Echo FileName
strDate = ConvDate(objFile.DateCreated)
'strDate = ConvDate(objFile.DateLastModified)
If strDate < strTargetDate Then
objFSO.MoveFile FileName, "d:\test\"
outfile.writeline filename
End If
Next
End Sub

Function ConvDate (sDate) 'Converts MM/DD/YYYY HH:MM:SS to string YYYYMMDD
strModifyDay = day(sDate)
If len(strModifyDay) < 2 Then
strModifyDay = "0" & strModifyDay
End If
strModifyMonth = Month(sDate)
If len(strModifyMonth) < 2 Then
strModifyMonth = "0" & strModifyMonth
End If
strModifyYear = Year(sDate)
ConvDate = strModifyYear & strModifyMonth & strModifyDay
End Function
====== Script Code ======

Tuesday, November 20, 2007

VBScript: Copy Group Membership From One Group to Anothe

Copies the group membership from one group to another (which is not possible by any GUI Tool)
Article is also available at:
http://www.microsoft.com/technet/scriptcenter/csc/scripts/ad/groups/cscad110.mspx

=========Script Code===========

strSGroupDN = InputBox ("Enter the DN of Source Group" & VBCRLF &_
vbcrlf& _
vbcrlf& _
"e.g. CN=Source Group,OU=Users,DC=NWTraders,DC=com")
strDGroupDN = InputBox ("Enter the DN of Destination Group" & VBCRLF &_
vbcrlf& _
vbcrlf& _
"e.g. CN=Destination Group,OU=Users,DC=NWTraders,DC=com")

set dicSeenGroupMember = CreateObject("Scripting.Dictionary")
set objDGroup = GetObject("LDAP://" & strDGroupDN)
DisplayMembers "LDAP://" & strSGroupDN, dicSeenGroupMember
Function DisplayMembers (strGroupADsPath, dicSeenGroupMember)
set objGroup = GetObject(strGroupADsPath)
for each objMember In objGroup.Members
objDGroup.Add("LDAP://" & objMember.distinguishedName)
next
End Function
MsgBox "Group Members have been copied to Destination Group"

=========Script Code===========

Tuesday, October 30, 2007

VBScript: Write Group Membership Information to a Text File

Retrieves the membership of a specified group (including the members of any nested groups), and then writes that information to a text file. Which is not possible with normal AD tools.

Article is also available at:
http://www.microsoft.com/technet/scriptcenter/csc/scripts/ad/groups/cscad109.mspx

=========Script Code===========

' ---------- Determine variable -----------
Dim outfile
'----Create File System Object Space ----
Set fso = CreateObject("Scripting.FileSystemObject")
' ---- Define variable for Output file to write ----
set outfile = fso.createtextfile("Members.txt",true)
on error resume next
'--Call the Function for Finding Group Membership - including nested groups--
strGroupDN = "CN=Domain Admins,CN=Users,DC=Domain,DC=com"
'*****Give here DN of Group*****'
strSpaces = " "
set dicSeenGroupMember = CreateObject("Scripting.Dictionary")
outfile.writeline "Members of " & strGroupDN & ":"DisplayMembers "LDAP://" & strGroupDN, strSpaces, dicSeenGroupMember
Function DisplayMembers ( strGroupADsPath, strSpaces, dicSeenGroupMember)
set objGroup = GetObject(strGroupADsPath)
for each objMember In objGroup.Members
outfile.writeline strSpaces & objMember.name
if objMember.Class = "group" then
if dicSeenGroupMember.Exists(objMember.ADsPath) then
outfile.writeline strSpaces & " ^ already seen group member " & "(stopping to avoid loop)"
else
dicSeenGroupMember.Add objMember.ADsPath, 1
DisplayMembers objMember.ADsPath, strSpaces & " ", dicSeenGroupMember
end if
end if
next
End Function
MsgBox "Group Membership has been dumped to -Members.txt- file"

=========Script Code===========
Google Groups
Subscribe to IT_Discussions
Email:
Visit this group