I am developing a script to divide the number of files in a folder into four groups. These will be turned into four batch files but for now the issue is dividing them up as evenly as possible.
The script below will work somewhat - if I have a Count that will be divided by 4 evenly but if I have an odd number, no go and less than four will crash. You can run the script just replace the "C:\1_SourceData\Section_16\" with your own path of files. If you un-comment the section 'Add remainder to front', it was to thow any extra files, like an odd number, to the first batch but that does not quite work. The number of files in the folder will range from 1 to 25.
Any help would be most appreciated.
Option Explicit
Dim fileList : Set fileList = GetFileList("C:\1_SourceData\Section_16\")
Dim NumOfFiles : NumOfFiles = fileList.Count - 1
Dim modNumber : modNumber = NumOfFiles/4
Dim remainder : remainder = NumOfFiles Mod modNumber
Dim string1 : string1 = "batch" & batchCounter
Dim string2 : string2 = ""
'Add remainder to front
'Dim i : i = 0
'For i = NumOfFiles - remainder To NumOfFiles
' string2 = string2 & vbTab & fileList(i) & vbNewLine
'Next
Dim batchCounter : batchCounter = 1
Dim file
Dim j : j = 0
For Each file In fileList
string2 = string2 & vbTab & file & vbNewLine
j = j + 1
If j Mod modNumber = 0 Then
WScript.Echo string1 & vbNewLine & string2
batchCounter = batchCounter + 1
string1 = "batch" & batchCounter
string2 = ""
End If
Next
Public Function GetFileList(path)
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim fileList : Set fileList = CreateObject("System.Collections.ArrayList")
Dim InfFolder : Set InfFolder = objFSO.GetFolder(path)
Dim File
For Each File In objFSO.GetFolder(path).Files
fileList.Add File
Next Set GetFileList = fileList
End Function
The problem is: the .Files collection is accessible via For Each only. A 'distribution by number' (think modulo) needs an extra counter. Demo script:
Option Explicit
ReDim a(3) ' 4 groups/collections
Dim i
For i = 0 To UBound(a)
Set a(i) = CreateObject("System.Collections.ArrayList")
Next
i = 0
Dim f
' fake a list of elms accessible via For Each only
For Each f In Split("a b c d e f g h i j k l m n")
a(i Mod 4).Add f ' use Mod to determine the 'bucket'
i = i + 1 ' counter needed for Mod
Next
For i = 0 To UBound(a)
WScript.Echo i, Join(a(i).ToArray())
Next
output:
cscript 40639293.vbs
0 a e i m
1 b f j n
2 c g k
3 d h l
You could structure your loop differently.
There are F files that should be devided into B batches of X files each. Two things can happen:
F is an exact multiple of B, in which case X = F / B
F is not an exact multiple of B, in which case X = (F / B) + 1
Therefore we can write two loops that (together) count from 1 to F:
Option Explicit
Const BATCHES = 4
Const PATH = "C:\1_SourceData\Section_16"
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
Dim fileList : Set fileList = GetFileList(PATH)
Dim b, i, f, x
f = fileList.Count
x = CInt(f / BATCHES)
If x * BATCHES < f Then x = x + 1
For b = 0 To BATCHES - 1
If (b * x < f) Then WScript.Echo "batch" & (b + 1)
For i = b * x To (b + 1) * x - 1
If (i < f) Then WScript.Echo vbTab & fileList(i)
Next
Next
Function GetFileList(path)
Dim file
Set GetFileList = CreateObject("System.Collections.ArrayList")
For Each file In FSO.GetFolder(path).Files
GetFileList.Add File
Next
End Function
Related
I have written code for an array of numbers which prints out. I'm now writing code to split the array into even and off numbers. I've started off with an if statement to separate the numbers but i'm struggling to find a solution on how to do it. My code below is failing as it's unable to split the numbers.
Sub main()
a=Array(5,10,15,20)
for each x in a
Msgbox(x)
If MyArray(I) / 2 = MyArray(I)
List1.AddItem MyArray(I) ' Even Integers
Else
List2.AddItem MyArray(I) ' Odd Integers
End if
next
End Sub
As Lankymart suggests, the simplest approach would be to use Mod() and check if the remainder is 1 or 0, but you can also do it with the approach you seemed to be working towards:
If MyArray(index)/2 = Int(MyArray(index)/2) Then
' Even number
Else
' Odd number
End If
Mod() approach:
If MyArray(index) Mod 2 = 0 Then
' Even number
Else
' Odd number
End If
Here's a complete subroutine that demonstrates what you are trying to do:
Dim arr(4) As Integer
Dim arrEven() As Integer
Dim iEvenValues As Integer
Dim arrOdd() As Integer
Dim iOddValues As Integer
Dim iCounter As Integer
' Initialize array
arr(0) = 5
arr(1) = 10
arr(2) = 15
arr(3) = 20
For iCounter = 1 To UBound(arr)
If arr(iCounter - 1) Mod 2 = 0 Then
iEvenValues = iEvenValues + 1
ReDim Preserve arrEven(iEvenValues)
arrEven(iEvenValues - 1) = arr(iCounter - 1)
Else
iOddValues = iOddValues + 1
ReDim Preserve arrOdd(iOddValues)
arrOdd(iOddValues - 1) = arr(iCounter - 1)
End If
Next
Dim sValues As String
sValues = "Even values (" & iEvenValues & "):"
For iCounter = 1 To UBound(arrEven)
sValues = sValues & " " & arrEven(iCounter - 1)
Next
MsgBox sValues
sValues = "Odd values (" & iOddValues & "):"
For iCounter = 1 To UBound(arrOdd)
sValues = sValues & " " & arrOdd(iCounter - 1)
Next
MsgBox sValues
I have below sample data. I want to convert this string into an array
device_name="Text Data" d_id=7454579598 status="Active" Key=947-4378-43248274
I tried below:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("d:\vbfile.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , " ")
For i = 0 to Ubound(arrServiceList)
Wscript.Echo arrServiceList(i)
Next
Loop
it generates below
device_name="Text
Data"
d_id=7454579598
status="Active"
Key=947-4378-43248274
Expected output
device_name="Text Data"
d_id=7454579598
status="Active"
Key=947-4378-43248274
How about this approach:
Option Explicit
Const ForReading = 1
Dim FSO, keyValueExpr
Set FSO = CreateObject("Scripting.FileSystemObject")
Set keyValueExpr = New RegExp
keyValueExpr.Pattern = "\b(\w+)=(""[^""]*""|\S*)"
keyValueExpr.Global = True
Dim result, record, match
Set result = CreateObject("Scripting.Dictionary")
With FSO.OpenTextFile("D:\vbfile.txt", ForReading)
While Not .AtEndOfStream
Set record = CreateObject("Scripting.Dictionary")
result.Add result.Count + 1, record
For Each match In keyValueExpr.Execute(.ReadLine)
record.Add match.SubMatches(0), match.SubMatches(1)
Next
Wend
.Close
End With
Dim msg, lineNo, key
For Each lineNo In result
msg = "Line " & lineNo & vbNewLine
For Each key In result(lineNo)
msg = msg & vbNewLine & key & ": " & result(lineNo)(key)
Next
MsgBox msg
Next
It uses a regular expression that can identify key-value pairs that fulfill these conditions:
The key is a string of characters (a-z), digits (0-9) or underscores (_)
The value is anything that is either enclosed in double quotes or anything except a space.
Compare https://regex101.com/r/zL2mX5/1
The program creates nested dictionaries, the outer dictionary holding all lines of the file with the corresponding line numbers (1..n) for keys, each inner dictionary holds the key-value pairs found on each line.
This layout gives you the opportunity to address every value very conveniently:
value = result(3)("status")
Here is a function that might help. It takes a string and a delimiter and returns an array obtained by splitting on the delimiter -- whenever the delimiter isn't inside a quote:
Function SmartSplit(s, d)
Dim c, i, j, k, n, A, quoted
n = Len(s)
ReDim A(n - 1)
quoted = False
i = 1
k = 0
For j = 1 To n
c = Mid(s, j, 1)
If c = """" Then quoted = Not quoted
If c = d And Not quoted Then
A(k) = Mid(s, i, j - i)
k = k + 1
i = j + 1
End If
Next
If i < n Then
A(k) = Mid(s, i)
Else
k = k - 1
End If
ReDim Preserve A(k)
SmartSplit = A
End Function
In your example -- just replace Split by SmartSplit and it should work.
I get the error:
Line: 23
Char: 4
Subscript out of range: 'numbers'
which is at: if numbers(i) = a then
I have made this in javascript and it works, but I need to convert it.
function info(numbers)
dim numbers2(99999)
numbers3 = ""
dim low
dim high
dim mean
dim halve
total = 0
dim spaces(99999)
dim a
dim b
dim i
dim c
dim whole
dim meadian
dim mode1
dim mode2
dim intResult
dim med
x = UBound(numbers)+1
For a=0 To 999999 Step 1
For i=0 To x Step 1
if numbers(i) = a then
c = numbers(i)
numbers2 = c
numbers3 = numbers3 + c + " "
low = numbers2(0)
high = a
total = total + c
end if
Next
Next
halve = UBound(numbers2)/2
whole = UBound(numbers2)
intResult = whole Mod 2
If intResult = 0 Then
halve = halve - 0.5
median = numbers2(halve)
med = true
Else
median = (numbers2(halve1)+numbers2(halve1-1))/2
med = false
End if
mean = total / UBound(numbers)
if med = true then
msgbox(numbers3 & chr(13) & chr(13) & "Lowest: " & low & chr(13) & "Highest: " & high & chr(13) & "Total: " & total & chr(13) & "Median: " & median & chr(13))
else
msgbox(numbers3 & chr(13) & chr(13) & "Lowest: " & low & chr(13) & "Highest: " & high & chr(13) & "Total: " & total & chr(13) & "Median: " & median & " -" & numbers2(halve1) & "x" & numbers2(halve1-1) & chr(13))
end if
end function
dim q(19,291,29)
info(q)
And also, how can I be able to put q in inside a inputbox?
Just ask if you want the javascript code.
If you're going to declare your variables (good idea for production code): use Option Explicit and declare all variables. Otherwise don't bother.
Variable declarations become a little more readable if you put them on one line (comma-separated):
Dim numbers2(99999), numbers3, low, high, mean, halve, total
Dim spaces(99999), ...
...
numbers3 = ""
total = 0
As #RogerRowland has mentioned, the line x = UBound(numbers)+1 will return an index 1 greater than the upper boundary of your array, so the loop
For i=0 To x Step 1
if numbers(i) = a then
...
end if
Next
will try to access an element outside the array in the last loop cycle. Better do it like this:
For i=0 To UBound(numbers)
If numbers(i) = a Then
...
End If
Next
Step 1 is the default, BTW, so it can be omitted.
The Type Mismatch error you got after fixing that is most likely because you declared numbers2 as a static array:
dim numbers2(99999)
but then assign a scalar to it inside the loop:
numbers2 = c
That line should probably be
numbers2(i) = c
How can I remove duplicates from an array in vbscript?
Code:
dim XObj(100),xObjXml
for s=0 to xObjXml.length-1
XObj(s)=xObjXml(s).getAttribute("xsx")
next
Please suggest a better answer for this.
Use a Dictionary to gather the unique items of the array:
>> a = Array(1, 2, 3, 1, 2, 3)
>> WScript.Echo Join(a)
>> Set d = CreateObject("Scripting.Dictionary")
>> For i = 0 To UBound(a)
>> d(a(i)) = d(a(i)) + 1
>> Next
>> WScript.Echo Join(d.Keys())
>>
1 2 3 1 2 3
1 2 3
>>
(BTW: There is no .length property for VBScript arrays)
Added:
The .Keys() method of the dictionary returns an array of the (unique) keys:
>> b = d.Keys()
>> WScript.Echo Join(b), "or:", b(2), b(1), b(0)
>>
1 2 3 or: 3 2 1
Added II: (aircode!)
Trying to get the unique attributes of the objects in an XML collection:
Dim xObjXml : Set xObjXml = ... get some collection of XML objects ...
Dim dicAttrs : Set dicAttrs = CreateObject("Scripting.Dictionary")
Dim i
For i = 0 To xObjXml.length - 1
Dim a : a = xObjXml(i).getAttribute("xsx")
dicAttrs(a) = dicAttrs(a) + 1
Next
Dim aAttrs : aAttrs = dicAttrs.Keys()
Added III (sorry!):
.Keys() is a method, so it should be called as such:
Dim aAttrs : aAttrs = dicAttrs.Keys()
Added IV:
For a working sample see here.
If you don't want a Dictionary you can use the following to compare each element in the array to itself.
Info = Array("Arup","John","Mike","John","Lisa","Arup")
x = 0
z = ubound(Info)
Do
x = x + 1
Do
z = z - 1
If x = z Then
Info(x) = Info(z)
ElseIf Info(x) = Info(z) Then
Info(x) = ""
End If
Loop Until z=0
z = ubound(Info)
Loop Until x = ubound(Info)
For each x in Info
If x <> "" Then
Unique = Unique & Chr(13) & x
End If
Next
MsgBox Unique
Quick question that I've been struggling with. I have 2 arrays of different lengths that contain strings.
I want to output a new array which removes BOTH the elements if a duplicate is detected. At the moment it only removes duplicates but leaves the original which is incorrect for what I am trying to accomplish.
E.g.
input = array ("cat","dog","mouse","cat")
expected output = array ("dog","mouse")
actual output = array ("cat","dog","mouse")
Code is below:
Sub removeDuplicates(CombinedArray)
Dim myCol As Collection
Dim idx As Long
Set myCol = New Collection
On Error Resume Next
For idx = LBound(CombinedArray) To UBound(CombinedArray)
myCol.Add 0, CStr(CombinedArray(idx))
If Err Then
CombinedArray(idx) = Empty
dups = dups + 1
Err.Clear
ElseIf dups Then
CombinedArray(idx - dups) = CombinedArray(idx)
CombinedArray(idx) = Empty
End If
Next
For idx = LBound(CombinedArray) To UBound(CombinedArray)
Debug.Print CombinedArray(idx)
Next
removeBlanks (CombinedArray)
End Sub
Thanks for all help and support in advance.
What about using Scripting.Dictionary? Like this:
Function RemoveDuplicates(ia() As Variant)
Dim c As Object
Set c = CreateObject("Scripting.Dictionary")
Dim v As Variant
For Each v In ia
If c.Exists(v) Then
c(v) = c(v) + 1
Else
c.Add v, 1
End If
Next
Dim out() As Variant
Dim nOut As Integer
nOut = 0
For Each v In ia
If c(v) = 1 Then
ReDim Preserve out(nOut) 'you will have to increment nOut first, if you have 1-based arrays
out(nOut) = v
nOut = nOut + 1
End If
Next
RemoveDuplicates = out
End Function
Here is a quick example. Let me know if you get any errors.
Sub Sample()
Dim inputAr(5) As String, outputAr() As String, temp As String
Dim n As Long, i As Long
inputAr(0) = "cat": inputAr(1) = "Hen": inputAr(2) = "mouse"
inputAr(3) = "cat": inputAr(4) = "dog": inputAr(5) = "Hen"
BubbleSort inputAr
For i = 1 To UBound(inputAr)
If inputAr(i) = inputAr(i - 1) Or inputAr(i) = temp Then
inputAr(i - 1) = "": temp = inputAr(i): inputAr(i) = ""
End If
Next i
n = 0
For i = 1 To UBound(inputAr)
If inputAr(i) <> "" Then
n = n + 1
ReDim Preserve outputAr(n)
outputAr(n) = inputAr(i)
End If
Next i
For i = 1 To UBound(outputAr)
Debug.Print outputAr(i)
Next i
End Sub
Sub BubbleSort(arr)
Dim value As Variant
Dim i As Long, a As Long, b As Long, c As Long
a = LBound(arr): b = UBound(arr)
Do
c = b - 1
b = 0
For i = a To c
value = arr(i)
If (value > arr(i + 1)) Xor False Then
arr(i) = arr(i + 1)
arr(i + 1) = value
b = i
End If
Next
Loop While b
End Sub
EDIT
Another way without sorting
Sub Sample()
Dim inputAr(5) As String, outputAr() As String
Dim n As Long, i As Long, j As Long
Dim RemOrg As Boolean
inputAr(0) = "cat": inputAr(1) = "Hen": inputAr(2) = "mouse"
inputAr(3) = "cat": inputAr(4) = "dog": inputAr(5) = "Hen"
For i = 0 To UBound(inputAr)
For j = 1 To UBound(inputAr)
If inputAr(i) = inputAr(j) Then
If i <> j Then
inputAr(j) = "": RemOrg = True
End If
End If
Next
If RemOrg = True Then
inputAr(i) = ""
RemOrg = False
End If
Next i
n = 0
For i = 0 To UBound(inputAr)
If inputAr(i) <> "" Then
n = n + 1
ReDim Preserve outputAr(n)
outputAr(n) = inputAr(i)
End If
Next i
For i = 1 To UBound(outputAr)
Debug.Print outputAr(i)
Next i
End Sub