From: LikeToCode on
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?

"Bill" wrote:

> Thank you very much for this!
>
> I'm 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's:
>
> 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?
>
>
>
> "Pegasus [MVP]" wrote:
>
> >
> >
> > "Bill" <Bill(a)discussions.microsoft.com> said this in news item
> > news:0F3B395D-8BB9-47AA-ABAC-0CF8AF43E349(a)microsoft.com...
> > > 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's 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!
> > >
> >
> > 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
> >
> >
From: LikeToCode on
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


From: Pegasus [MVP] on


"Bill" <Bill(a)discussions.microsoft.com> said this in news item
news:4DA670DD-F56E-4573-B42C-2BD9CF889A94(a)microsoft.com...
> Thank you very much for this!
>
> I'm 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's:
>
> 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?

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's 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.

From: Bill on
I very much appeciate both of you taking the time out of your day to help me,
and now I do understand what's going on - and I think I got it all figured
out.

Here's the code I ended up with, it works and wscript seems to terminate
afterwards so I think it's 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!


"Pegasus [MVP]" wrote:

>
>
> "Bill" <Bill(a)discussions.microsoft.com> said this in news item
> news:4DA670DD-F56E-4573-B42C-2BD9CF889A94(a)microsoft.com...
> > Thank you very much for this!
> >
> > I'm 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's:
> >
> > 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?
>
> 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's 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.
>
First  |  Prev  | 
Pages: 1 2 3
Prev: Running ASP Page.
Next: SQL Version (Build) with WMI