Solved

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

Posted on 2011-09-19
5
489 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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
thanks
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now