Trying to insert delimiter into a text document - vbscript

I am trying to place a vertical bar at certain points in each line of a text file. My code is pretty straightforward I'm pretty sure...but when i try running it nothing happens. I don't even get an error. The file that it is supposed to write to just remains a blank text file
Const ForReading = 1
Const ForWriting = 2
arrCommas = Array(10,14,21,24,39,43,46,61,72,79,82,85,88,91,94,97,101,142,173,189,192,198,205,211,218,222,229,236,240)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\SQL DATA FILES\MBS Stats\mbsedited\mbsfact102013_linebreaks.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
intLength = Len(strLine)
For Each strComma in arrCommas
strLine = Left(strLine, strComma - 1) + "|" + Mid(strLine, strComma, intLength)
Next
strText = strText & strLine & vbCrLf
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("c:\SQL DATA FILES\MBS Stats\mbsfinal\mbsfact102013_delimited.txt", ForWriting)
objFile.Write strText
objFile.Close

Const ForReading = 1
Const ForWriting = 2
arrCommas = Array(10,14,21,24,39,43,46,61,72,79,82,85,88,91,94,97,101,142,173,189,192,198,205,211,218,222,229,236,240)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\SQL DATA FILES\MBS Stats\mbsedited\mbsfact102013_linebreaks.txt", ForReading)
strTextFile = objFile.ReadAll
objFile.Close
aTextFile = Split(strTextFile, vbCRLF)
strText = ""
For Each strLine In aTextFile
intLength = Len(strLine)
For Each strComma in arrCommas
strLine = Left(strLine, strComma - 1) + "|" + Mid(strLine, strComma, intLength)
Next
strText = strText & strLine & vbCrLf
Next
Set objFile = objFSO.OpenTextFile("c:\SQL DATA FILES\MBS Stats\mbsfinal\mbsfact102013_delimited.txt", ForWriting)
objFile.Write strText
objFile.Close

Related

Delete preceding lines from text file if line has value

i have a text file with numbers and i must delete the lines before a given number.
45048
67113.88
74484
597.6
1945.65
8714.5
32085.9
741.12
1721.39
35266.7
8260.71
23635.8
40487.5
40702.18
29544.74
110000
810000
3161000
29201.91
33000
So i must delete the lines before number 110000 . Or in another day i must delete the lines before 1721.39. I don't know how to make the deletion before the user input value. or just delete first 25 line if user prompt 25...
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " ", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "forward", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, ".", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " ", "")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = Trim(strLine)
If Len(strLine) > 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\db\a.txt", ForWriting)
objFile.Write strNewContents
objFile.Close
Try this for delete number of lines, And try yourself others.
Const FOR_READING = 1
Const FOR_WRITING = 2
strFileName = "C:\txt.txt"
NumberOfLinesToBeDelete=inputbox("How many lines want to delete from beginning","Delete","")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName, FOR_READING)
strContents = objTS.ReadAll
objTS.Close
arrLines = Split(strContents, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
For i=0 To UBound(arrLines)
If i > (NumberOfLinesToBeDelete - 1) Then
objTS.WriteLine arrLines(i)
End If
Next

Read text and match all dates in text file otherwise write value to error.txt file

The below .TXT file is read into a VBS FileSystemObject. I am trying to search for all dates that match otherwise I need to put them in a "error.txt" file. However, when I run my code below it is always placing the matches in the error.txt file and not skipping the matching dates.
Why are the dates not matching?
INPUT:
"LIRRR 1M",.412900,02/08/2016
"LIRRR 3M",.222700,02/08/2016
"LIRRR 6M",.333200,02/08/2016
"LIRRR12M",1.1333300,02/08/2016
"FEDFRRRR",.333000,02/08/2016
"CCC 1YR",.550330,02/08/2016
"5YRCMT",1.2503300,02/08/2016
"10YRCMT",1.860000,02/08/2016
Here is the code that I have written:
On error resume next
Const ForReading = 1
Dim strSearchFor
Dim MyDate, MyWeekDay
MyDate = Date ' Assign a date.
MyWeekDay = Weekday(MyDate)
If MyWeekDay = 2 then
strSearchFor =Right("0" & DatePart("m",Date), 2)&"/"&Right("0" & DatePart("d",Date-3), 2)&"/"&DatePart("yyyy",Date)
Else
strSearchFor =Right("0" & DatePart("m",Date), 2)&"/"&Right("0" & DatePart("d",Date-1), 2)&"/"&DatePart("yyyy",Date)
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Users\Desktop\index.txt", ForReading)
do until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine()
If InStr(strLine, strSearchFor) <> 0 then
Set objFile = objFSO.CreateTextFile("C:\Users\Desktop\pass.txt")
objFile.Write "date is match"& vbCrLf
Else
Set objFile = objFSO.CreateTextFile("C:\Users\Desktop\error.txt")
objFile.Write "date is not match"& vbCrLf
End If
loop
objTextFile.Close
Why not use RegEx to get the portion of the string that appears to be the date and use the IsDate Function to validate it?
Option Explicit
Dim arrLines,i
arrLines = ReadFile("./input.txt","byline")
For i=LBound(arrLines) to UBound(arrLines)
wscript.echo FormatOutput(arrLines(i))
Next
'*********************************************
Function FormatOutput(s)
Dim re, match
Set re = New RegExp
re.Pattern = "[\d]+[\/-][\d]+[\/-][\d]+"
re.Global = True
For Each match In re.Execute(s)
if IsDate(match.value) then
FormatOutput = CDate(match.value)
Exit For
end if
Next
Set re = Nothing
End Function
'*********************************************
Function ReadFile(path,mode)
Const ForReading = 1
Dim objFSO,objFile,i,strLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(path,ForReading)
If mode = "byline" then
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
strLine = objFile.ReadLine
strLine = Trim(strLine)
If Len(strLine) > 0 Then
arrFileLines(i) = strLine
i = i + 1
ReadFile = arrFileLines
End If
Loop
objFile.Close
End If
If mode = "all" then
ReadFile = objFile.ReadAll
objFile.Close
End If
End Function
'*****************************************************************

Facing issue in replace string in a file using vbscript

I have a text file which contains below data
mkdir language
Here is my vbscript which replace language string in text file
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "language", "english,french,spanish")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
I am getting output mkdir english,french,spanish but what i want output in text file as shown below
mkdir english
mkdir french
mkdir spanish
How to achieve this please help
This will work too...
Const ForReading = 1
Const ForWriting = 2
Dim langs
langs = Array("english","french","spanish")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Text.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
tempTxt = ""
strNewText = ""
For Each i in langs
tempTxt = "mkdir " + i + vbCrLf
strNewText = strNewText + tempTxt
Next
strNewText = Replace(strText, "mkdir language", strNewText)
Set objFile = objFSO.OpenTextFile("Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Here try this variation:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
For Each strLanguage In Array("english", "french", "spanish")
objFile.WriteLine Replace(strText, "language", strLanguage)
Next
objFile.Close
Use Join to build a string from an array:
>> c = "mkdir "
>> l = Split("english,french,spanish", ",")
>> WScript.Echo c & Join(l, vbCrLf & c)
>>
mkdir english
mkdir french
mkdir spanish
Of course, c could come from a file.
(But I suspect your real world problem isn't shown in your simplified sample)

VB Scripting error.. Object Required

This is my first day with VB scripting. I found following code to search and replace text in a text file but when I run that using the following command
cscript replace.vbs "test.txt" "Jim" "James"
I get an error saying
replace.vbs(6, 1) Microsoft VBScript runtime error: Object required: 'Scripting'
Here is the code
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject(Scripting.FileSystemObject)
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText 'WriteLine adds extra CRLF
objFile.Close
Use
Set objFSO = CreateObject("Scripting.FileSystemObject")
(mark the quotes, CreateObject() needs a string)

vbscript replace text - one works two breaks the whole thing

I want this script to replace two values in the same text file instead of one. However, if I uncomment line 12, it breaks the script. Do I have to make this into a loop, or can I do multiple replaces?
Sub ReplaceTxt()
'Writes values we got earlier to our unattend file '
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "***COMPNAME***", strCompname)
' strNewText = Replace(strText, "***Winkey***", strPoductkey) '
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strNewText
objFile.Close
End Sub
I think you will want to do the second replace on the string returned by the first one:
strNewText = Replace(strText, "***COMPNAME***", strCompname)
strNewText = Replace(strNewText , "***Winkey***", strPoductkey)
Otherwise you will lose the first replace, and only the second one will appear in the result.
Try this:
Sub ReplaceTxt() 'Writes values we got earlier to our unattend file'
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strText = Replace(strText, "COMPNAME", strCompname)
strText = Replace(strText, "Winkey", strPoductkey)
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strText
objFile.Close
End Sub
By doing it the way you were, you were using the original, unused text twice, overwriting the first replace when you did the second.
I'm sure my if statement is ugly to the real coders out there, but here's how I got it to work
Sub ReplaceTxt() 'Writes values we got earlier to our unattend file'
Const ForReading = 1
Const ForWriting = 2
counter = 1
For Each searchterm In Array("COMPNAME", "Winkey")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
If counter < 2 Then
strText = Replace(strText, searchterm, strCompname)
Else
strText = Replace(strText, searchterm, strProductKey)
End If
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strText
objFile.Close
counter = counter + 1
Next
End Sub
Well.. Thats easy.. Bueno ni tanto.. I was trying to do this some time. and i found the answer:
Sub ReplaceTxt()
'Writes values we got earlier to our unattend file '
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strSIFpath, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "***COMPNAME***", strCompname)
strNewText2 = Replace(strNewText, "***Winkey***", strPoductkey) '
Set objFile = objFSO.OpenTextFile("C:\$WIN_NT$.~BT\winnt.sif", ForWriting)
objFile.WriteLine strNewText2
objFile.Close
End Sub
Tanks, Gracias. José Villa From Culiacán, Sinaloa Mexico

Resources