I wrote the VBS script to count all the folders under C:\ , the code as below:
set wshell = createobject("WScript.Shell")
dim fso,file,subfolder,folder
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("\\192.168.0.201\thang\Learning\test.txt")
Set folder = fso.GetFolder("C:\")
dim i,j
i = 0
j = 0
For Each subfolder In folder.SubFolders
'file.WriteLine """" & subfolder.path & """" 'print quotation marks trong VBS
'arr(i) = subfolder.path
i=i+1
Next
msgbox "i = " & i 'In my case , C folders has 19 subfolders in there
dim arr
arr = Array(i) 'declare the array which has i member
' For Each subfolder In folder.SubFolders
' 'file.WriteLine """" & subfolder.path & """" 'print quotation marks trong VBS
' arr(j) = subfolder.path
' j=j+1
' Next
' msgbox arr(0)
' msgbox arr(1)
msgbox "lbound = " & lbound(arr) 'when ran the code, it always show lbound = 0
msgbox "ubound = " & ubound(arr) 'when ran the code, it always show ubound = 0
file.close()
It show the value of i = 19 , then i define 1 array with i members , then check its lbound and ubound , however it shows lbound = 0 and ubound = 0. Can you please help correct my code ?
See: Array Function
arr = Array(i)
creates an array with a single element i.
If you need to create an array specifying a variable as size, you need to use the ReDim Statement
Redim arr(i)
Hello everyone I got a doubt, about how to read a .txt file starting in specific line, I got the line number from I want to start to read, and I'm trying to read it, but always start from the first line
And it takes a several minutes to reach my needed line
Here's my code
'Call F_CMN_GRL_FindObjects(17,15)
strProperty = F_CMN_GRL_GlobalMain(6,17,15,0,"all items" )
strLine = split(strProperty,",")
Print strLine(0)
strValor = split(strLine(0)," ")
Print strValue(1)
numberLine = strValue(1)
Call readFromLine(numberLine)
Function readFromLine(numberLine)
numberLineEnd =numberLine + 7
print numberLine
print numberLineEnd
' Option Explicit
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.OpenTextFile("C:\myFile.txt", 1)
' Dim myArray()
' ReDim myArray(0)
'numberLine it's the number line from I want to start to read and I want to read until numberLineEnd
For i = numberLine to numberLineEnd step 1
strLine = oFile.ReadLine
print strLine
Next
'i = i + 1
'numberLine = numberLine + 1
End Function
I've solved it by doing it like this:
Dim i
Do While (i <= numeroLineaFin)
If (i => numeroLinea) and (i<=numeroLineaFin) Then
' If i <= numeroLineaFin Then
' tmpstr = tmpstr & oFile.readline & VbCrLf
tmpstr = oFile.readline
print tmpstr
' else
' oFile.SkipLine
' End If
else
oFile.SkipLine
end if
i = i + 1
Loop
I have the below code to replace NUL characters in a text file. This code is working as per my requirement for smaller files but the problem is when the file size is increasing it is taking more time. I have a file which consists of more than 200,000 lines consists of 160MB+ size. I have executed my code for this file and I waited for more than 2 hours still the code executing.
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
If (WScript.Arguments.Count > 0) Then
sInfile = WScript.Arguments(0)
Else
WScript.Echo "No filename specified."
WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
sOutfile = WScript.Arguments(1)
Else
sOutfile = sInfile
End If
'Get the text file from cmd file
sData = ""
FinalData = ""
sInfile = WScript.Arguments(1)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Pattern = "\x00.*"
re.Global = True
Set f = oFSO.OpenTextFile(sInfile, 1, False, -1)
Do Until f.AtEndOfStream
sData = Replace(f.ReadLine, vbCrLf, "")
FinalData = FinalData + re.Replace(sData, "") + vbCrLf
Loop
f.Close
Set oOutfile = oFSO.OpenTextFile(sOutfile, 2, True, -1)
oOutfile.Write(FinalData)
oOutfile.Close
Set oOutfile = Nothing
Set oFS = Nothing
WScript.Quit
Is there any way to optimize the code to execute in less interval of time.
EDIT 1:
Updated Code:
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
If (WScript.Arguments.Count > 0) Then
sInfile = WScript.Arguments(0)
Else
WScript.Echo "No filename specified."
WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
sOutfile = WScript.Arguments(1)
Else
sOutfile = sInfile
End If
'Get the text file from cmd file
sData = ""
FinalData = ""
sInfile = WScript.Arguments(1)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Pattern = "\x00.*"
re.Global = True
Set f = oFSO.OpenTextFile(sInfile, 1, False, -1)
Do Until f.AtEndOfStream
sData = Replace(f.ReadAll, vbCrLf, "")
FinalData = FinalData + re.Replace(sData, "") + vbCrLf
Loop
f.Close
Set oOutfile = oFSO.OpenTextFile(sOutfile, 2, True, -1)
oOutfile.Write(FinalData)
oOutfile.Close
Set oOutfile = Nothing
Set oFS = Nothing
WScript.Quit
Do not use ReadAll for large files. Reading large files into memory might exhaust the available RAM on your computer, so that it will come grinding to a halt because it starts swapping.
Also avoid concatenating strings in a loop, because the operation is slow.
Change this:
Set f = oFSO.OpenTextFile(sInfile, 1, False, -1)
Do Until f.AtEndOfStream
sData = Replace(f.ReadLine, vbCrLf, "")
FinalData = FinalData + re.Replace(sData, "") + vbCrLf
Loop
f.Close
Set oOutfile = oFSO.OpenTextFile(sOutfile, 2, True, -1)
oOutfile.Write(FinalData)
oOutfile.Close
to this:
Set f = oFSO.OpenTextFile(sInfile, 1, False, -1)
Set oOutfile = oFSO.OpenTextFile(sOutfile, 2, True, -1)
Do Until f.AtEndOfStream
oOutFile.WriteLine re.Replace(f.ReadLine, "")
Loop
f.Close
oOutfile.Close
Same code with string operations instead of a regular expression replacement:
Set f = oFSO.OpenTextFile(sInfile, 1, False, -1)
Set oOutfile = oFSO.OpenTextFile(sOutfile, 2, True, -1)
Do Until f.AtEndOfStream
line = f.ReadLine
pos = InStr(line, Chr(0))
If pos > 0 Then line = Left(line, pos-1)
oOutFile.WriteLine line
Loop
f.Close
oOutfile.Close
I know it's not up to date, but it might be useful to someone.
I tried another approach that takes about 5 seconds! :)
It seems that scripting engine (wscript) or FileSystemObject has a problem loading 160 MB at a time (by .ReadAll method).
So I tried to load all data (into the Dictionary) line by line via .ReadLine, process it and then save it to the output file at once.
Appendix:
- I added the option to create a test file - if you specify "CreateData" as the second argument:wscript util.vbs "C:\Temp\SampleData.txt" CreateData
- You do not need to remove CR + LF from a string that returns .ReadLine. They are already skipped.
- Sometimes it is good to test .AtEndOfStream before .ReadAll method, because if the file will be empty, the method will cause run-time error.
Dim mode, sInFile, sOutFile
If (WScript.Arguments.Count > 0) Then
sInfile = WScript.Arguments(0)
Else
WScript.Echo "No filename specified."
WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
If StrComp(WScript.Arguments(1), "CreateData", 1) = 0 then
mode = "CreateData"
sOutfile = sInFile
Else
mode = "processing"
sOutfile = WScript.Arguments(1)
End If
Else
mode = "processing"
sOutfile = sInfile
End If
Set oFSO = CreateObject("Scripting.FileSystemObject")
if mode = "CreateData" then
Call CreateDataFile(sInfile, 160) '160 = approx. 160 MB'
Wscript.Quit
end if
Dim dictData, i, sLine, tim
tim = Timer()
'Load data
set dictData = CreateObject("Scripting.Dictionary")
Set f = oFSO.OpenTextFile(sInfile, 1, False, -1)
do while not f.AtEndOfStream
dictData.Add dictData.Count, f.ReadLine()
loop
f.Close
'Process data
for each i in dictData
sLine = dictData(i)
dictData(i) = Replace(sLine, Chr(0), "")
next
'Save processed data
sFinalData = Join(dictData.Items, vbCrLf)
Set oOutfile = oFSO.OpenTextFile(sOutfile, 2, True, -1)
oOutFile.Write sFinalData
oOutfile.Close
'Message
WScript.Echo "Data processed (" & (Timer() - tim) & " sec)"
'-------------------------------------------------------------------------------
sub CreateDataFile(ByVal sFilePath, ByVal nSizeInMB)
'-------------------------------------------------------------------------------
Dim sLine, arrData, i, iMax, sData, tim
rem tim = Timer()
sLine = String(255, "A") & Chr(0) & String(254, "B")
iMax = CLng((nSizeInMB*1024*1024)/(Len(sLine)*2)) 'Unicode chars take 2 bytes
ReDim arrData(iMax)
for i = 0 to iMax
arrData(i) = sLine
next
sData = Join(arrData, vbCrLf)
set oFile = oFSO.CreateTextFile(sFilePath, True, True)
oFile.Write sData
oFile.Close
rem WScript.Echo "Data created (" & (Timer() - tim) & " sec)"
end sub
Starting from an input file (input.txt) i have to find some strings and write them in another file txt (output.txt) This is the input.txt
**********************************************************
* NAME : CONTROLLER
* FUNCTION : NOTHING IMPORTANT
* BEGIN DATE : 31/07/13
* TIME BEGIN : 23.39.17.75
**********************************************************
* DATA INPUT READ : 000000540
**********************************************************
And this is the code:
Const ForReading = 1
Const ForWriting = 2
Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objInputTS 'Text Stream Object
Set objInputTS = objFSO.OpenTextFile("D:\input.txt", ForReading, False)
Dim objOutputTS 'Text Stream Object
Set objOutputTS = objFSO.OpenTextFile("D:\output.txt", ForWriting, True)
Do Until objInputTS.AtEndOfStream
Dim strLine
strLine = objInputTS.ReadLine()
If (Left(strLine, 13) = "BEGIN DATE:") Then objOutputTS.WriteLine(Mid(strLine, 20))
If (Left(strLine, 13) = "TIME BEGIN:") Then objOutputTS.WriteLine(Mid(strLine, 20))
If (Left(strLine, 18) = "DATA INPUT READ:") Then objOutputTS.WriteLine(Mid(strLine, 22))
Loop
objOutputTS.Close()
objInputTS.Close()
But in the output file nothing appears. Any helps? i want this output for esxample
20/05/2013 22/05/2013 21.00.00.00 0000000054
The line
* BEGIN DATE : 31/07/13
does not match the condition
If (Left(strLine, 13) = "BEGIN DATE:")
you have to ac*count* for the "* " prefix too.
To spell it out:
>> s1 = "* BEGIN DATE : 31/07/13"
>> s2 = Left(s, 13)
>> WScript.Echo """" & s2 & """"
>>
"* BEGIN DATE "
>> c1 = "BEGIN DATE:"
>> c2 = "* BEGIN DATE "
>> WScript.Echo 1, CStr(c1 = s2)
>> WScript.Echo 2, CStr(c2 = s2)
>>
1 False
2 True
I have been using the following code to split my text file into two files.My original file only consists of 20 lines which i am trying to split into 2 files.Even when the script runs and i get the message at the end saying that the process is complete i can't see any splitted files at the output location.Please tell me what's the problem in the code;I am new to vbscript so please help me.Thanks in advance :)
Dim Counter
Const InputFile = "C:\Cs.txt"
Const OutputFile = "C:\Users\rmehta\Desktop"
Const RecordSize = 10
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (InputFile, ForReading)
Counter = 0
FileCounter = 0
Set objOutTextFile = Nothing
Do Until objTextFile.AtEndOfStream
if Counter = 0 Or Counter = RecordSize Then
Counter = 0
FileCounter = FileCounter + 1
if Not objOutTextFile is Nothing then objOutTextFile.Close
Set objOutTextFile = objFSO.OpenTextFile( OutputFile & "_" & FileCounter & ".txt", ForWriting, True)
end if
strNextLine = objTextFile.Readline
objOutTextFile.WriteLine(strNextLine)
Counter = Counter + 1
Loop
objTextFile.Close
objOutTextFile.Close
Msgbox "Split process complete"
If you leave out all the spurious fat (the Textstream has a line counter and the first output file can be opened before the loop), you get
Option Explicit
Const cnSize = 10
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sDir : sDir = "..\testdata\18308970"
Dim tsIn : Set tsIn = oFS.OpenTextFile(oFS.BuildPath(sDir, "all.txt"))
Dim nFCnt : nFCnt = 0
Dim tsOut : Set tsOut = oFS.CreateTextFile(oFS.BuildPath(sDir, nFCnt & "-part.txt"))
Do Until tsIn.AtEndOfStream
If 0 = tsIn.Line Mod cnSize Then
tsOut.Close
nFCnt = nFCnt + 1
Set tsOut = oFS.CreateTextFile(oFS.BuildPath(sDir, nFCnt & "-part.txt"))
End If
tsOut.WriteLine tsIn.ReadLine()
Loop
tsIn.Close
tsOut.Close
That this 'works' - if you have the folder, input file, and permissions - is obvious. In your code, the problem
>> Const OutputFile = "C:\Users\rmehta\Desktop"
>> FileCounter = 0
>> WScript.Echo OutputFile & "_" & FileCounter & ".txt"
>>
C:\Users\rmehta\Desktop_0.txt
is is deeply hidden.