Write a script to read and write data from file?
Dim fso,fol,fls
Set fso =
createObject("scripting.fileSystemObject")
Set File =
fso.OpenTextFile("D:\KT Sessions\Testing.txt")
BeforeChange =
""
AfterChange =
""
Const Readmode
=1,WriteMode=2, AppendMode=8
FileName="D:\KT
Sessions\Testing.txt"
'To Write in the File
Set File =
fso.OpenTextFile(FileName, WriteMode, True)
File.WriteLine
"This is VB File Stream Script."
File.WriteBLankLines(1)
' For Blank Line
File.WriteLine "To
Understand Read/write from File"
File.Close
'To Read from the File
Set File =
fso.OpenTextFile(FileName, ReadMode)
BeforeChange =
BeforeChange & File.ReadAll
File.Close
'To Append in the File
Set File = fso.OpenTextFile(FileName,
AppendMode, True)
File.WriteLine
"This is Append Data." & Now
File.Close
Set File =
fso.OpenTextFile(FileName, ReadMode)
'Do until
File.AtEndOfStream = True
'AfterChange =
AfterChange & File.ReadLine
'Loop
AfterChange =
AfterChange & File.ReadAll
File.Close
MsgBox "Before
change: " & vbcrlf & BeforeChange & vbcrlf & vbcrlf
& "After Append: " & vbcrlf & AfterChange
|
If we have 40 files in a folder, how to find the
latest updated file in the folder?
Dim fso,Folders,Files,Temp
Set fso=
createObject("scripting.FileSystemObject")
set Folders =
fso.getFolder("D:\Software Testing\VB Script")
set files= Folders.Files
dim FileName :
FileName=""
For each x in files
For each y in Files
if x.DateLastModified > y.DateLastModified then
FileName = x.Name
Else
FileName = y.Name
End IF
Next
Exit For
Next
MsgBox "Last
Updated File Name = " &Filename
|
Script to find number of hours in given date?
Dim CurDateVar,CurHour
CurDateVar = #2/29/2016
1:02:03 PM#
CurHour=
HOur(CurDateVar)
'CurHour= HOur(NOW)
MsgBox "Hour from
Date = " & CurHour
|
There is an array with 1-100 randomly arranged,
one integer is missing, find it efficiently.
Dim max,min,FNumber
Dim Arr(99)
max=100
min=1
Randomize
isFound = "Not
Found"
For i =0 to 99
Arr (i)= Int((max-min+1)*Rnd+min)
Next
Fnumber= int
(inputbox(" Enter the number you want to search from 1-100 from Random
number"))
For i =0 to 99
If FNumber = Arr (i) then
isFound="Found"
exit for
End IF
Next
MsgBox "Enter
Number is " & isFound
|
Script to get the file name from the folder.
Dim fso,fol,fls
Set fso =
createObject("scripting.fileSystemObject")
set
fol=fso.getFolder("D:\KT Sessions")
Set fls= fol.Files
Result =""
For each i in fls
Result = Result &
i.Name & VBCRLF
'Result = Result &
" Type = " & i.type & VBCRLF
'Result = Result &
" Size = " & i.size & VBCRLF
'Result = Result &
" Attributes = " & i.Attributes & VBCRLF
'Result = Result &
" DateLastModified = " & i.DateLastModified & VBCRLF &
VBCRLF
Next
MsgBox Result
|