I have an HTML page with one or more newlines after </html>. My VBScript file is able to find the replace the newlines with emptiness. But, it looks like OpenTextFile is putting a newline at the end again. Help!
'Pulled this from the InterWebs
Const ForReading = 1 Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("a.html", ForReading)
strText = objFile.ReadAll
'Wscript.Echo strText
objFile.Close
strNewText = Replace(strText, "</html>" & vbCrlf, "</html>")
Set objFile = objFSO.OpenTextFile("a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Instead of objFile.WriteLine strNewText use objFile.Write strNewText. This will write the file without a newline at the end.
BTW, another way of removing the newline(s) from after your </html> tag would be strNewText = Trim(strText) instead of using Replace()
This may help:
The TextStream object has the following important methods for writing to text files:
Write(string) - Writes a string to the open text file.
WriteLine(string) - Writes a string to the text file and finishes it off with the new line character.
WriteBlankLines(lines) - Writes a specified number of new line characters.
If you do not want a newline at the end, use
objFile.Write strNewText
instead of
objFile.WriteLine strNewText
Your code is mostly correct. It's not OpenTextFile that's adding a newline, it's WriteLine. If you replace that with Write, it will work as you expect.
Related
I have some XML files in a folder \\demo.US\Modified\. The files in the folder are:
USA.xml
Canada.xml
Mexico.xml
The code below is changing the encoding from UTF-8 to windows-1252 and is creating a modified file mod.xml.
This mod.xml file have data from all three XML files concatenated.
I need help so I can save files separately.
If value of objFile.Name is USA.xml then it should save modified file name as USA_mod.xml. the output for \\demo.US\Modified\ folder after execution is complete should have mod files in it as below.
USA.xml
Canada.xml
Mexico.xml
USA_mod.xml
Canada_mod.xml
Mexico_mod.xml
The code I used is as follows.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "\\demo.US\Modified\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile In colFiles
WScript.Echo objFile.Name
Set objFile = objFSO.OpenTextFile(objStartFolder & objFile.Name, 1)
Set outFile = objFSO.OpenTextFile(objStartFolder & "mod.xml", 2, True)
Do Until objFile.AtEndOfStream
strContent = strContent & objFile.ReadLine
Loop
MsgBox strContent
strContent = Replace(strContent, "encoding=""UTF-8""", "encoding=""windows-1252""")
outFile.WriteLine strContent
outFile.Close
objFile.Close
Next
As others have already pointed out, you shouldn't do what you're attempting to do here, because it is very likely to create more problems down the road. Find the cause of the issue and fix that instead of trying to handle symptoms. You have been warned.
With that said, the reason why the content of all input files is written to the same output file is because you always specify the same output file. That file should contain only the content of the last input file, though, because you open the file for writing (thus erasing previous content) rather than for appending.
Replace these lines:
Set objFile = objFSO.OpenTextFile(objStartFolder & objFile.Name, 1)
Set outFile = objFSO.OpenTextFile(objStartFolder & "mod.xml", 2, True)
with this:
Set inFile = objFile.OpenAsTextStream
outFilename = objFSO.BuildPath(objStartFolder, objFSO.GetBaseName(objFile) & "_mod.xml")
Set outFile = objFSO.OpenTextFile(outFilename, 2, True)
and also replace the other occurrences of objFile after that with inFile (always avoid changing the value of a loop variable), and the code should do what you expect it to do. But again, be warned that the output may not be valid XML.
I managed to made it working, below is the code I used
Dim objFSO, filePath, objFile, colFiles, s , FName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set filePath = objFSO.GetFolder("\\demo.US\Modified\")
Set colFiles = filePath.Files
For Each FName in colFiles
set objFile = objFSO.OpenTextFile(FName.Path,1)
set outFile = objFSO.OpenTextFile(LEFT(FName.Path,instr(FName.Path,".xml")-1) &"_mod.xml",2,True)
do until objFile.AtEndOfStream
strContent=objFile.ReadLine
Loop
strContent = Replace(strContent, "encoding=""UTF-8""", "encoding=""windows-1252""")
outFile.WriteLine strContent
outFile.Close
objFile.Close
Next
This question already has answers here:
Adding quotes to a string in VBScript
(8 answers)
Closed 5 years ago.
The script below works fine. It searches a specified location for .exe files and outputs a file which contains the location of the files.
All of that works great. Except I'd like the output file lines to all be enclosed in " ", how to do this?
Option Explicit 'force all variables to be declared
On Error Resume Next
Const ForWriting = 2
Dim objFSO
Set objFSO = CreateObject(Scripting.FileSystemObject)
Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("output_path", ForWriting, True)
Recurse objFSO.GetFolder("location_of_files")
objTS.Close()
Sub Recurse(objFolder)
Dim objFile, objSubFolder
For Each objFile In objFolder.Files
If LCase(objFSO.GetExtensionName(objFile.Name)) = "exe" Then
objTS.WriteLine(objfile.Path)
End If
Next
For Each objSubFolder In objFolder.SubFolders
Recurse objSubFolder
Next
End Sub
Change this line:
objTS.WriteLine(objfile.Path)
To this:
objTS.WriteLine("""" & objfile.Path & """")
It looks weird because you have to double the embedded quotation marks.
Or instead you can use chr(34), like this:
objTS.WriteLine(chr(34) & objfile.Path & chr(34))
The chr() function converts the specified character code to a character.
I have a VBScript - if I run it I do not want it repeating the words it is replacing. Is there an If statement I can add to stop it doing this?
Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim objFSO,objFile,strText,strNewText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "<Jim","<!--<Jim")
'<pseudocode>
IF "<!--<Jim" = EXIST THEN DO NOTHING
END IF
'</pseudocode>
Set objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt",ForWriting)
objFile.WriteLine strNewText
objFile.Close
set objFSO = Nothing
set objFile =
Replace <Jim with <!--Jim instead of <!--<Jim to avoid repeated replacements (i.e. make sure that your search string isn't a substring of your replacement string).
You can use InStr() to check whether strText contains "Jim" or "James" and do or don't replace or save according to what you really want to achieve.
The docs for Replace() explain how to restrict the number of substitutions - if that's what your "execption"/"repeating" is hinting at.
Demo:
>> WScript.Echo Replace("AA", "A", "a", 1, 1, vbTextCompare)
>>
aA
>>
I am in need of a vb script that will be used as a macro in excel.
I need the rows with data that have spaces to have the spaces removed. I know how to create a formula one cell at a time but I am clueless on how to do this on a large scale with a looping script. Any help would be appreciated.
Don't know that much about the micros, but in vb-script you can use following code :-
Option Explicit
Dim objFso,strFileName,objFile
Set objFso = CreateObject("Scripting.FileSystemObject")
strFileName="C:\Documents and Settings\amolc\Desktop\test.txt" ''Path of text file
Set objFile = objFso.OpenTextFile(strFileName,1)
strText = objFile.ReadAll
objFile.Close
strText = Replace(strText," ","",vbTextCompare) ''Replace function to remove space
Set objFile = objFso.OpenTextFile(strFileName,2)
objFile.Write (strText)
objFile.Close
Try this code:
Columns("F:F").Select
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
I try to replace character space (" ") with none ("")
Start of that I'm not that good with VBS but trying to learn.
Right now I have some difficulties with writing replace to a text file.
What I want to do is to search for the text "VGML", when this is found check on same row if there is a "STML" if so, this should replaced with " " to not mess up the positions in the file. And finally, if "VGML" is found without the "STML", the "VGML" should be removed.
I got so far when using echo I can see that the code does what I want, but writing to file mostly get me a empty file where the code removed everything.
Could anyone put me in the correct direction?
Here is the code:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Script\RemoveSTML\testFiles\test.txt", ForReading)
strEMTY = " "
strSTML = "STML"
strVGML = "VGML"
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,strVGML)>0 Then
If Instr(strLine,strSTML)>0 Then
strLine = Replace(strLine, strSTML, strEMTY)
wscript.Echo strLine
Else
strLine = Replace(strLine, strVGML, strEMTY)
wscript.Echo strLine
End If
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\Script\RemoveSTML\testFiles\test.txt", ForWriting)
objFile.Write strLine
objFile.Close
Wscript.Echo "done"
Thanks for your help! For giving me a kick in the correct direction!
In the loop, you read each line of the file and you write it into the variable strLine.
After that, you replace "SMTL"/"VGML", but after that, you're doing nothing with the content of strLine - you just replace it with the next line from the source file in the next loop.
Only at the end, you write strLine to the file once - but in that moment, strLine contains only the last line from the source file.
Solution: you can either write into the destination file inside the Do...Loop (but I'm not familiar with VBScript, so I don't know if/how to do this) or you can use a second string variable where you append strLine inside the loop but after the replacing...like this:
strFinal = strFinal & strLine
At the end, you write strFinal to the file, not strLine.