VBS replace and write - vbscript

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.

Related

Saving file into another file using VBScript after modification

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

VBscript using variable from text file error

Good day all.
What I am trying to do is ready a value from a text file and using it as a variable within the code. The text file holds a path directory, that i want to use to create another folder in with a datestamp.
This is what it looks like:
Set FSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objTextFile = FSO.OpenTextFile("C:\MyFolder\Settings.txt", ForReading)
For i = 0 to 0
objTextFile.ReadLine
Next
New_Path = objTextFile.ReadLine
FSO.CreateFolder New_Path & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2)
objTextFile.Close
I get an error that says Bad file name or number
But when I use Wscript.Echo New_Path it shows me exactly the right path that I need. Why is it not using it if it comes back correctly?
The first line has comments so I read the second line that contains the path that looks like this: "C:\NewFolder\"
In the text file i took away the "" between the Directory and it worked

VBScript Renaming File Code Issue

I wrote a simple vbscript to rename files in a particular folder. Specifically to remove particular content from the filname.
The Script I wrote (listed below) runs fine but the highlighted part (second IF-THEN statement) doesn't run. I can't figure out whats wrong with the code. I plan to add more IF-THEN statement to remove particular content from file names.
I'm a novice at this so please be patient with me. Can anyone help?
Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="C:\Users\Admin2\Downloads\Compressed"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
strFileName = strFile.Name
If InStr(strFileName,"(2014)") > 0 Then
strNewFileName = Replace(strFileName,"(2014)","")
strFile.Name = strNewFileName
End If
**If InStr(strFileName,"(digital)") > 0 Then
strNewFileName = Replace(strFileName,"(digital)","")
strFile.Name = strNewFileName
End If**
Next
Type prefix fraud detected:
For Each strFile In objFolder.Files
"strFile" should be "objFile". Dangerous extra variable in:
strFileName = strFile.Name
The variable "strFileName" will get stale if you change "objFile.Name". Use a variable to hold the new/desired name instead.
strNewFileName = objFile.Name
Renaming the file twice will loose changes on the way. Modify "strNewFileName" (in steps or all at once:
strNewFileName = Replace(Replace(strNewFileName, "(2014)", ""), "(digital)", "")
; you don't really need the If guard, because Replace won't change strings that don't contain the target).
Check for .FileExists(strNewFileName) before you do the rename.
Can you prove that there are file names that contain "(digita1)" <-- mark the digit 1) exactly? Lower vs. upper case? A nasty blank?
I hope the following code helps
Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="pathtofolder"
Set objFolder = objFS.GetFolder(strFolder)
For Each objFile In objFolder.Files
ObjFileName = ObjFile.Name
NewFileName = Replace(Replace(ObjFileName,"(2014)",""),"(digital)","")
Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
If fileSystemObject.FileExists(NewFileName) Then
Else
ObjFile.Name = Trim(NewFileName)
End If
Next

vbscript find last line of a text file that does not contain the text "Ack"

I have a log file that I would like to read with vbscript, the file has two lines for each command, e.g. the command itself and following that on the next like an Acknowledgement of the previous line.
So I get:
xxx,xxx,xxx, ,Blah - some more blah
xxx,xxx,xxx, ,Blah Ack
I want to go right to the bottom of the log file, and then read back up line by lane until I get to the first line that does not have an "Ack" in it, then put that line in a variable and write it out.
I can get most of it working, I just can figure out how to read back up line by line until I see a line that doesnt have an Ack?
Does anyone have any ideas?
This is what I'm sing to get to the bottom of the file:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Y:\Selcall\logs\DEC-20130929.LOG", ForReading)
Do Until objFile.AtEndOfStream
strNextLine = objFile.ReadLine
If Len(strNextLine) > 0 Then
strLine = strNextLine
End If
Loop
objFile.Close
Wscript.Echo strLine
Wscript.Echo Right(strLine, Len(strLine) - 35)
Technically you can't read a file back to front, so you must read the file from the beginning and remember the last line that doesn't contain the keyword Ack:
Do Until objFile.AtEndOfStream
strNextLine = objFile.ReadLine
If InStr(strNextLine, "Ack") = 0 Then strLine = strNextLine
Loop
After the loop terminates, the variable strLine contains the line you're looking for.
You can't move/read backwards with the FSO's TextStream object. You must read the file from top to bottom and store/overwrite each candidate line(s); after the .Close, the storage will contain the last/desired candidate.

VBScript remove newline

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.

Categories

Resources