Counter reading and writing to a txt file - vbscript - vbscript

I have a script as a counter subscribing to the file C:\tmp\yourtextfile.txt:
Set myFSO = CreateObject ("Scripting.FileSystemObject")
Licznik_ID = myFSO.OpenTextFile ("C:\tmp\yourtextfile.txt"). ReadAll
Licznik_ID + 1 = Licznik_ID
myFSO.OpenTextFile ( "C:\tmp\yourtextfile.txt", 2, True) .write (Licznik_ID)
how to do that instead of counting the counter in such a way:
1
2
3
4
5
6
7
8
9
10
11
e.t.c.
the Counter in the following way to read and save the file to C:\tmp\yourtextfile.txt?
00000001
00000002
00000003
00000004
00000005
00000006
00000007
00000008
00000009
00000010
00000011
e.t.c.

You need to first read the text, get the last part with split, convert it to a number, add 1, concatanate that that tot the string and write to the file.
Here your adapted script
Function PadZeros(s, l)
PadZeros = Right("00000000" & s, l)
End Function
Dim myFSO, Licznik_ID, txt, arr
Set myFSO = CreateObject ("Scripting.FileSystemObject")
txt = myFSO.OpenTextFile ("C:\tmp\yourtextfile.txt").ReadAll
arr = split(txt, " ")
Licznik_ID = arr(UBound(arr))
txt = txt & " " & PadZeros(CInt(Licznik_ID)+1, 8)
myFSO.OpenTextFile ("C:\tmp\yourtextfile.txt", 2, True).write (txt)
As an extra: since I'm switched to Ruby here the equivalent in that language to show you the power of Ruby
filename = "C:/tmp/yourtextfile.txt"
txt = File.read(filename)
txt += " %08d" % ((txt.split.last.to_i)+1).to_s
File.write(filename, txt)
or as a single line
File.read(filename).tap {|txt| File.write(filename, txt + " %08d" % ((txt.split.last.to_i)+1))}
It's pretty much explaining itself, the %08d is a formatting template for the string which takes the number and adds until 8 leading zero's, the tap method enumerates the object, in this case the last line of the file

First of all, have you tested this code? because it shouldn't work..
You should replace this line Licznik_ID + 1 = Licznik_ID with Licznik_ID = Licznik_ID + 1.
Now, in order to pad the number with leading zeros in vbscript, you'll have to write a function for that. You can use the following code:
Function LPad(str, l)
Dim n : n = 0
If l > Len(str) Then n = l - Len(str)
LPad = String(n, "0") & str
End Function
Set myFSO = CreateObject ("Scripting.FileSystemObject")
Licznik_ID = myFSO.OpenTextFile ("C:\tmp\yourtextfile.txt").ReadAll
Licznik_ID = Licznik_ID + 1
myFSO.OpenTextFile("C:\tmp\yourtextfile.txt", 2, True).write(LPad(Licznik_ID, 8))
Where LPad(Licznik_ID, 8) adds leading zeros to your number to produce an eight digit number. You can replace 8 with the preferred digit numbers.
Hope that helps :)

Related

Split int array in to two arrays even and odd VBscript error

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

splitting a string in chunks using classic asp

i got a list coma separated values (a,b,c,d,e,f,g,h,....)
i wish to split them into chunks of 5 like (a,b,c,d,e) (f,g,h,i,j)....
can someone help me with the code in classic asp ?
arr = Split(messto, ",") ' convert to array
totalemails = UBound(arr) ' total number of emails
if totalemails mod 5 = 0 then
totalloops = int(totalemails/5)
else
totalloops = int(totalemails/5) + 1
end if
x = 0
y = 0
b = 0
for x = 0 to totalloops
for counter = (5* x) to ((b+5)-1)
if Trim(arr(counter)) <> "" and isnull(trim(arr(counter))) = false then
response.Write(Trim(arr(counter)))
response.Write(counter & "<br>")
mymssto = mymssto & Trim(arr(counter)) & ","
response.Write(mymssto)
end if
next
You want to use Mod() to do this it's very powerful and underutilised function.
Here is a simple example based on the code in the question;
<%
Dim mumberToGroupBy: numberToGroupBy = 5
Dim index, counter, arr, messto
messto = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q"
arr = Split(messto, ",") ' convert to array
For counter = 0 To UBound(arr)
'Can't divide by 0 so we need to make sure our counter is 1 based.
index = counter + 1
Call Response.Write(Trim(arr(counter)))
'Do we have any remainder in the current grouping?
If index Mod numberToGroupBy = 0 Then Response.Write("<br>")
Next
%>
Output:
abcde
fghij
klmno
pq
Useful Links
A: Change response to only respond one set of values (details the use of Mod())

For next to add with classic asp

for the next "A to Z" getting results.
for into; 5 and the number 6, I want to add.
How do I make.
for k = asc("A") to asc("Z")
response.write chr(k)
next
Result :
A
B
C
..
Z
I want
A
B
C
..
Z
5
6
Such as (
k = asc("A") to asc("Z") add "5" and add"6" )
Thank You.
You can't really have a loop for this, just add separate commands:
for k = asc("A") to asc("Z")
response.write chr(k)
next
response.write "5"
response.write "6"
Another option is storing all the ASCII numbers in array then looping that array:
Dim arrLetters(), x
ReDim arrLetters(-1)
For k=Asc("A") To Asc("Z")
ReDim Preserve arrLetters(UBound(arrLetters) + 1)
arrLetters(UBound(arrLetters)) = k
Next
ReDim Preserve arrLetters(UBound(arrLetters) + 1)
arrLetters(UBound(arrLetters)) = Asc("5")
ReDim Preserve arrLetters(UBound(arrLetters) + 1)
arrLetters(UBound(arrLetters)) = Asc("6")
For x=0 To UBound(arrLetters)
k = arrLetters(x)
response.write chr(k)
Next
Erase arrLetters
strString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ56"
For i=1 To Len(strString)
Response.Write Mid(strString,i,1)
Next

Convert Binary to String

I want to convert a password which is stored in binary to normal ASCII form so that i can read it. I need a VBscript for that and script should also return this de-crypted password
Eg: Encrypted Binary password: 00110001 00110010 00110011 00110100
De-crypted Original password : 1234
I Tried this
'Binary contains the binary password
dim S
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
MSGBOX S
But the output is
0
How can achieve this. Please help!!
If you are dealing with a byte array, you must know the character encoding before you can convert it to string. Without that knowledge the bytes will be converted to the wrong characters.
The ADODB.Stream object can handle byte arrays. Here is a function that that does that:
Const adTypeBinary = 1
Const adTypeText = 2
Const adModeReadWrite = 3
Function BytesToString(bytes, charset)
With CreateObject("ADODB.Stream")
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write bytes
.Position = 0
.Type = adTypeText
.Charset = charset
BytesToString = .ReadText
End With
End Function
And here is how to use it:
MsgBox BytesToString(binary, "Windows-1252")
For the sake of completeness, this is the reverse operation:
Function StringToBytes(str, charset)
With CreateObject("ADODB.Stream")
.Mode = adModeReadWrite
.Type = adTypeText
.Charset = charset
.Open
.WriteText str
.Position = 0
.Type = adTypeBinary
StringToBytes = .Read
End With
End Function
Since your input seems to be a string like "00110001 00110010 00110011 00110100", here is a function to convert that to a byte array, which you can then use with BytesToString() shown above:
Function BinaryStringToBytes(binaryStr)
Dim b, n, i, l
l = GetLocale
SetLocale 1031
With CreateObject("ADODB.Stream")
.Mode = adModeReadWrite
.Charset = "Windows-1252"
.Type = adTypeText
.Open
For Each b In Split(binaryStr, " ")
If Len(b) <> 8 Or Replace(Replace(b, "0", ""), "1", "") <> "" Then
' invalid procedure call or argument
Err.Raise 5, "BinaryStringToBytes", _
"Only stings of 8-blocks of 0s and 1s, " & _
"separated by a single space are accepted."
End If
n = 0
For i = 0 To 7
n = n + Mid(b, 8 - i, 1) * 2^i
Next
.WriteText Chr(n)
Next
.Position = 0
.Type = adTypeBinary
BinaryStringToBytes = .Read
End With
SetLocale l
End Function
Usage
Dim input, output
input = "00110001 00110010 00110011 00110100"
output = BytesToString(BinaryStringToBytes(input), "Windows-1252")
MsgBox output ' -> "1234"
And, more importantly, it can handle multi-byte encodings properly:
input = "00110001 00110010 00110011 00110100 11000011 10100100"
output = BytesToString(BinaryStringToBytes(input), "UTF-8")
MsgBox output ' -> "1234รค"
try this code ;)
the code :
function BinaryToString(bin)
dim next_char
dim result
dim i
dim ascii
For i = 1 To Len(bin) + 18 Step 8
next_char = Mid(bin, i, 8)
ascii = BinaryToLong(next_char)
result = result & Chr(ascii)
Next
BinaryToString=result
end function
Function BinaryToLong(binary_value)
Dim hex_result
Dim nibble_num
Dim nibble_value
Dim factor
Dim bit
binary_value = UCase(Trim(binary_value))
If Left(binary_value, 2) = "&B" Then
binary_value = Mid(binary_value, 3)
End If
binary_value = Replace(binary_value, " ", "")
binary_value = Right(String(32, "0") & binary_value, 32)
For nibble_num = 7 To 0 Step -1
factor = 1
nibble_value = 0
For bit = 3 To 0 Step -1
If Mid(binary_value,1 + nibble_num * 4 + bit, 1) = "1" Then
nibble_value = nibble_value + factor
End If
factor = factor * 2
Next 'bit
hex_result = Hex(nibble_value) & hex_result
Next 'nibble_num
BinaryToLong = CLng("&H" & hex_result)
End Function
usage:
response.Write(BinaryToString("00110001001100100011001100110100"))
don't forget to take off " " blank spaces from the binary string
If I'm right, all you're after is converting a binary number to decimal (eg 0100 -> 4)?
dim binary, n, s
binary= "00110001"
For s = 1 To Len(binary)
n = n + (Mid(binary, Len(binary) - s + 1, 1) * (2 ^ (s - 1)))
Next 's
WScript.Echo binary & " = " & n
outputs
00110001 = 49
Converted from here: http://www.vb-helper.com/howto_decimal_to_binary.html
There are so many ways.
If it's a binary reg value then from Help (you did read it, didn't you)
The RegRead method returns values of the following five types.
Type Description In the Form of
REG_SZ
A string
A string
REG_DWORD
A number
An integer
REG_BINARY
A binary value
A VBArray of integers
REG_EXPAND_SZ
An expandable string (e.g., "%windir%\\calc.exe")
A string
REG_MULTI_SZ
An array of strings
A VBArray of strings
If a string, split on space (gives you an array of strings). The least significant bit is 2^0, 2^1, ..., 2^7.
EDIT
The normal way, not the only way though, to store a password, is to dump it in the registry.
Reading it gives you an array, not a scalar variable. So ...
The second method, handles cases where it's stored in a file.

if i declare 5 values in 25 size of an array, how can i find used size of the array in VB Script?

dim a(100)
a(0)=9,a(1)=3,a(2)=-3,a(3)=8,a(4)=2
how can i find size of used array(i.e used size is 5
You have to count the non-empty elements:
Option Explicit
Function UsedElms(a)
UsedElms = 0
Dim i
For i = 0 To UBound(a)
If Not IsEmpty(a(i)) Then UsedElms = UsedElms + 1
Next
End Function
Dim a(5)
a(2) = 2
a(4) = 4
WScript.Echo "ub:", UBound(a), "sz:", UBound(a) + 1, "us:", UsedElms(a)
output:
cscript 23027576.vbs
ub: 5 sz: 6 us: 2
Here's a hacky one-liner that I just thought of. It essentially counts the number of empty elements by converting them to spaces and then trimming them off.
intLastIndex = UBound(a) - Len(Join(a, " ")) + Len(Trim(Join(a, " ")))
Just for fun! Don't go putting it into your production code. It would certainly be more efficient as a two-liner:
s = Join(a, " ")
intLastIndex = UBound(a) - Len(s) + Len(Trim(s))
Ekkehard has the proper answer here, though. This hack only works if your array is filled contiguously.

Resources