Delete preceding lines from text file if line has value - vbscript

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

Related

Prepend text to Text file using VBScript

I have this script to allow me to insert text into a text file but I need it to be at the start of the text file. This script currently adds this to the end of the .txt file.
And I am new to trying these things out myself
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "c:\scripts"
strFile = "\csv.txt"
strText = "sep=|"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If
set objFile = nothing
set objFolder = nothing
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
objTextFile.WriteLine(strText)
objTextFile.Close
WScript.Quit
You can read the file's content into a string, add your string in front of that and write everything back to the same file.
Dim sFileText
Dim sPrependText
Const ForReading = 1, ForWriting = 2
' Open file For Reading and Read All content to a variable
Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForReading, True)
sFileText = objTextFile.ReadAll
objTextFile.Close
' Prepend text in front of file's content
sPrependText = "sep=|"
sFileText = sPrependText & sFileText
' Open file For Writing and write text variable
Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForWriting, True)
objTextFile.Write sFileText
objTextFile.Close

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)

Trying to insert delimiter into a text document

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

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