Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find position 1421 in a txt file

Status
Not open for further replies.

kdjonesmtb2

Technical User
Nov 19, 2012
93
US
I have a text file that is displaying a character of 'm' according to HIPAA compliance editor in postion 1421

However I do not see this character visually in the file

How can you write script to display via msgbox what character is in position 1431 in a text file?

thanks
 
Hi [peace]
Try this Code :

Code:
Option Explicit
Dim Title,FromLine,ToLine
Title = "Extract Lines From TextFile © Hackoo Crackoo 2013"
FromLine = InputBox("Please select the number of the start line to extract",Title,"1421")
If FromLine = "" Or Not IsNumeric(FromLine) Then WScript.Quit
ToLine = InputBox("Please select the number of the end line to extract",Title,"1421")
If ToLine = "" Or Not IsNumeric(ToLine) Then WScript.Quit
MsgBox ExtractLinesFromTextFile(BrowseForFile,Int(FromLine),Int(ToLine)),64,Title

Function BrowseForFile()
	Dim shell : Set shell = CreateObject("WScript.Shell")
	Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
	Dim tempFolder : Set tempFolder = fso.GetSpecialFolder(2)
	Dim tempName : tempName = fso.GetTempName()
	Dim tempFile : Set tempFile = tempFolder.CreateTextFile(tempName & ".hta")
	tempFile.Write _
	"<html>" & _
	"    <head>" & _
	"        <title>Browse</title>" & _
	"    </head>" & _
	"    <body>" & _
	"        <input type='file' id='f'>" & _
	"        <script type='text/javascript'>" & _
	"            var f = document.getElementById('f');" & _
	"            f.click();" & _
	"            var shell = new ActiveXObject('WScript.Shell');" & _
	"            shell.RegWrite('HKEY_CURRENT_USER\\Volatile Environment\\MsgResp', f.value);" & _
	"            window.close();" & _
	"        </script>" & _
	"    </body>" & _
	"</html>"
	tempFile.Close
	shell.Run tempFolder & "\" & tempName & ".hta",0,True
	BrowseForFile = shell.RegRead("HKEY_CURRENT_USER\Volatile Environment\MsgResp")
	shell.RegDelete "HKEY_CURRENT_USER\Volatile Environment\MsgResp"
End Function

Public Function ExtractLinesFromTextFile(ByRef TextFile, ByRef FromLine, ByRef ToLine) '<-- Inclusive
	Const TristateUseDefault = -2 'To Open the file using the system default.
	On Error Resume Next
	If FromLine <= ToLine Then
		With CreateObject("Scripting.FileSystemObject").OpenTextFile(TextFile,1,true,TristateUseDefault)
			If Err.number <> 0 Then
				MsgBox err.description,16,err.description
				Exit Function
			Else
				Do Until .Line = FromLine Or .AtEndOfStream
					.SkipLine
				Loop
				Do Until .Line > ToLine Or .AtEndOfStream
					ExtractLinesFromTextFile = ExtractLinesFromTextFile & (.ReadLine & vbNewLine)
				Loop
			End If
		End With
	Else
		MsgBox "Error to Read Line in TextFile", vbCritical,"Error to Read Line in TextFile"
	End If
End Function
 
Or quick'n'dirty:

Code:
[blue]Option Explicit

Dim fso
Dim strfile
    
strfile = "c:\temp\test.xps" [green]' <= your filename goes here

' OP seemed a little confused between char position 1421 and 1431. This example assumes 1431[/green]    
With CreateObject("scripting.filesystemobject")
    If .GetFile(strfile).Size >= 1431 Then MsgBox Mid$(.OpenTextFile(strfile, 1).ReadAll, 1431, 1)
End With[/blue]

However, the issue may actually be related to the underlying format of the text file. Is it ANSI or Unicode?
 
Hello,

I tried the code above but I receiving the following syntax error

The test run cannot continue due to a syntax error.

Invalid character

Line (1): "MsgBox $Mid(.OpenTextFile(strfile, 1).ReadAll, 1431, 1)".


Option Explicit

Dim fso
Dim strfile

strfile = "\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Text files\qtptest1.txt" ' <= your filename goes here


With CreateObject("scripting.filesystemobject")
If .GetFile(strfile).Size >= 1431 Then
MsgBox $Mid(.OpenTextFile(strfile, 1).ReadAll, 1431, 1)
End With
 
Your

If .GetFile(strfile).Size >= 1431 Then
MsgBox $Mid(.OpenTextFile(strfile, 1).ReadAll, 1431, 1)

should read

If .GetFile(strfile).Size >= 1431 Then MsgBox Mid(.OpenTextFile(strfile, 1).ReadAll, 1431, 1)
 
Hello,

This last code worked fine

The code is returning no visible characters but the compliance editor is returning a 'M'

What is the best way to delete this character from the file?

Thanks

Option Explicit

Dim fso
Dim strfile

strfile = "\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Text files\qtptest_full.txt" ' <= your filename goes here


With CreateObject("scripting.filesystemobject")
If .GetFile(strfile).Size >=1431 Then MsgBox Mid(.OpenTextFile(strfile, 1).ReadAll, 1431, 1)
End with
 
I wouldn't delete anything if your results don't line up...

I'll echo strongm's question:
strongm said:
However, the issue may actually be related to the underlying format of the text file. Is it ANSI or Unicode?
 
Hello,

I was able to confirm that my txt file type is "ascii" via this code

How should I handle the extraneous 'm' as it causing my file to fail compliance at position 1431?

Thanks


Option Explicit

Dim fso
Dim strfile
Dim ts
Dim char1
Dim char2
Dim text


strfile = "\\ikanas267\Documents\kgittensjones\My Documents\QTP 834\Archive2\qtptest_full.txt" ' <= your filename goes here


With CreateObject("scripting.filesystemobject")
If .GetFile(strfile).Size >=1431 Then MsgBox Mid(.OpenTextFile(strfile, 1).ReadAll, 1430, 1)
End with




set fso = createobject("scripting.filesystemobject")
set ts = fso.opentextfile(strfile)
char1 =ts.read(1)
char2 =ts.read(1)
ts.close
if asc(char1) = 255 and asc(char2) = 254 then
msgbox "unicode"
set ts = fso.opentextfile(strfile,,,true)
else
msgbox "ascii"
set ts = fso.opentextfile(strfile)
end if
text = ts.readall
ts.close
msgbox text






 
>if asc(char1) = 255 and asc(char2) = 254 then

a) The BOM isn't ALWAYS that way around.
b) The BOM is optional
 
The other possibility, of course, is that they are counting from 0 instead of 1 ...
 
Hello,

So how do I determine the next step?

Can I run a script to determine the ascii type/name of the character at postion 1431?

The character at 1430 is "~" which is allowable value

 
Hello

Position 1432 has " "


So what is the code to check the type of this character - could it be non breaking space?

thanks
 
Help yourself out. What is the ASC() value?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Easier is to type

edit /50 "c:\path to file\file.txt"

50 characters per line so 1421/50 is the 28th line at column 21. The ASCII character code is in the status bar. If unicode double the above calculations and the character code is over two positions.

Type for Help

edit /?

I'd use

edit /h /50 "c:\pathtofile\file.txt
 
Code:
[blue]Option Explicit

Dim fso
Dim strfile
    
strfile = "c:\temp\test.xps" ' <= your filename goes here

' OP seemed a little confused between char position 1421 and 1431. This example assumes 1431    
With CreateObject("scripting.filesystemobject")
    If .GetFile(strfile).Size >= 1431 Then MsgBox [b]Asc$([/b]Mid$(.OpenTextFile(strfile, 1).ReadAll, 1431, 1)[b])[/b]
End With[/blue]
 
strongm, are you sure VBS supports Asc[!]$[/!] and Mid[!]$[/!] functions ?
 
Nope. Made the correction for that earlier on, but forgot to do so for this second illustrative example. Mea Culpa.
 
>Easier is to type

Possibly.

Easier to use? Debatable.

And 'edit' isn't available on any 64bit Windows platform.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top