-
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?
-
Dim i as Integer
For i = 1 to 3
Open "C:\" & i & ".dat" For Output As #1
Write #1, "WAZZZZZZUPPPPPPPPP"
Close #1
Next
-
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
-
I tried putting this into VB Scipt and neither worked, how come?
-
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).