I have a file that is comma delimited
Text File:
"some_key_1", "Translation 1"
"some_key_2", "Translation 2"
"some_key_3", "Translation 3"
"some_key_4", "Translation 4"
"some_key_5", "I am a very long line of text that has decided to cause an issue for the programmer, I have thus far laughed at his futile attempts to fix me."
Private Sub ImportFile()
Dim strEmpFileName As String
Dim intEmpFileNbr As Integer
Dim strTranslationKey As String
Dim strTranslation As String
Dim error As String
strEmpFileName = "C:\Files\test_file_1.asp"
intEmpFileNbr = FreeFile
Open strEmpFileName For Input As #intEmpFileNbr
Do Until EOF(intEmpFileNbr)
Input #intEmpFileNbr, strTranslationKey, strTranslation
Loop
End Sub
The code assigns the lines of text just fine until it gets to some_key_5, even tho there it thinks the text on a new line even tho it is a new line because of word wrap and not me hitting enter.
Is there any way around this? Shortening the line is not really an option.
I think the likely problem is the comma in the translation element. I'd suggest your best bet is probably to read the file with the
Line Input #my_file, my_string
statement and pick it apart by hand. See here for more.
You can use regular expression to match all strings inside "", and then put them into pairs.
As for line breaks, you can simply replace all of them to empty string before doing a regexp match.
Most likely you have a line termination issue, and you actully have a CR where it stops reading. I would suggest recreating the file using notepad, or looking at it with a text editor with a hex mode (that might tell you if you have a really long line as as well, if your real file has a line that exceeds a few K it might actually be a problem).
I ran your code against your supplied data, with a msgbox giving the length and right 10 characters of the string, and it worked. It also worked when I increased the field length of the second part to 721 chars.
I opted to use the file system object and it works just fine with the same data. It couldn't have been the comma, as it would attach the data from the file to the variable just fine, and include information past the comma. It would then add the item on the next line as a new key. I couldn't get past this, tried even removing the commas from the offending item and it would still break. But after I switched it to the file system object, it works just fine with the same data, i just have to manually split it.
Related
I'm honestly a novice on scilab.
I'm using print function to create .txt file with my character matrix in it.
But , when I open txt file, double quote appeared. I just want words without "".
This is how I'm using print
Compterendu(1,1)= "Medecin demandeur: "
fileresname= fullfile(RES_PATH, "compterendu.txt")
print(fileresname,Compterendu)
And, compterendu.txt was printed out like this.
Would be so grateful for any help!!
Thanks
Why do you use "print" ? After looking into the doc, yes, it is used to produce the same text as when you type the expression or the variable name on the command line. Hence it does print double quotes for strings. If you need something more basic use lower level i/o commands, like mputl.
S.
I've created a script file reader, nothing more than a glorified text reader that changes loop cases in my program, but I need it to be able to ignore comments on a line, execute that command, and go to the next line and process the new command after it finds the comment denoted with a semicolon. For the life of me, I can't figure out how to do this.
Currently, the commands are read in like this:
DO THIS FUNCTION
DO THAT FUNCTION
I'd like to comment it with a semicolon like this:
DO THIS FUNCTION ;this is a comment to be ignored
Below is my text file read code, should be able to drag and drop it in to test. The command indicator just echoes the command being read. I've removed the rest of my program, sorry, can't send that part.
Can someone shed some light?
Is a semicolon used anywhere else in your file? Or is it just used to indicate a comment?
If it is only used to indicate a comment then as you read each line in, call the Split String primitive and split at the ";". Just use the top output regardless of whether or not the line contains a semicolon:
You can use the "Match Regular Expression Function" to split up the string, as #Moray already suggested.
Sadly I can't give you an example vi right now.
The main idea is:
find the "Match Regular Expression Function"
give it a ; as char to search for
there are three outputs of the function (before match, match, after match)
use the 'before match' instead of the whole line and give it to the rest of your program
This only works if your commands don't contain any ; except for the comments.
Note: I not quite sure what happens if you give the function a string that doesn't contain ; but you can figure that out by yourself by using the detailed help to this function :)
I am generating a CSV file from a Microsoft SQL database that was provided to me, but somehow there are invalid characters in about two dozen places throughout the text (there are many thousands of lines of data). When I open the CSV in my text editor, they display as red, upside-down question marks (there are two of them in the attached screenshot).
When I copy the character and view the "find/replace" dialog in my text editor, I see this:
\x{0D}
...but I have no idea what that means. I need to modify my script that generates the CSV so it strips these characters out, but I don't know how to identify them. My script is written in Classic ASP.
You can also use RegEx to remove unwanted characters:
Set objRegEx = CreateObject(“VBScript.RegExp”)
objRegEx.Global = True
objRegEx.Pattern = “[^A-Za-z]”
strCSV = objRegEx.Replace(strCSV, “”)
This code is from the following article which explains in details what it does:
How Can I Remove All the Non-Alphabetic Characters in a String?
In your case you will want to add some characters to the Pattern:
^[a-zA-Z0-9!##$&()\\-`.+,/\"]*$
You can simply use the Replace function and specify Chr(191) (or "¿" directly):
Replace(yourCSV, Chr(191), "")
or
Replace(yourCSV, "¿", "")
This will remove the character. If you need to replace it with something else, change the last parameter from "" to a different value ("-" for example).
In general, you can use charmap.exe (Character Map) from Run menu, select Arial, find a symbol and copy it to the clipboard. You can then check its value using Asc("¿"), this will return the ASCII code to use with Chr().
I have an issue about getting the same result in the output txt file that I get applying the sub method in a string. So the thing is when I apply the following code in a single string I get the \n before the capital letter in the middle of the string:
line3= "We were winning The Home Secretary played a
important role."
line3.sub(/(?<!^) *+(?=[A-Z])/, "\n")
=> "We were winning\nThe Home Secretary played a\n important role."
But if I apply the following code the txt file that I get doesn't have any \n before the capital letter.
old= File.readlines("Modificado word.txt")
second= old.join
third= second.sub(/(?<!^) *+(?=[A-Z])/, "\n")
new= IO.write("new.txt", third)
I've tried multiple ways of encoding(surely in the wrong way) because I thought the the issue might be there but any of them worked. Even the gsub, but didn't work either.
Ok, I've got the solution, I don't know why but the type of encode of the txt file is in a format that the readlines command is not even able to read, so I copied all the content in another txt file which should be created from scratch and it worked :)
I'm trying to grab data from an AS400 screen & populate an email using that data but seem to have bumped into something I'm struggling to overcome. Here's a slice of what I have so far:
Dim polNo
polNo = GetText(10,18,10)
Dim wsh
Set wsh=CreateObject("WScript.Shell")
subSub1_()
sub subSub1_()
// Just doing this to check the text I have
SendKeys(polNo)
// Sent the eMail with the text
wsh.Run "mailto:testing#somemailbox.com?Subject=" & polNo
end sub
With the above, the resulting email subject line takes only the first word upto the first space. From what I've found, this is a parsing issue & have discovered the following line that should help.
polNo = Chr(34) + Replace(polNo,chr(34),chr(34)&chr(34))
The above line places all of the text in quotes (I know this because my SendKeys line now shows the GetText result with a " at the start.
The issue is when I reach the mailto line as Outlook pops up a window saying:
"The command line argument is not valid. Verify the switch you are using."
My end result will be an email that has a subject & a body with text taken from various parts of the screen.
Solved: Thanks to dmc below, he started me on the right line.
However, the solution was not to use Chr(34) but to use something as simple as:
polNo = Replace(polNo," ","20%")
Although it might not look like it, you're constructing a URL. As such, the contents of that URL must be URL Encoded. Certain characters can't be included in a URL, including a space. Those characters are represented with a percent sign followed by the ASCII code of the character in hexadecimal. For example, a space is changed to %20.
See the link below for a VBScript routine that will URL encode and decode strings.
http://www.justskins.com/forums/wsh-equivalent-of-server-38778.html
Edit: Although this is commonly known as URL encoding, the thing you're constructing is technically a URI. Wikipedia has a good page that explains further.