Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


hello! problem with code

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,47679
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> hello! problem with code
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 hello! problem with code - 5/29/2007 2:41:44 AM   
  jbauer

 

Posts: 2
Score: 0
Joined: 5/29/2007
Status: offline
I belive there is something wrong with my case selection but i am not sure. I would appreciate any input, as this script is not working at the moment.
I am trying to read a ftp log file (.txt) and write any errors to another file to use as a upload template. here is the code i have written.

set fso=createobject("scripting.filesystemobject")
Set infile=fso.OpenTextFile("c:\logfile_bad.txt", 1)
Set out=fso.CreateTextFile("c:\ftpput.txt",true)
Do Until infile.AtEndOfStream
myrec=infile.readline
Select Case infile.readline
 Case mid(myrec,6,3)="put"
  If lst_put="" Then
  lst_put=myrec
  Else
  out.writeline mid(lst_put,6)
  lst_put=myrec
  End If
 Case mid(myrec,5,8)="Transfer"
  myrec=""
 Case mid(myrec,6,2)="cd"
  out.writeline mid(myrec,6)
End Select
loop
infile.close()
out.close()
Set fso=Nothing
 
 
Post #: 1
 
 RE: hello! problem with code - 5/29/2007 3:05:21 AM   
  ehvbs

 

Posts: 2200
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi jbauer,

there are 2 problems with your script:

(1) Select Case <SelectExpression>
        Case <FirstCaseExpression>
        ...
        Case <SecondCaseExpression>
         ...
         Case Else
    End Select

    will compare <SelectExpression> against each <*CaseExpression> and execute
   the code below/after the first matching Case <*CaseExpression> or the code
    below/after the Case Else statement. So

      Select Case infile.readline
          Case mid(myrec,6,3)="put"

    can't possibly work.

(2)  Each infile.readline will consume a line from your input file. If you really want
      to process two line for each 'round' you should store both lines in variables
      (like myrec).

So some postings concerning nifty uses of "Select Case" not withstanding, I'd use some
oldfashioned If..EndIf statements to process your input:

      sFirstLine = infile.ReadLine
      sSecLine = infile.ReadLine  ' perhaps not needed
      If "put" = Mid( sFistLine, 6, 3 ) Then
          ...
      End If
      If "Transfer" = Mid( sFirstLine, 5, 8 Then
         ...
      End If

If your script works as expected, you can 'optimize' using Else and/or
ElseIf or even Select Case.

Good luck

ehvbs

(in reply to jbauer)
 
 
Post #: 2
 
 RE: hello! problem with code - 5/29/2007 3:41:46 AM   
  jbauer

 

Posts: 2
Score: 0
Joined: 5/29/2007
Status: offline
thanks a lot ehvbs. i used if..else and it works great now

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: hello! problem with code - 5/29/2007 4:40:38 AM   
  mcds99


Posts: 434
Score: 4
Joined: 2/28/2006
Status: online
So this would be a Case where using Case would not work.

Yes it's a week attempt at humor with some education included it's good to have examples of where NOT to use specific code.
This way there is a way to compare what works and what doesn't.

_____________________________

Sam

Keep it Simple Make it Fun KiSMiF

(in reply to jbauer)
 
 
Post #: 4
 
 RE: hello! problem with code - 5/29/2007 5:15:43 AM   
  DiGiTAL.SkReAM


Posts: 1184
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Actually, and I didn't WANT to post this - it is my OCD forcing me to do it, this would have been a situation where Select Case True would have made sense:

      

It is helpful to always remember to use Case Else, too.  You might not ever NEED it, but it can be a great debugging tool.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to mcds99)
 
 
Post #: 5
 
 RE: hello! problem with code - 5/29/2007 5:22:45 AM   
  ehvbs

 

Posts: 2200
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi DiGiTAL.SkReAM,

what's an OCD and how could I get one of my own?

ehvbs

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 6
 
 RE: hello! problem with code - 5/29/2007 5:38:50 AM   
  dm_4ever


Posts: 2663
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
quote:

ORIGINAL: ehvbs

Hi DiGiTAL.SkReAM,

what's an OCD and how could I get one of my own?

ehvbs


I think this is what he is referring to: OCD

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: hello! problem with code - 5/29/2007 6:09:05 AM   
  ebgreen


Posts: 5035
Score: 31
Joined: 7/12/2005
Status: online
I have not spent a lot of time looking at all of the requirements for the initial question but I wanted to add in one gotcha to watch out for when using Select Case True. This code does one thing:

strTest = "This is the test string"
If InStr(strTest, "This") > 0 Then
   'Code
End If
If Instr(strTest, "the") > 0 Then
   'Code
End If
If InStr(strTest, "string") > 0 Then
   'Code
End If

This code looks like it might do the same thing but does not:

strTest = "This is the test string"
Select Case True
   Case InStr(strTest, "This") > 0
      'Code
   Case InStr(strTest, "test") > 0
      'Code
   Case InStr(strTest, "string") > 0
      'Code
End Select

This If-End If code is what the Select-Case above really does:

strTest = "This is the test string"
If InStr(strTest, "This") > 0 Then
   'Code
ElseIf InStr(strTest, "test") > 0 Then
   'Code
ElseIf InStr(strTest, "string") > 0 Then
   'Code
End If

< Message edited by ebgreen -- 5/29/2007 6:10:30 AM >


_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dm_4ever)
 
 
Post #: 8
 
 RE: hello! problem with code - 5/29/2007 6:26:26 AM   
  DiGiTAL.SkReAM


Posts: 1184
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Heh.  You don't want one.

Actually, to be more precise, it is OCPD I live with, but since nobody ever comes across or hears of OCPD, it is usually easier to describe it as OCD.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to dm_4ever)
 
 
Post #: 9
 
 RE: hello! problem with code - 5/29/2007 6:30:42 AM   
  DiGiTAL.SkReAM


Posts: 1184
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
quote:

ORIGINAL: ebgreen

I have not spent a lot of time looking at all of the requirements for the initial question but I wanted to add in one gotcha to watch out for when using Select Case True. This code does one thing:

strTest = "This is the test string"
If InStr(strTest, "This") > 0 Then
  'Code
End If
If Instr(strTest, "the") > 0 Then
  'Code
End If
If InStr(strTest, "string") > 0 Then
  'Code
End If

This code looks like it might do the same thing but does not:

strTest = "This is the test string"
Select Case True
  Case InStr(strTest, "This") > 0
     'Code
  Case InStr(strTest, "test") > 0
     'Code
  Case InStr(strTest, "string") > 0
     'Code
End Select

This If-End If code is what the Select-Case above really does:

strTest = "This is the test string"
If InStr(strTest, "This") > 0 Then
  'Code
ElseIf InStr(strTest, "test") > 0 Then
  'Code
ElseIf InStr(strTest, "string") > 0 Then
  'Code
End If


You are correct, ebgreen, however, in the instance above, i carefully looked at the "cases" that were being tested, and they were:
3 Characters, starting at position 6
8 Characters, starting at position 5
2 Characters, starting at position 6

From what he was testing for ("put", "Transfer", "cd") I was able to determine that in no case would these 'overlap' so to speak, so it would be safe to use Select...Case.
But what you say is aboslutely correct, and if there were any overlap - one test shows true for multiple results - then Select Case would not be the proper method to use.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: hello! problem with code - 5/29/2007 7:31:23 AM   
  dm_4ever


Posts: 2663
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
quote:

ORIGINAL: DiGiTAL.SkReAM

Heh.  You don't want one.

Actually, to be more precise, it is OCPD I live with, but since nobody ever comes across or hears of OCPD, it is usually easier to describe it as OCD.


OCD and OCPD always bring to mind Jack Nicholson in the movie "As Good As It Gets" since he shows symptoms of both.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 11
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> hello! problem with code Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts