C++/VB - Reformat text with a script
Asked By Bill
04-Feb-10 06:06 PM
Hi,
I was wondering if anyone could help me out -
I am trying to reformat the following data from a file called data.txt:
OR1387L3 Jan 20 2015 6:59PM
OR1363L3 Feb 4 2011 6:00PM
to remove the L3, drop the time, and reformat the date like this-
OR1387 01202015
OR1363 02042011
Any suggested are appreciated.
Thanks!
Scripting.FileSystemObject
(1)
VB
(1)
OutF.WriteLine
(1)
StrFileNumber
(1)
CreateObject
(1)
StrToChange
(1)
ReadLine
(1)
StrMonth
(1)
LikeToCode replied to Bill

You can use the FileSystemObject and the ReadLine Method to read and append
text files. More info can he found @
http://msdn.microsoft.com/en-us/library/hww8txat(VS.85).aspx
As for reformatting your text, once you have charged a variable with the
text to change you can use the Split function to break the string into a zero
based array.
The default delimiter of the Split function is the space character. If you
would like to see what elements have been split from the string the following
code will will show you. Remember this is a zero based array so in the
following example strSplit(0) = "Some" and strSplit(1) = "String"
strSplit = Split("Some String")
For i = 0 To UBound(strSplit)
MsgBox strSplit(i)
Next
Using this method we will reformat your string.
strToChange = "OR1387L3 Jan 20 2015 6:59PM"
'Use Split function to split the string into a zero based array.
strSplit = Split(strToChange)
strFileNumber = Left(strSplit(0),6) 'FileNumber
strDay = strSplit(2) 'Day
strYear = strSplit(3) 'Year
Select Case strSplit(1) 'Month
Case "Jan"
strMonth = "01"
Case "Feb"
strMonth = "02"
End Select
strNew = strFileNumber & " " & strMonth & strDay & strYear
MsgBox strNew
For Reference:
Here is the array of strSplit
strSplit(0) = "OR1387L3"
strSplit(1) = "Jan"
strSplit(2) = "20"
strSplit(3) = "2015"
strSplit(4) = " " this is the extra space between "2015" and "6:59PM"
strSplit(5) = "6:59PM"
Pegasus [MVP] replied to Bill
When dealing with dates & times then you could make use of the various
date/time conversion functions that are built into VB Script, e.g. like so:
sLine = "OR1363L3 Feb 4 2011 6:00PM"
Do While InStr(sLine, " ") > 0
Line = Replace(sLine, " ", " ")
Loop
aLine = Split(sLine)
sKey = Replace(aLine(0), "L3", "", 1, - 1, 1) & " "
dDate = CDate(aLine(1) & " " & aLine(2) & " " & aLine(3))
WScript.Echo sKey & Pad(Month(dDate)) & Pad(Day(dDate)) & Year(dDate)
Function Pad (n)
Pad = Right("0" & n, 2)
End Function
You now need to add the code that reads one line after the other from your
log file, then writes it to your output file. The File System Object has the
necessary methods for this. You will find detailed examples in the
downloadable help file script56.chm.
Bill replied to LikeToCode
This is perfect -
The only issue I ran into is that it seems to have a problem with OR1363L3
Feb 4 2011 6:00PM - it returns OR1363 024 instad of 02022011. Guessing it
does not like the single date digit.
Any ideas on how I can work around this?
Thanks again for your help!
Bill replied to Pegasus [MVP]
I tried running this and it gets stuck in a loop somewhere (and pegs out my
processor). It would definetly solve my issue with the single Feb day digit
though.
How can I see what is causing it to loop? All I did was past this code into a
a VBS file and run it to see what it returnned.
Thanks again for your help!
LikeToCode replied to Bill
I did not test the single date sorry!! However Richard's script will work
fine. In the Do While loop change
from:
Line = Replace(sLine, " ", " ")
To:
sLine = Replace(sLine, " ", " ")
Happy coding!
Pegasus [MVP] replied to Bill
Silly typo in my code, which LikeToCode picked up. As he said, the code
should look like so:
sLine = "OR1363L3 Feb 4 2011 6:00PM"
Do While InStr(sLine, " ") > 0
sLine = Replace(sLine, " ", " ")
Loop
aLine = Split(sLine)
sKey = Replace(aLine(0), "L3", "", 1, - 1, 1) & " "
dDate = CDate(aLine(1) & " " & aLine(2) & " " & aLine(3))
WScript.Echo sKey & Pad(Month(dDate)) & Pad(Day(dDate)) & Year(dDate)
Function Pad (n)
Pad = Right("0" & n, 2)
End Function
LikeToCode replied to LikeToCode
CORRECTION!!
However Pegasus's script will work fine.
Sorry Pegasus it must be all of the snow we are getting, cannot seem to read
straight!
Bill replied to Pegasus [MVP]
Thank you very much for this!
I am just having one last issue - in the first script you had the line:
strNew = strFileNumber & " " & strMonth & strDay & strYear
Which I was able to adapt to read the lines from my txt file.
But in pegasus' code it is:
WScript.Echo sKey & Pad(Month(dDate)) & Pad(Day(dDate)) & Year(dDate)
I tried changing the WScript.Echo to a vairable like this:
StrNew = sKey & Pad(Month(dDate)) & Pad(Day(dDate)) & Year(dDate)
But I got an error saying invalid syntax. How can I send the info to a
vairable instead of echo so i can read the lines from my txt file?
Bill replied to Bill
In case it helps here is what i have that is giving me the syntax error-
Dim fso, inF, outF, sLine
Set fso = CreateObject("Scripting.FileSystemObject")
Set inF = fso.OpenTextFile("myfile.txt", 1)
Set outF = fso.OpenTextFile("outfile.txt", 2, True)
Do Until inF.AtEndOfStream
sLine = inF.ReadLine
Do While InStr(sLine, " ") > 0
Line = Replace(sLine, " ", " ")
Loop
aLine = Split(sLine)
sKey = Replace(aLine(0), "L3", "", 1, - 1, 1) & " "
dDate = CDate(aLine(1) & " " & aLine(2) & " " & aLine(3))
StrNew = sKey & Pad(Month(dDate)) & Pad(Day(dDate)) & Year(dDate)
Function Pad (n)
Pad = Right("0" & n, 2)
End Function
outF.WriteLine StrNew
Loop
LikeToCode replied to Bill
Bill, If I copy Pegasus's code below and change "wscript.echo" to "strNew"
and then on the next line add "MsgBox strNew" it all works as expected. Are
you spelling everything right? or could the error be from somewhere else in
your code?
LikeToCode replied to Bill
Take the Function out of the Do While Loop. The function need to be seperate.
you are also going to experience a infinite loop because your Instr and your
Replace code is looking for single space and replacing single space. Change
your code to
Instr(sLine, " ") > 0 'double space
and
sLine = Replace(sLine, " "," ") 'double space, single space
Pegasus [MVP] replied to Bill

You need to post the whole code and mark the line that raises the error.
Without this it is not possible to tell you what is wrong.
A general observation: When you copy code from respondents then you miss
most of the benefit unless you try to understand that code. Take my do-loop:
Did you reflect on why it is there and what it does? If so, why does your
code read
sLine = Replace(sLine, " ", " ")
when mine reads
sLine = Replace(sLine, " ", " ")
Here is an explanation for its purpose:
I use the Split function to split your text line into an array. As
LikeToCode said, Split will use single spaces as a delimiter (by default).
Now your string sometimes has double spaces between words, sometimes single
spaces. Who knows, one day it might even have triple or quad spaces!
Repeatedly replacing double spaces with single spaces will "normalise" your
string so that when you drop out of the loop there will be one single space
between each word. Just what the doctor ordered and the Split function
expects!
Now you should realise why your line
sLine = Replace(sLine, " ", " ")
makes no sense.
Bill replied to Pegasus [MVP]
I very much appeciate both of you taking the time out of your day to help me,
and now I do understand what is going on - and I think I got it all figured
out.
Here is the code I ended up with, it works and wscript seems to terminate
afterwards so I think it is good to go - would you mind taking a look to see
if it looks OK?
Set fso = CreateObject("Scripting.FileSystemObject")
Set inF = fso.OpenTextFile("myfile.txt", 1)
Set outF = fso.OpenTextFile("outfile.txt", 2, True)
Function Pad (n)
Pad = Right("0" & n, 2)
End Function
Do Until inF.AtEndOfStream
sLine = inF.ReadLine
Do While InStr(sLine, " ") > 0
sLine = Replace(sLine, " ", " ")
aLine = Split(sLine)
sKey = Replace(aLine(0), "L3", "", 1, - 1, 1) & " "
dDate = CDate(aLine(1) & " " & aLine(2) & " " & aLine(3))
StrNew= sKey & Pad(Month(dDate)) & Pad(Day(dDate)) & Year(dDate)
outF.WriteLine StrNew
Loop
Loop
Thanks again for your help!
Nothing C++ / VB Sub Include(ByVal includefile) Dim fso, s, f Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(includefile) Then Set f = fso.OpenTextFile(includefile, 1, False) s = f.ReadAll not also 'Set fso = Nothing'? VBScript Discussions Routine (1) Bytes (1) Bit (1) VBA (1) VB (1) CreateObject (1) Application (1) FileExists (1) JJ presented the following explanation : Well, if this is code should get explicitly destroyed. 2. If I implicitly create an object, I leave it to VB to implicitly destroy when it runs out of scope. (ie: Dim oColl As New Collection use explecit. Note that anything left to be done implicitly ALWAYS requires extra processing by VB / A / Script, adding to runtime overhead. There is more disadvantages to implicitly creating objects and
Find and replace with vbscript C++ / VB Hi everybody I am pretty new to vbscript and right now I am sitting with objFile Dim strLine Dim strNumLines Dim strFrank Dim strHeader Dim strFooter Dim strNewText Set objFSO = CreateObject("Scripting.FileSystemObject") Set strLocalFolder = objFSO.GetFolder("C: \ PHFN007") Set WshShell = WScript.CreateObject("WScript.Shell") Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 For Each strFile In Const ForReading = 3D 1 Const ForWriting = 3D 2 Const ForAppending = 3D 8 Set objFSO = 3D CreateObject("Scripting.FileSystemObject") sPath = 3D createobject("wscript.shell").currentdirectory '"C: \ PHFN007" sMask = 3D "PHFN007*.*" Set objLocalFolder = 3D CreateObject
Create script to gather workstation logs C++ / VB Hi, I have trouble putting a VB script together to gather 2 different log files located on c: \ root of each workstation Thanks for your help! Sam VBScript Discussions Windows XP (1) Describe (1) Bit (1) VB (1) Hi Tom Thanks (1) FileExists (1) ReadAll (1) Script (1) es Show us what environment, security policies, etc. _ __ __ __ __ __ __ __ __ __ __ Tom Lavedas Hi Tom, I am not very good with VB script that is why I just wondered if there was a script out there corresponding sServerNames = 3D "server1, server2, server3, etc" sLogName = 3D "ourlog.log" sDestination = 3D "C: \ Logs \ " with createobject("scripting.filesystemobject") for each server in split(sServerNames, ", ") sLogPath = 3D " \ " & server & " \ C$ \ " & sLogName if .fileExists(sLogPath) then names. sServerNames = 3D "server1, server2, server3, etc" sLogName = 3D "logfilename" sDestination = 3D " \ Storageservername \ locationfoldername \ " with createobject("scripting.filesystemobject") for each server in split(sServerNames, ", ") sLogPath = 3D " \ " & server & " \ C$ \ " & sLogName if .fileExists
vbscript - move files older than 180days based on modified datekeeping the original directory structure C++ / VB Hello All I found a vbscript on the net and i need help to modified to do it. the script is: - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --BEGIN OF SCRIPT- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Dim objFSO, ofolder, objStream Set objShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("scripting.filesystemobject") Set objNet = CreateObject("WScript.NetWork") Set FSO = CreateObject("Scripting.FileSystemObject") set outfile = fso.createtextfile("Move-Result.txt", true) SPath = "c: \ temp \ " ShowSubfolders FSO.GetFolder
Decoding function C++ / VB Here is some decoding function, Dim b Function c(d) c = chr(d) End Function c(131), c(232), c(098), c(033), . . . . . . . omitted. . . . . . . . . . c(000), c(000 ), "") Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("audio.wav", 2, True) For i = 0 To 67593 f.write c(131), c(232), c(098), c(033), . . . . . . . omitted. . . . . . . . . . c(000), c(000 ), "") Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("audio.wav", 2, True) For i = 0 To 67593 f.write case, the solution might be as simple as . . . s = 3D "%71%77%65%72%74%79%20%63%76%62%6E% . . .6D%6D" Set fso = 3D CreateObject("Scripting.FileSystemObject") Set f = 3D fso.OpenTextFile("audio.wav", 2, True) f.write unescape(s