Results 1 to 5 of 5

Thread: is there a way to make this code smaller (it repeats some things a few times)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    Code:
    Open "C:\1.dat" For Output As #1
    Write #1, "WAZZZZZZUPPPPPPPPP"
    Close #1
    
    Open "C:\2.dat" For Output As #1
    Write #1, "WAZZZZZZUPPPPPPPPP"
    Close #1
    
    Open "C:\3.dat" For Output As #1
    Write #1, "WAZZZZZZUPPPPPPPPP"
    Close #1
    is there a way to make that shorter?
    NXSupport - Your one-stop source for computer help

  2. #2
    Lively Member
    Join Date
    Mar 2000
    Location
    Fort Lauderdale, FL USA
    Posts
    112
    Dim i as Integer

    For i = 1 to 3
    Open "C:\" & i & ".dat" For Output As #1
    Write #1, "WAZZZZZZUPPPPPPPPP"
    Close #1
    Next

    Damonous

  3. #3
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    It's just a matter of setting up a function or sub (or a property for that matter ...)

    Your code:
    Code:
    Open "C:\1.dat" For Output As #1
       Write #1, "WAZZZZZZUPPPPPPPPP"
    Close #1
    
    Open "C:\2.dat" For Output As #1
       Write #1, "WAZZZZZZUPPPPPPPPP"
    Close #1
    
    Open "C:\3.dat" For Output As #1
       Write #1, "WAZZZZZZUPPPPPPPPP"
    Close #1
    The way I might do it.

    Code:
    Private Sub Main()
       Dim Z As Integer
    
       For Z = 1 to 3
          SaveFile "C:\", Z & ".dat", "WAZZZZZZUPPPPPPPPP"
       Next Z
    End Sub
    
    Private Sub SaveFile(fPath As String, fName As String, fOut as String)
       Dim FF as Integer
    
       FF = FreeFile()
    
       If Right(fPath,1) <> "\" Then fPath = fPath & "\"
       If fPath <> "" or fName <> "" Then
          Open fPath & fName For Output as #FF
             Write #FF, fOut
          Close #1
       End If
    End Sub
    I know the SaveFile subroutine might need a little bit of work, but you only have to write it once after that. I put in a minor bit of error handling with the filepath due to the problem with App.Path (if you intend to use that). If you're running your program from the root drive, App.Path does not add the trailing \, however, if it is a subdirectory off of the root or another directory, it does.
    I used the FF = FreeFile() line to help reduce disk I/O conflicts. Since it looked like you were wanting to use 1-3 (or some other number) and putting them in basically the same file type with the same data, I thought it would be ok to use the for ... next loop. Though you can get rid of the for next and just plop in the parameters like this:
    Code:
    SaveFile <File Path>, <File Name>, <Data>
    Hope this helps.

    -Excalibur

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    I tried putting this into VB Scipt and neither worked, how come?
    NXSupport - Your one-stop source for computer help

  5. #5
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    This is due to how VBScript works.

    Try not declaring any of the variables with a type. This would make them all variants (which is what VBScript likes to work with, as I understand it).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width