Hi,

I would like to write in test file "01","02", ....,"99"
For this I wrote,

Dim MyFile
Const ForWriting = 2

Set objFichier = CreateObject("Scripting.FileSystemObject")
Set MyFile = objFichier.OpenTextFile("test.txt",ForWriting,True)

for i = 1 to 99
m=""""+CStr(i)+""""+","
MyFile.Writeline m
next

MyFile.Close
Wscript.Quit 0

This doesn't work, as test.txt has,

1,
2,
3,
...
99,

1) How avoid to go to next line after each Myfile.Writeline ?
2) How to format 1 to 01 ?

Re: Format file writing by McKirahan

McKirahan
Sat May 03 07:13:05 CDT 2008

"seb" <seb@hotmail.com> wrote in message
news:481c38f8$0$736$426a74cc@news.free.fr...
> Hi,
>
> I would like to write in test file "01","02", ....,"99"
> For this I wrote,
>
> Dim MyFile
> Const ForWriting = 2
>
> Set objFichier = CreateObject("Scripting.FileSystemObject")
> Set MyFile = objFichier.OpenTextFile("test.txt",ForWriting,True)
>
> for i = 1 to 99
> m=""""+CStr(i)+""""+","
> MyFile.Writeline m
> next
>
> MyFile.Close
> Wscript.Quit 0
>
> This doesn't work, as test.txt has,
>
> 1,
> 2,
> 3,
> ...
> 99,
>
> 1) How avoid to go to next line after each Myfile.Writeline ?
> 2) How to format 1 to 01 ?

Will this help?

Option Explicit
'*'
Dim i, s
For i = 1 To 99
s = s & Chr(34) & Right(CStr(100+1),2) & Chr(34) & ","
Next
s = Left(s,Len(s)-1)
'*'
Dim objFichier
Set objFichier = CreateObject("Scripting.FileSystemObject")
Dim MyFile
Set MyFile = objFichier.CreateTextFile("test.txt",True)
MyFile.Write s
MyFile.Close
'*'
Wscript.Quit 0



Re: Format file writing by McKirahan

McKirahan
Sat May 03 07:15:54 CDT 2008

"McKirahan" <News@McKirahan.com> wrote in message
news:4eWdnRTD3fdMy4HVnZ2dnUVZ_r6rnZ2d@comcast.com...
> "seb" <seb@hotmail.com> wrote in message
> news:481c38f8$0$736$426a74cc@news.free.fr...
> > Hi,
> >
> > I would like to write in test file "01","02", ....,"99"
> > For this I wrote,
> >
> > Dim MyFile
> > Const ForWriting = 2
> >
> > Set objFichier = CreateObject("Scripting.FileSystemObject")
> > Set MyFile = objFichier.OpenTextFile("test.txt",ForWriting,True)
> >
> > for i = 1 to 99
> > m=""""+CStr(i)+""""+","
> > MyFile.Writeline m
> > next
> >
> > MyFile.Close
> > Wscript.Quit 0
> >
> > This doesn't work, as test.txt has,
> >
> > 1,
> > 2,
> > 3,
> > ...
> > 99,
> >
> > 1) How avoid to go to next line after each Myfile.Writeline ?
> > 2) How to format 1 to 01 ?
>
> Will this help?
>
> Option Explicit
> '*'
> Dim i, s
> For i = 1 To 99
> s = s & Chr(34) & Right(CStr(100+1),2) & Chr(34) & ","
> Next
> s = Left(s,Len(s)-1)
> '*'
> Dim objFichier
> Set objFichier = CreateObject("Scripting.FileSystemObject")
> Dim MyFile
> Set MyFile = objFichier.CreateTextFile("test.txt",True)
> MyFile.Write s
> MyFile.Close
> '*'
> Wscript.Quit 0

Typo! Use this line instead:

s = s & Chr(34) & Right(CStr(100+i),2) & Chr(34) & ","



Re: Format file writing by Todd

Todd
Sat May 03 09:46:41 CDT 2008

seb wrote:
> Hi,
>
> I would like to write in test file "01","02", ....,"99"
> For this I wrote,
>
> Dim MyFile
> Const ForWriting = 2
>
> Set objFichier = CreateObject("Scripting.FileSystemObject")
> Set MyFile = objFichier.OpenTextFile("test.txt",ForWriting,True)
>
> for i = 1 to 99
> m=""""+CStr(i)+""""+","
> MyFile.Writeline m
> next
>
> MyFile.Close
> Wscript.Quit 0
>
> This doesn't work, as test.txt has,
>
> 1,
> 2,
> 3,
> ...
> 99,
>
> 1) How avoid to go to next line after each Myfile.Writeline ?

Change Writeline to Write.


> 2) How to format 1 to 01 ?

Change CStr(i) to Right(i+100,2)


Also I would use Chr(34) instead of """" to make the code easier to follow.
And to eliminate the comma from the end of the line you might consider
stoping the For/Next counter early and adding the final number seperately.

For i = 1 to 98
MyFile.Write Chr(34) & Right(i+100,2) & Chr(34) & ","
Next
MyFile.Write Chr(34) & "99" & Chr(34)


--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)



Re: Format file writing by seb

seb
Sun May 04 12:15:18 CDT 2008

Very nice ! thank you.