how to change word in text file by VB script (like sed in unix)
You can use the FileSystemObject Object. Some notes:
Set fs = CreateObject("Scripting.FileSystemObject")
sf = "C:\Docs\In.txt"
Set f = fs.OpenTextFile(sf, 1) ''1=for reading
s = f.ReadAll
s = Replace(s, "Bird", "Cat")
f.Close
Set f = fs.OpenTextFile(sf, 2) ''2=ForWriting
f.Write s
f.Close
Following steps: (when tackling a computing problem, divide and conquer!)
Open the text file
Save file contents to string variables
Close the text file!
Search variable for the word
Replace the word(s)
Save the variable as a text file
overwriting old one
With the help of Google, you should be able to search and discover how to achieve all of the above points.
Related
I'm trying to move one or more files from one directory to another directory using a wildcard:
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("Z:\somepath\somefile_*_A.xlsm") Then
filesys.MoveFile "Z:\somepath\somefile_*_A.xlsm", "Z:\destpath\"
End If
And it doesn't work...
Notes:
There are other files in these directories that I do not want to move. I want to move all of the files that are returned using the wildcard. Must be using VBS.
Links:
VBscript to move files from one directory to another
https://msdn.microsoft.com/en-us/library/2wcf3ba6%28v=vs.84%29.aspx
Function ShowFolderList(folderspec)
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & "<BR>"
Next
ShowFolderList = s
End Function
This is from Help. There are no wildcards in FSO. You have to do it yourself. So test if f1.name meets your requirements then copy that file.
You can't pass a wildcard to findfiles imo.
You could do the check yourself
For Each file In filessys.GetFolder("Z:\somepath").Files
If( <do your checks on file.Name, might be a regex or a simple string compare>) Then
filesys.MoveFile file, "Z:\destpath\"
End If
Next
Depending on how much you know about the format of the file it might be enough to just check the rightmost characters if they are always "_A.xlsm" or you can use a regular expression
(I'm quite new to vb, but familiar with vba).
I'm trying to find out how to read a text file from bottom to top as:
the text file is updated 'x' period of time; lines being added,
and I need to find the last entry "line" that contains the contains the text "System Pass". However between the last line of the file and the last line that contains the needed string are a lot unnecessary "dump" lines.
With excel I used to import the text file and loop through the rows starting at the bottom and to determine if I had the correct string line with the inStr function. But this doesn't work, or I just simply don't know how to convert the code to vb.
Help is greatly appreciated.
Thanks
Philippe
Here is an example of how to read a txt file into an array and poll through it from bottom to top using instr to search for text:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\test.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
MyArray = Split(strText, vbCrLf)
For X = Ubound(MyArray) to lbound(MyArray) step -1
If instr(1,MyArray(X),"T") > 0 then
Wscript.Echo MyArray(X)
End if
Next
My Test file contained this:
hello
World
This
Is
Text
The VBS file popped up 2 message boxes, one with "Text" and one with "This"
You can DIM them if you want:
Dim objFSO
Dim objTextFile
Dim X
Dim MyArray
But VBS doesn't support types so don't try Dim X as Long or anything like that.
Hope that helps
I recommend import the data with Excel, you can use NPOI library, with NPOI you can easily read Excel files in .NET.
EDIT:
Read txt files with VB: https://msdn.microsoft.com/en-us/library/yw67h925.aspx
I am trying to split a tab delemeted file into pieces with similar header. I have my logic in place. However, I am trying to read input file line by line and writing it to another file. When I open the outptut file it doesn't contain any data. Here is my code.
Can some one help me whats going wrong here?
Note: The below code doesn't contain actual logic of splitting the file
Wscript.Echo "Begin"
InputFile = "test.txt"
Set InputFSO = CreateObject("Scripting.FileSystemObject")
Set InputFileObject = InputFSO.OpenTextFile(InputFile)
HeaderLine = InputFileObject.ReadLine
Do While InputFileObject.AtEndOfStream <> True
strTemp = InputFileObject.SkipLine
Loop
TotalLines = InputFileObject.Line-1
Set OutputFSO = CreateObject("Scripting.FileSystemObject")
Set OutputFileObject = OutputFSO.CreateTextFile("out.txt")
#Code for reading line by line and writing it to another file
Do While not InputFileObject.AtEndOfStream
line = InputFileObject.Readline
OutputFileObject.WriteLine(line)
Loop
Set InputFileObject = Nothing
Set OutputFileObject = Nothing
Wscript.Echo "Completed"
You're looping through the entire input file in the first Do loop when you are trying to get the line count. So the InputFileObject is already "AtEndOfStream" when you hit the second Do loop. Therefore, none of the code inside the second loop is executing.
Consider eliminating the first Do loop and count the lines in the file at the end of the other loop (unless in your real program the logic in the first loop is required?).
The alternative is to close the input file and reopen it. The problem in this case is that you'll wind up reading the file twice.
'Close and reopen the file from the top...
InputFileObject.Close
Set InputFileObject = InputFSO.OpenTextFile(InputFile)
I was googling around but didn't find the right answer, perhaps people from here are willingly and able to help me.
I'm very new to VBS or WSH and I like to have a solution for this problem:
I'm searching for textstrings within a file without a line break (only one line). The textstrings I'm looking for start always with the same content "jpgline" and ends with the three letters "qbm". How can we extract each sentence (the strings are always 64 chars long) containg "jpgline....qbm" into a separate file.
I'm looking for a solution in Visual Basic Script as I use Windows 7.
Thanks in advance
M i k e
Use a regular expression:
Set re = New RegExp
re.Pattern = "^jpgline.*qbm$"
re.IgnoreCase = True
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile = fso.OpenTextFile("C:\path\to\input.txt")
Set outFile = fso.OpenTextFile("C:\path\to\output.txt", 2, True)
Do Until inFile.AtEndOfStream
line = inFile.ReadLine
If re.Test(line) Then outFile.WriteLine line
Loop
inFile.Close
outFile.Close
As your input file has no lines, use .ReadAll() to load its entire content into a string variable. Apply a RegExp to get all parts (Matches) defined by the pattern "jpgline.{N}qbm" where N is either 64 or 64 - the length of the pre/suffix. Ansgar has shown how to open and write to the output file.
Use the RegExp Docs to learn about .Execute and how to loop over the resulting match collection. The docs will tell you about .Test too.
Hi I have a text file that I would like to assign to an array and then assign each item in the array to a custom defined variable. When I open the file in notepad, it seems as if the data is on one line and there's about 10 tabs worth of space until the next piece of information.
I use the following code to successfully view the information in a msgbox as MyArray(i).
In my code example, all the information is listed in MyArray(0) and MyArray(1) gives me an error of subscript out of range. The information in the text file seems to appear as if it were delimited by vbCrLf but that does not work either...
Is there a way to trim the spaces from MyArray(0) and then re-assign the individual data to a new array? Here's what the first two pieces of information look like from my file:
967042
144890
Public Function ReadTextFile()
Dim TextFileData As String, myArray() As String, i As Long
Dim strCustomVariable1 As String
Dim strCustomVariable2 As String
'~~> Open file as binary
Open "C:\textfile\DATA-SND" For Binary As #1
'~~> Read entire file's data in one go
TextFileData = Space$(LOF(1))
Get #1, , TextFileData
'~~> Close File
Close #1
'~~> Split the data in seperate lines
myArray() = Split(TextFileData, vbCrLf)
For i = 0 To UBound(myArray())
MsgBox myArray(i)
Next
End Function
Under normal circumstances, I'd suggest that you use Line Input instead:
Open "C:\textfile\DATA-SND" For Input As #1
Do Until EOF(1)
Redim Preserve myArray(i)
Line Input #1, myArray(i)
i = i + 1&
Loop
Close #1
However, you're likely dealing with different end-line characters. You can use your existing code and just change it to use vbCr or vbLf instead of vbCrLf. My method assumes that your end-line characters are vbCrLf.
So for UNIX files:
myArray() = Split(TextFileData, vbLf)
And for old Mac files:
myArray() = Split(TextFileData, vbCr)