Hello everyone I made a simple program that takes my external IP and places in a my websites public camera. And I got a problem - The program is making a txt file with the ip inside it and uploads it to the server.When the program is overwriting/editing/creating the file its adding an empty new line which messes up my PHP code...
This is the code used for both overwriting/editing and creating the file
Dim strFile As String = "c:/IPtoUse.txt"
Dim fileExists As Boolean = File.Exists(strFile)
Using sw As New StreamWriter(File.Open(strFile, FileMode.OpenOrCreate))
sw.WriteLine( _
IIf(fileExists, GetIP, GetIP))
End Using
(the GetIP function is getting my ip from my server)
This ends up with another empty line. How can I fix it?
Thanks!
Going on the information from the question and comments, it seems that your file will end up with an additional linefeed at the end in both cases (ie. both for new and modified files).
The reason for this is that you're using the WriteLine method, which will append a newline at the end of the text it writes, even if that text already ends with a newline.
Simply change the code to use the Write method instead of the WriteLine method and you should end up with a file that contains only the text passed to the method.
Related
I'm trying to get Word to fill in cells in a table. The script works when run as a macro from within Word, but fails when saved as a .vbs file and double-clicked, or run with wscript. This is a part of it.
set obj = GetObject(,"Word.Application)
With obj
With .Selection
MsgBox .text
If (.Information(wdWithInTable) = True) Then
.Collapse Direction:=wdCollapseStart
tCols = .Tables(1).Columns.Count
tRow = .Information(wdStartOfRangeRowNumber)
tCol = .Information(wdStartOfRangeColumnNumber)
For I = 2 To 5
.Tables(1).Cell(tRow, I).Range.Text = "fred" & Str(I)
Next
` now make new row
For I = 1 To tCols - tCol + 1
.MoveRight unit:=wdCell
Next
End If
End With
End With
I have three problems. First, it won't compile unless I comment out the .Collapse and .MoveRight lines. Second, although the MsgBox .text displays the selected text, I get "out of range" errors if I try to access any .Information property.
I'm sure I'm missing something very simple: I usually write software for Macs, and I'd do this using AppleScript. This is my first attempt at getting anything done under Windows.
VBScript and VBA are different languages.
They are a bit similar, but not very. Moreover, VBScript is not like AppleScript; it doesn't let you easily interface with running programs.
The interfaces you'll get from VBScript can behave subtly differently in VBA and VBScript. However, I think you've got two problems here:
:= is invalid syntax in VBScript; you'll need to find an alternative way of calling the function. Try just using positional arguments.
You've no guarantee that this will open the expected file; there could be another instance of Word that it's interacting with instead.
Since your code is not running within the Word environment it would require a reference to the Word object library in order to use enumeration constants (those things that start with wd).
VBScript, however, cannot work with references, which means the only possibility is to use the long value equivalents of the enumerations. You'll find these in the Word Language References. Simplest to use is probably the Object Browser in Word's VBA Editor. (In Word: Alt+F11 to open the VBA Editor; F2 to start the Object Browser; type in the term in the "Search" box, click on the term, then look in the bottom bar.)
The code in the question uses, for example:
wdWithInTable
wdCollapseStart
wdStartOfRangeRowNumber
wdStartOfRangeColumnNumber
wdCell
The reason you get various kinds of errors depends on where these are used.
Also, VBScript can't used named parameters such as Unit:=. Any parameters must be passed in comma-delimited format, if there's more than one, in the order specified by the method or property. If there are optional parameters you don't want to use these should be left "blank":
MethodName parameter, parameter, , , parameter
I have an input file and a batch file. When the batch file is executed using the System command,
a corresponding outfile is generated.
Now I want a particular text (position 350 to 357) from that outfile to be displayed on to my lineedit widget
Here is that part of my code:
system("C:/ORG_Class0178.bat")
Now the outfile will be generated
File.open("C:/ORG_Class0178_out.txt", 'r').each do |line|
var = line[350..357]
puts var
# To test whether the file is being read.
#responseLineEdit = Qt::LineEdit.new(self)
#responseLineEdit.setFont Qt::Font.new("Times NEw Roman", 12)
#responseLineEdit.resize 100,20
#responseLineEdit.move 210,395
#responseLineEdit.setText("#{var}")
end
When I do test whether the file is being read using puts statement, I get the exact required output in editor. However, the same text is not being displayed on LineEdit. Suggestions are welcome.
EDIT: A wired observation here. It works fine when I try to read the input file and display it , however it does not work with the output file. The puts statement does give the answer in editor confirming that output file does contain the required text. I am confused over this scenario.
There is nothing wrong with the code fragments shown.
Note that var is a local variable. Are the second and third code fragments in the same context? If they are in the same method, and var is not touched in-between, it will work.
If the fragments belong to different methods of the same class, than an instance variable (#var) will solve the problem.
If all that does not help, use Pry to chase the problem. Follow the link to find the pre-requisites and how to use. Place binding.pry in your code, and your program will stop at that line. Then inspect what your variables are doing.
try 'rb' instead of 'r'
File.open("C:/ORG_Class0178_out.txt", 'rb').each do |line|
var = line[350..357]
puts var
I have a ruby function that accesses files in my unix filesystem.
I have 2050 files each representing an hashed value in a dedicated directory.
The function reads a file containing email addresses and performs a hashing function, finds out the file id and prints.
Usually I do those things in Java but I wanna start doing it in Ruby. My problem is, that within my function, I try to open the correct file for reading, but I see that open works the same as new when no code block is provided. From the IO class, method ::openWith no associated block, IO.open is a synonym for ::new.
What I simply need to do is, open the file, set the reader pointer to the first available line, write and flush.
For simplicity I will put every code statement in one line. The file should be opened with its current status (see the HERE comment).
def dispatch
while (id=IDS_FILE.gets)
bucket="#{BUCKETS}" << (PERFORM HERE THE HASH CALCULATION) ".txt"
#HERE
bucket_file=File.open("#{bucket}","w")
bucket_file.write(id)
bucket_file.close
end
log "Writing #{id.chomp!} to #{bucket_file.to_path}"
end
end
To get "the file to be opened with its current status" you'll need the "append mode", as documented here:
"a" Write-only, starts at end of file if file exists,
otherwise creates a new file for writing.
So your code should read like so:
File.open(bucket, "a") do |f|
f.write id
end
I have a variable called random number that needs to be stored when the application has be shutdown or the computer has been shutdown. Every time this number is used I also need to +1 to it.
I have a few variables in my current vb6 application that need to be saved when the app is closed and loaded when the app is launched. Is this possible? I could use a text file or a config file to store the variables?
EDIT -------------------------------------------------------------------------------
I managed to fix this problem had just using a simple input and output text file. Please read my answer below if you have the same problem and need assistance.
The standard way to save values in VB6 apps was to use INI files. If I remember there are a couple of Win32 functions to read/write them.
They are GetPrivateProfileString and WritePrivateProfileString.
Using the registry is the correct way to do it.
VB has built in functions SaveSetting and GetSetting for writing to and reading from the registry.
See registry tutorial or Stack Overflow question to help you out.
I managed to complete the task by creating a file in my C Drive and putting in the number "123" to the top line of the text file. I then wrote the following code:
Function GetPOIRandomNum()
Dim LineA As String
'Collect stored variables
Open "C:\TestPartner\Config\POIRandomNum.txt" For Input As #1
While Not EOF(1)
Line Input #1, LineA 'Read the first line in the file
POIRandomNum = LineA + 1 'Give POIRandomNum the integer from line 1 and add 1 to it
Wend
Close #1
'Save the new random number variable to the file
Open "C:\TestPartner\Config\POIRandomNum.txt" For Output As #1 'Open for output to replace the old number
Write #1, POIRandomNum 'Input the new number to the text file
Close #1
End Function
Now whenever the Random number Variable is needed I call the above function.
Here is the situation, I have this string table, in a .res file and I have some strings loaded into one of the forms, say Form1. On a form on I want to popup a message box with a message loaded from the string table using LoadResString(1234).
Is it possible that when Resource ID 1234 contains "This is testing vbNewline This is a new line!." that string will be loaded using the said function onto the message box (popup box)?
I've tested it: It will also print out the "vbNewline" command and NOT parse it. Is there any other way to parse the said string making this kind of message ?
This is testing
This is new line!.
I wanted that kind of message to appear.
You are trying to put a VB constant in a String expression so it is treating it like text, you can try using the Replace Function ( I realize this is a .Net link but the signature is the same as the VB6 method) to remove your string and substitute the correct value something like this should work:
MsgBox (Replace(LoadResString(1234), "vbNewLine", vbNewLine))
or create a function like this:
Public Function ParseNewLine(value As String) As String
ParseNewLine = Replace(value, "vbNewLine", vbNewLine)
End Function
and call it like this:
MsgBox (ParseNewLine(LoadResString(1234)))
Why don't you embed the newline sequence into the RES file. If you are using the Resource Add-In, you can press Ctrl+Enter to insert these characters.
In visual Studio's resource editor and a raw resource script, you can use \n.