vbscript
hi all
Im building a form using .hta which outputs a text file
<script language="vbscript">
Function mybutton
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.Createtextfile("C:\test.txt", True)
a.WriteLine (form1.textfield1.value & form1.textfield2.value)
a.Close
End Function
as you can see i am writing from two fields but in truth i am writing from about 50. If a field is left blank the text file rcords this as an empty line.
How do i ignore empty fields so the text file writes line by line of values
[596 byte] By [
AXEmonster] at [2007-11-17 18:31:54]

# 1 Re: vbscript
asuming that there are multiple fields and you want to write them line by line in a text file, what is the problem in trying this:
if(trim(form1.textfield1.value) <> "")
a.WriteLine (form1.textfield1.value)
the check will prevent blank field to make a blank line in the file.
(if i am getting you correctly...:)
# 2 Re: vbscript
thanks anupam
you are right.
if i can explain abit more maybe you could think of an easier method
the form consists of the following
row 1 has 5 fields which accept data for one line
row 2 has 5 fields which accept data for one line
row 3 has 5 fields which accept data for one line
etc for 50 lines
<script language="vbscript">
Function mybutton
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.Createtextfile("C:\test.txt", True)
'first row--------
a.WriteLine (form1.textfield1.value & form1.textfield2.value & form1.textfield3.value & form1.textfield4.value & form1.textfield5.value)
'second row-------
a.WriteLine (form1.textfield6.value & form1.textfield7.value & form1.textfield8.value & form1.textfield9.value & form1.textfield10.value)
'etc for 50 rows------
a.Close
End Function
If i was to use your example for each row
if(trim(form1.textfield1.value & form1.textfield2.value & form1.textfield3.value & form1.textfield4.value & form1.textfield5.value) <> "")
the code would be huge.
is there any way i can reference the fields by their rows which would cut down in the code.
# 3 Re: vbscript
with anupams example i am getting errors when i run it
if(trim(& form1.trk2.value & form1.trk3.value & form1.trk4.value & form1.trk5.value) <> "")
a.WriteLine (form1.trk1.value & form1.trk2.value & form1.trk3.value & form1.trk4.value & form1.trk5.value)
i believe that you need an 'End if'
where would that be written?
after the
'if(trim(& form1.trk2.value & form1.trk3.value & form1.trk4.value & form1.trk5.value) <> "")
end if
-------
or after
if(trim(& form1.trk2.value & form1.trk3.value & form1.trk4.value & form1.trk5.value) <> "")
a.WriteLine (form1.trk1.value & form1.trk2.value & form1.trk3.value & form1.trk4.value & form1.trk5.value)
end if
-------
# 4 Re: vbscript
The 'end if' that i forgot to write is
if(trim(& form1.trk2.value & form1.trk3.value & form1.trk4.value & form1.trk5.value) <> "")
a.WriteLine (form1.trk1.value & form1.trk2.value & form1.trk3.value & form1.trk4.value & form1.trk5.value)
end if
I will be back with a solution on Moday...
till then enjoy the weekend :)
# 5 Re: vbscript
See attached file - but be sure to change it to an .ASP extension
Based on the previous threads, it sounds like each row in the form may or may not have all 5 values? This code will skip a line if the entrie line is blank.
If there is even one value in a field in that row, it will write a line with a tab to separate each field, even if that field is blank.
Are you expecting the textfile to be in a certain format (other than no blank lines)? like does the first field in each row need a place holder even if it is blank or should the code just look for the nex field value in that row?
# 6 Re: vbscript
Thanks goddess
Let me explain abit more
I have a form like this:-
row1
field 1, field2, field3, field4
row2
field 1, field2, field3, field4
etc x50
all the fields from 1 to 4 will be filled across each row but not all the rows may be filled.
The user may fill in rows 1 - 10 leaving 11 - 50 blank.
if a user fills in rows 1 -5 and then 7 -8 (missing row 6) then there is a space in the txt file indicating the missing row of data, in this case row 6
What i want is if row? is missed (the full row of 4 fields) then it will not write a blank line to the txt file just write the next line of data.
I have tried
if(trim(form1.textfield1.value) <> "")
a.WriteLine (form1.textfield1.value)
End if
but that doesnt work and i get syntax errors
any ideas.
# 7 Re: vbscript
goddess
I have just tested your example and it does what i want, almost
I dont want any spaces between the form fields so it appears as one long string for each line or row
the next row will write another line to the file but on its own line
hope this isnt confusing
# 8 Re: vbscript
If you don't want any spaces between the row fields, then just take out the "& vbTab " in the code.
I updated the code to take out the vbTab spacer and I changed some of the logic so that it only creates 4 fields in each row.
# 9 Re: vbscript
Originally posted by goddess_spanky
If you don't want any spaces between the row fields, then just take out the "& vbTab " in the code.
I sussed the "& vbTab "
Thanks very much for your help
# 10 Re: vbscript
Using the example
<script language="vbscript">
Function mybutton
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.Createtextfile("C:\test.txt", True)
Is ther any way i can name the text file using a form on the page before it is created
e.g
<script language="vbscript">
Function mybutton
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.Createtextfile("C:\myvariable .txt", True)
# 11 Re: vbscript
Since you are creating the web page is already loaded, you can pull the name of the file from the form instead of hard-coding it.
You should definitely check to make sure some file name was entered before tyring to create the file however.
In your form (and I am not sure if you want the user to input this information, but I am assuming NO) do:
<INPUT TYPE="Text" NAME="FileName" VALUE="OutputFile.txt" ReadOnly>
If you want them to enter their own file path and file name, that's gonna get trickier to check for entry errors. If you do want to do this, just leave out the default value and remove the ReadOnly attribute.
<script language="vbscript">
Function mybutton
Set fs = CreateObject("Scripting.FileSystemObject")
myFile = form1.FileName.value
'//Check that there is valid data in the file name field
if trim(myFile) <> "" then
'//If you are expecting the user to enter the file extension, then check to make sure it is a .txt extension
if Lcase(Right(myFile,3)) = "txt" then
Set a = fs.CreateTextFile("C:\" & myFile & "", True)
else
Set a = fs.CreateTextFile("C:\" & myFile & ".txt", True)
end if
end if
set fso = nothing
end function
