Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

change text in .txt file with vbscript or .bat script with a radio button

Posted on 2011-09-19
5
Medium Priority
?
539 Views
Last Modified: 2012-06-27
i have a .txt file that has some true/false info in it.

i was wondering if there is a vbscript or .bat that allows a popup to change them on check of
the radio button.

example:
c:\client\client-jones.txt

click on script, opens popup with radio buttons as follows:
paid march [ ] true  [ ] false
paid april  [ ] true  [ ] false
paid may  [ ] true  [ ] false

the text file is like this:
 
clent=jones, jimmy
address=
city=
state=
zip=
phone=


paid invoices
march=true
april=true
may=false

Open in new window


it would only change the true/false after each month=?

thanks
0
Comment
Question by:webdott
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 36566705
Hi, with pure vbscript, you won't have the ability to use check boxes, but you could build a HTA that would read the file and display checkboxes for each option.

I can help with that, but I have a question first....is there anything else after the "paid invoices" section?  I would write the HTA to read the file up to the "paid invoices" line, then read the rest of the options until the end of the file, and display them....

Rob.
0
 

Author Comment

by:webdott
ID: 36568488
just all the months - examples:
january=true
february=false

etc. for each month thru to december.

thanks for your help!

what is a HTA? is it a script that is edited in notepad?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 36570873
Hi, first, create a VBS file called LaunchHTA.vbs (or whatever you want to call it), and put this code in it (by pasting it in to Notepad):
strHTA = "C:\Temp\Scripts\TestHTA2.hta"
Set objShell = CreateObject("WScript.Shell")
If WScript.Arguments.Count = 1 Then
	objShell.Run "mshta """ & strHTA & """ """ & WScript.Arguments.Item(0) & """", 1, False
Else
	objShell.Run "mshta """ & strHTA & """", 1, False
End If

Open in new window


In that VBS code, change "C:\Temp\Scripts\TestHTA2.hta" to the path that you save the following HTA code to.  Create a file called the same name as that HTA you just referenced, and paste the below code into that file with notepad:
<html>
<head>
<title>Test</title>
<HTA:APPLICATION 
	ID = "objHTA"
	APPLICATIONNAME="Test"
	BORDER="dialog"
	SCROLL="no"
	SINGLEINSTANCE="yes"
	WINDOWSTATE="normal"
	Maximizebutton="no"
	ContextMenu="no"
>
<html>
	<head>
		<script language="vbscript">
			Dim strOptions
			Sub Window_OnLoad
				intWidth = 800
				intHeight = 600
				Me.ResizeTo intWidth, intHeight
				Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
			    If Mid(document.location, 6, 3) = "///" Then
			    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
			    Else
			    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
			    End If
				strFileName = Trim(Replace(LCase(objHTA.CommandLine), """" & LCase(strHTAPath) & """", ""))
				If strFileName <> "" Then
					If Left(strFileName, 1) = """" Then strFileName = Mid(strFileName, 2)
					If Right(strFileName, 1) = """" Then strFileName = Left(strFileName, Len(strFileName) - 1)
					txt_filepath.Value = strFileName
					GetData
				End If
			End Sub
			
			Sub GetData
				span_info.InnerHTML = ""
				span_options.InnerHTML = ""
				strOptions = ""
				Set objFSO = CreateObject("Scripting.FileSystemObject")
				Const ForReading = 1
				strFileName = Trim(txt_filepath.Value)
				If strFileName <> "" Then
					If objFSO.FileExists(strFileName) = True Then
						Set objFile = objFSO.OpenTextFile(strFileName, ForReading, False)
						blnStartOfOptions = False
						strHTML = "<table width='50%'><tr><th width='50%' align='left'>Month</th><th>Paid</th></tr>"
						While Not objFile.AtEndOfStream
							strLine = Trim(objFile.ReadLine)
							If LCase(strLine) = LCase("paid invoices") Then blnStartOfOptions = True
							If blnStartOfOptions = True Then
								If InStr(strLine, "=") > 0 Then
									strMonth = Split(strLine, "=")(0)
									strPaid = Split(strLine, "=")(1)
									If strOptions = "" Then
										strOptions = strMonth
									Else
										strOptions = strOptions & "," & strMonth
									End If
									If LCase(strPaid) = "true" Then
										strChecked = " checked"
									Else
										strChecked = ""
									End If
									strHTML = strHTML & vbCrLf & "<tr><td>" & strMonth & "</td><td align='center'><input type='checkbox' id='chk_" & strMonth & "' name= 'chk_" & strMonth & "'" & strChecked & "></td></tr>"
								End If
							Else
								span_info.InnerHTML = span_info.InnerHTML & strLine & "<BR>"
							End If
						Wend
						objFile.Close
						span_options.InnerHTML = strHTML
					Else
						MsgBox "Unable to find file. Please enter a valid file path."
					End If
				Else
					MsgBox "Please enter a file path."
				End If
			End Sub
			
			Sub SaveData
				Set objFSO = CreateObject("Scripting.FileSystemObject")
				Set objFile = objFSO.CreateTextFile(txt_filepath.Value, True)
				objFile.Write Replace(span_info.InnerHTML, "<BR>", vbCrLf)
				objFile.WriteLine "paid invoices"
				For Each strMonth In Split(strOptions, ",")
					strPaid = LCase(Eval("chk_" & strMonth & ".checked"))
					objFile.WriteLine strMonth & "=" & strPaid
				Next
				objFile.Close
				MsgBox "Data has been saved."
			End Sub
		</script>
	</head>
	<body>
		<br>
		Please type in the path to the file you want to open, and click "Get Data"
		<br>
		<input type="text" name="txt_filepath" id="txt_filepath" size="60">
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="button" id="btn_getdata" name="btn_getdata" value="Get Data" onclick="GetData">
		<br>
		<br>
		<span id="span_info"></span>
		<br>
		<span id="span_options"></span>
		<br><br>
		<input type="button" id="btn_savedata" name="btn_savedata" value="Save Data" onclick="SaveData">
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="button" id="btn_exit" name="btn_exit" value="Exit" onclick="window.close">		
	</body>
</html>

Open in new window


Then, to run it, drag a text file onto the VBS file, using Windows Explorer.

Regards,

Rob.
0
 

Author Closing Comment

by:webdott
ID: 36594164
thanks
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36594175
No problem. The HTML in the HTA I wrote is very basic (it was a quick and dirty build), and doesn't look very nice, but you can add whatever HTML code you like to spruce it up a bit.  It supports CSS as well if you want to use it.

Rob.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Progress

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question