Visual Basic Compare Files Task - vbscript

I currently have this code that will compare between two files only if each file has one column:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\Users\Downloads\Define Kickouts\Metadata_Account.txt", ForReading)
strCurrentDevices = objFile1.ReadAll
objFile1.Close
Set objFile2 = objFSO.OpenTextFile("C:\Users\Downloads\Define Kickouts\DataFile_Account.txt", ForReading)
Do Until objFile2.AtEndOfStream
strAddress = objFile2.ReadLine
If InStr(strCurrentDevices, strAddress) = 0 Then
strNotCurrent = strNotCurrent & strAddress & vbCrLf
End If
Loop
objFile2.Close
Wscript.Echo "Addresses without current devices: " & vbCrLf & strNotCurrent
Set objFile3 = objFSO.CreateTextFile("C:\Users\Downloads\Define Kickouts\Differences.txt")
objFile3.WriteLine strNotCurrent
objFile3.Close
However, I'm trying to figure out a way to create the script where the user can define which columns in the date file to compare against the same set of metadata files.
For example, in the data file, if we want to compare Account, Entity, and Department members, in the script, we would type in columns 1, 4, 5 based on the position in the headers...
Account, Project, Practice, Entity, Department
GL1000,P5000,PP2000,USA,D120
GL2000,P6000,PP3000,CANADA,D220
Then, the script will compare 'always' against the first column in each metadata file...
Account.csv
First column sample values:
GL5000,blah,blah,blah
GL1000,blah,blah,blah
Entity.csv
First column sample values:
ASIA,blah,blah,blah
CANADA,blah,blah,blah
Department.csv
First column sample values:
D100,blah,blah,blah
D200,blah,blah,blah
The output file will have kick-outs from the data file that aren't in the metadata files for each column.
Account Kickout.txt
GL2000
Entity Kickout.txt
USA
Department Kickout.txt
D120
D220
Any help would be appreciated!

Related

alternate method to do mail merge to create letters for mIllions of employees country wide

Hi here I am reposting my query..
I have more than million records of employee in oracle table having emp id , name,designation ,office Id and address.
I need to generate letter for each employee using word template(include company logo image), and save it as pdf..
Single pdf for each office, containing letters pertaining to employees of that office...
I have written a batch file to extract data from oracle using pls all and save it in CSV.
Then I call open word document with open_document macro to merge CSV data with word template to generate letters and then save result as pdf
Batch file is
——————————————————
For /f "delims=" %%x in (office I’d.txt) do (
sqlplus userid/pass#emp #createcsv.sql %%x
move /Y %%x.lst %%x.CSV
echo %%x > datasourcename.txt
"C:\Program Files\Microsoft Office\root\Office16\winword.exe" /x /q newtemp.dotm
———————-—————————————
My dotm file contains macro named open_document to perform mail merge and close document after saving pdf file.
This works fine at home PC.. but in office coz macros are disabled,open_document is not working.
To over come that. I write macro code to vbscript file. And called it from batch file.
vbscript :::---
Dim strPath
Dim strDataSource
Dim strTemplate
Dim doc
Dim wrdApp
Set args = Wscript.Arguments
Set wrdApp = CreateObject("Word.Application")
wrdApp.Options.Pagination = False ' suppress pagination
With CreateObject("WScript.Shell")
strPath=.CurrentDirectory
End With
strDataSource = args.Item(0)
strTemplate = "Annexure11.dot"
'Start Word using mailmerge template
wrdApp.Documents.Open (strPath & "\" & strTemplate)
Set oMergeDoc = wrdApp.ActiveDocument
Set oMerge = oMergeDoc.MailMerge
oMerge.MainDocumentType = 0
msgBox "Before Open Datasource"
oMerge.OpenDataSource strPath & "\" & strDataSource &".csv"
msgBox "After Open Datasource"
oMerge.Destination = 0 ' 0 = wdSendToNewDocument
omerge.SuppressBlankLines=True
omerge.DataSource.FirstRecord=1
omerge.DataSource.LastRecord=-16
If omerge.State=2 then oMerge.Execute pause= false
'Do not display the mail merge document
wrdApp.Visible = False
'Save mail merge document as doc and pdf
wrdApp.ActiveDocument.SaveAs strPath & "\"& arg(0) &".pdf" , 17
wrdApp.ActiveDocument.SaveAs strPath &".doc"
omergedoc.Close False
wrdApp.Quit
Set wrdApp = Nothing
GetObject(, "Word.Application").Quit False
when I run the above code, I get msgbox with msg "Before Open Datasource" but never gets msg "After Open Datasource"
*Please suggest some alternate method to get desired output.

VBScript to copy/move file by lowest value in filename

I'm fairly new to scripting and am in need of some help. I have come across a unique situation for a Non-Profit client of ours that requires us to compare two or more files in a specific folder and move the file with the lowest numerical value in the filename.
This organization runs a non-profit radio station which has content submitted from hundreds of volunteers that name their files (when they record more than one) with various numbers at the end that either represent the date or the order in which the files are to be aired.
Essentially I am looking to create a vbscript (because I think it can be done this way) that will run with windows task scheduler 30 minutes prior to the first air date of the content and move the file with the lowest value (if more than one file exists) to a folder where it will be automatically processed by the radio automation software.
Examples of files in a folder might look something like these:
Folder1: (in this instance, "news.mp3" is the lowest value)
news.mp3
news1.mp3
news2.mp3
Folder2:
entertainment24.mp3
entertainment26.mp3
Folder3:
localnews081420.mp3
localnews081520.mp3
Honestly, on this one, I'm not even sure where to start. I've found several scripts that can look at file date or a specific numerical or date format in the filename, but none that can parse numbers from a filename and move/copy a file based on the numerical value. I'm hoping there is someone out there smarter than me that can point me in the right direction. Thanks for looking at my problem!
One script I've been playing with (from the scripting guy) looks at specific years in a filename:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colFiles = objWMIService.ExecQuery _
(“ASSOCIATORS OF {Win32_Directory.Name=’C:\Test’} Where ” _
& “ResultClass = CIM_DataFile”)
Set objRegEx = CreateObject(“VBScript.RegExp”)
For Each objFile in colFiles
objRegEx.Global = True
objRegEx.Pattern = “\d{4}”
strSearchString = objFile.FileName
Set colMatches = objRegEx.Execute(strSearchString)
strYear = colMatches(0).Value
strNewFile = “C:\Test\” & strYear & “\” & objFile.FileName & _
“.” & objFile.Extension
objFile.Copy(strNewFile)
objFile.Delete
Next
...but I can't seem to make the leap to regular numbers and then take a lowest value...
You can use FileSystemObject to Work with Drives, Folders and Files.
Also i used GETNUM function to get number.
Try my way :
sFolder = "C:\Test\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile in oFSO.GetFolder(sFolder).Files
Number=GETNUM(objFile.Name)
strNewFile = sFolder & Number & "\" & objFile.Name
If NOT (oFSO.FolderExists(sFolder & Number)) Then
oFSO.CreateFolder(sFolder & Number)
End If
oFSO.MoveFile objFile, strNewFile
Next
Function GETNUM(Str)
For i=1 To Len(Str)
if IsNumeric(Mid(Str,i,1)) Then
Num=Num&Mid(Str,i,1)
End if
Next
GETNUM=Num
End Function
For understanding the used code and how they work, open these sites and read all pages very carefully.
MoveFile method
Vbs Script to check if a folder exist

CurrentRegion.Select and Table format in VBS

I'm very new (1 week) to visual basic and basically I'm trying to automate some repetitive work, now to the point , within a number of files produced with varying data I need to format the selected range as a table (medium 9) but i'm in a block at the moment and need some help and would really appreciate it, here is what i have so far>>>>
Option Explicit
Dim strDate, strRepDate, strPath, strPathRaw , strDate2
dim dteTemp, dteDay, dteMth, dteYear, newDate, myDate
myDate = Date()
dteTemp = DateAdd("D", -1, myDate)
dteDay = DatePart("D", dteTemp)
dteMth = DatePart("M", dteTemp)
dteYear = DatePart("YYYY", dteTemp)
If (Len(dteDay) = 1) Then dteDay = "0" & dteDay
If (Len(dteMth) = 1) Then dteMth = "0" & dteMth
strDate = dteYear&"-"&dteMth&"-"&dteDay
strDate2 = dteYear&""&dteMth&""&dteDay
Dim objXLApp, objXLWb, objXLWs
Set objXLApp = CreateObject("Excel.Application")
Set objXLWb = objXLApp.Workbooks.Open("C:\Users\CuRrY\Desktop\"&strDate2&"\Agent Daily Disposition "&strDate2&".xls")
objXLApp.Application.Visible = True
'start excell
Set objXLWs = objXLWb.Sheets(1)
'objXLWs.Cells(Row, Column ).Value
With objXLWs
objXLWs.Cells(3, 1).Value = "Agent Name"
'objXLWs.Range("A3").Select
objXLWs.Range("A3").CurrentRegion.Select
'End With
as you can see i reached as far as CurrentRegion.Select but how to format selected cells into (medium 9) i've tried so much and failed
Thanks for any help
You can configure the CurrentRegion(which represents a Range object) through the SpecialCells Submethod. Although your conditions are specific to your xls sheet, you will still have to follow the formatting available through the specialcells() method properties. Also, by utilizing the currentregion property, the page assumes you have a xls header. So it is important to verify your table structure before trying to incorporate this property.
For instance:
Sub FillIn()
Range("A1").CurrentRegion.SpecialCells(xlCellTypeBlanks).FormulaR1C1 _
= "=R[-1]C"
Range("A1").CurrentRegion.Value = Range("A1").CurrentRegion.Value
End Sub
View the available properties that can be applied to CurrentRegion -> Here
And the MSDN Article -> Here

I have two text files, I need to merge the two with a date stamp using VBscript

I have two .txt files; I have written a VBscript to identify the two last modified files. The code echoes the two modified files separately. I need to merge these two modified files and provide a different name with a date stamp.
Example:
txt1.txt
txt2.txt
After merge:
txt09022011.txt
Assuming you just want to dump the contents of each text file in sequence into a new text file:
Dim strInputPath1, strInputPath2, strOutputPath
Dim txsInput1, txsInput2, txsOutput
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
strInputPath1 = "C:\txt1.txt"
strInputPath2 = "C:\txt2.txt"
strOutputPath = "C:\txt" & Format(Now, "ddmmyyyy") & ".txt"
' For the timestamp I use Now (today's date). Can also choose some other date.
Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1)
Set txsInput2 = FSO.OpenTextFile(strInputPath2, 1)
Set txsOutput = FSO.CreateTextFile(strOutputPath)
txsOutput.Write txsInput1.ReadAll
txsOutput.Write txsInput2.ReadAll
txsInput1.Close
txsInput2.Close
txsOutput.Close

VBScript to export all members of multiple Active Directory groups?

Is there a way of exporting all the members of multiple Active Directory groups at once using a VBScript? Preferably the output would be the usernames listed under the group they are a member of.
I have the following which allows me to export the members of 1 AD Group at a time, but I am at a loss as to how to modify it to look at multiple groups.
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set outfile = fso.CreateTextFile("Members.csv")
Set objGroup = GetObject("LDAP://cn=*GROUPNAME*,OU=Groups,DC=domain,DC=local")
objGroup.GetInfo
arrMembersOf = objGroup.GetEx("member")
For Each GetObject in ObjGroup
outfile.WriteLine objGroup.Name
Next
For Each strMember in arrMembersOf
outfile.WriteLine strMember
Next
Any ideas?
Yeah, this is possible, but I think you might need to change your approach slightly. You need to write an LDAP query to query two groups at once, rather than just setting your scope to a particular group.
So, try reworking your script like this:
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objRootDSE = Nothing
Set ad = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
ad.ActiveConnection = adoConnection
'Put the distinguishedname of your two groups here:
strFilter = "(|(memberof=CN=Group Name,OU=....)(memberof=CN=Group Name 2,OU=....))"
'Chose what you want to return here:
strAttributes = "samaccountname,cn"
strQuery = "<LDAP://" & strDNSDomain & ">" & ";" & strFilter & ";" & strAttributes & ";subtree"
ad.CommandText = strQuery
ad.Properties("SearchScope") = 2
ad.Properties("Page Size") = 1000
ad.Properties("Cache Results") = False
Set objRS = ad.Execute
Now you've got all the results in a recordset, you can work your way through them writing each one to a file or whatever you want to do. So something like:
Do Until objRS.EOF
'Do something with each value
objRS.Fields("samaccountname")
objRS.MoveNext
Loop
Any use? I'm assuming here you know a little bit about writing LDAP queries
The best place to find scripts for Active Directory is Microsoft's Script Center Repository.
You can find a script listing all groups and all group members here ("List all groups in the domain and all members of the groups").

Resources