VBS If Then Else - vbscript

I'm new to VBS and I'm attempting to make a start on a UAC in Windows - A will to do and in turn, learn.
I don't understand why I keep getting a syntax error on the line before the last End If
If you could help, I'd be greatly appreciative!
CurPass = ReadIni("C:\Program Files\User Account Control\ident.ini", "pass", "pass")
InpPass = inputbox("Please enter your current password:")
If InpPass = CurPass Then
NewPass = inputbox("Please enter your new password")
if NewPass=CurPass Then
KpPass=msgbox("Your new password is the same as your old password. Keep old password?",4+32,"UAC")
if KpPass=7 Then MsgBox("Please try again")
end if
Else
RNewPass = inputbox("Please re-enter your new password")
end if
if RNewPass=NewPass then
WriteIni "C:\Program Files\User Account Control\ident.ini", "pass", "Pass", NewPass
else msgbox("Your new passwords do not match. Try again.")
end If
else msgbox("Incorrect password")
End if

There is no If .. Then for your last Else .. End If. This would be obvious immediately, if you had used proper indentation.
Update: Above diagnosis is wrong.
I think you want this structure:
CurPass = ""
InpPass = ""
If InpPass = CurPass Then
NewPass = ""
If NewPass = CurPass Then
KpPass = ""
If KpPass = "7" Then
KpPass = "4711"
End If
Else
RNewPass = ""
End If
If RNewPass = NewPass Then
RNewPass = "xx"
Else
RNewPass = "yy"
End If
Else
CurPass = "whatamess"
End If
Then VBScript lost its way when you wrote:
if KpPass=7 Then MsgBox("Please try again")
end if
You can have 'short ifs' like
If Condition Then OneStatementAction
but then there should be no End If. It's a bit like omitting {} for single line/statement conditionals in other C alike languages. That is: better avoided.

End IF is a expresion that tells to VB.NET that you done your Conditions.
Every IF may close with an END IF
so your code may be like:
If textbox1.text = "Cat" Then
Do something
Else
Do not do that thing
End If
Check your code again and avoid writing End IFs and then elses look:
If textbox1.text = "Cat" Then
Do something
End IF
Else
Do not do that thing
End If
// This will not work! You can t have more than one IF in a contition:
This is incorect:
IF something Then
IF blabla bla then
Else
Endif
See here a link where you can learn how to use therse:
http://msdn.microsoft.com/en-us/library/752y8abs.aspx

Related

Ruby - Create a class with a file as a variable - possible?

I need to create a program in which the user can take different tests. As i dont want to copy paste my code all over for every test, i have tried to setup a class for that purpose - but i have problems with this class.
Error message = undefined variables or method in 'display_test'
I have predefined som test as a .txt file
I want to choose the file in the class depending on what the user answer - is that possible?
Class code:
class Test
#correct_answers = 0
def display_question( question, options, answer )
puts question
options.each_with_index { |option, idx| puts "#{ idx + 1 }: #{ option
}" }
print 'Answer: '
reply = gets.to_i
if answer == reply
puts 'Correct!'
#correct_answers += 1
puts "#{#correct_answers}"
else
puts 'Wrong. The correct answer was: ' + answer.to_s
end
end
def display_test()
f = File.new(userinput, 'r')
while ! (f.eof?) #logikken til at splitte
line = f.gets()
question = line.split("|")
question[1] = question[1].split(";")
display_question question[0], question[1], question[2].to_i
end
end
display_test
puts "________________________________________________________"
puts "Total score:"
puts "You've got" + " #{#correct_answers}" + " correct answers!"
Before hand i have used ("geografitest.txt") instead of username in the File.new so it looked like this:
f = File.new('geografitest.txt','r')
But now i am trying to let the user decide what test to take.
I am very new to ruby, so please bear with me.
I have tried to do it this way, which obviously is not working.
puts "Which test do you want to take?"
select = 0
while (select != 3)
puts "Press 1 to take Geografi test."
puts "Press 2 to take Math test."
puts "Press 3 to take Religion test."
puts "Press 3 to exit"
select = gets.chomp.to_i
if (select == 1)
gets.chomp = userinput
userinput =`geografitest.txt`
echo $userinput
end
if (select == 2)
gets.chomp = userinput
userinput =`matematiktest.txt`
echo $userinput
end
if (select == 3)
gets.chomp = userinput
userinput =`religionstest.txt`
echo $userinput
end
if (select > 4)
puts "Not a correct selection"
elsif (select == 4)
puts "Goodbye"
end
end
abort
So my questions is now;
How can i make the user choose what test to take? Can i make a variable instead of the textfile as i have tried, but in a different way? Or is there a smarter way?
And in what way is my class wrong and how do i fix it? I know its not the way to make it, but i simple cant get my head around how to make it right.
Please help a rookie out.
Cheers!
You can pass file as dependency to you Test class based on user input with object constructor. Something like this
class Test
attr_reader :correct_answers_count
def initialize(file)
#file = file
#correct_answers_count = 0
end
#other code goes here
end
loop do
case user_input = gets.chomp
when '1'
file_name = 'some_file1'
when '2'
file_name = 'some_file1'
when '3'
break
else
puts 'wrong variant'
end
test = Test.new(File.new(file_name, 'r'))
test.display
end

Ruby | Trying to check password by comparing to a file

I have some troubles with my code using Ruby. its just for a terminal program, so no website or anything.
In my code I Will have the user create a login. Then I will have the user to login, but I cant seem to figure out how to check if password or username is correct.
The program should compare whatever the user types in as a username/password with the file (userdatabase) - I think i got that right.
Now I am trying to stop the user if the input is not found in the user database, using a while loop, but i cant seem to make that work.
Code:
puts "What will be your user name?"
username = gets.chomp
puts "What will be your password?"
password = gets.chomp
puts "Please repeat your password."
passwordsafe = gets.chomp
f = File.new("student.txt", "w+")
f.puts username + ";" + password + ";" + passwordsafe
f.close
puts "well done, you have created a new user."
lines = IO.readlines("student.txt")
lines.each{|line| print(line)}
puts "now you need to login."
puts "What is your username?"
username = gets.chomp
File.open("student.txt") do |f|
f.any? do |line|
while line.include?(username)
end
end
elsif puts "Sorry your username was incorrect"
end
#lines = IO.readlines("student.txt")
#lines.each{|line| (line)}
puts "what is your password?"
password = gets.chomp
The while is unnecessary.
The username check currently like this:
File.open("student.txt") do |f|
f.any? do |line|
while line.include?(username)
end
end
Could be like this:
File.open("student.txt").each_line.any? do |line|
line.include?(username)
end
Or collapsed to a single line like this:
File.open("student.txt").each_line.any?{ |l| l.include?(username) }
Check the docs on the any? method to make sure you understand what it's expecting: http://ruby-doc.org/core-2.4.2/Enumerable.html#method-i-any-3F
As for the rest of the syntax errors, I'll leave those up to you ;)

loop through json array and retrieve one attribute, gives errors also

i am new to programming in ruby, and i am trying to get the value of json['earning_rate_hr'] but i get an error, in '[]': no implicit conversion of String into Integer (TypeError)
i know and i understand the error, however this is not my main question here is my file :
checkingchecker.rb :
#require_relative '../lib/hackex/net/typhoeus'
require_relative '../lib/hackex'
require 'rubygems'
require 'json'
file = 'accounts1.txt'
f = File.open file, 'r'
puts "MADE BY THE PEOPLE, FOR THE PEOPLE #madebylorax"
puts ""
puts "--------------------------------------------------------"
puts ""
while line = f.gets
line = line.chomp.split(';')
email, password = line
puts "logging in as " + email
HackEx.LoginDo(email, password) do |http, auth_token, user|
puts "getting info..."
user = HackEx::Request.Do(http, HackEx::Request.UserInfo(auth_token))['user']
puts "receieved user info!"
bank = HackEx::Request.Do(http, HackEx::Request.UserBank(auth_token))['user_bank']
puts "recieved bank info!"
json = HackEx::Request.Do(http, HackEx::Request.UserSpam(auth_token))['spam']
puts "recieved spam info!"
puts json['earning_rate_hr'] #error line, the error is because this is an array, and it cant be turned into integer, i was wondering if there is a way to use puts on it without trying to make it an integer
userchecking = bank["checking"]
checking = userchecking.scan(/.{1,3}/).join(',')
puts email + " has in Checking: BTC #{checking}"
puts ""
puts "--------------------------------------------------------"
puts ""
end
end
i tried to do puts json, it puts items like this one :
{"id"=>"9867351", "user_id"=>"289108", "victim_user_id"=>"1512021",
"victim_ip"=
"86.60.226.175", "spam_level"=>"50", "earning_rate_hr"=>"24300", "total_earning s"=>"13267800", "started_at"=>"2015-11-01 07:46:59",
"last_collected_at"=>"2015- 11-24 01:46:59"}
what i want to do is select the earning_rate_hr for each one of them and add them together, however i do not have a clue on how to do that, since the error is not fixed and i cant get the value of it
ps : i tried turning it into a Hash, and i also tried using .first, but .first only shows the firs one, i want to show all of them, thank you
I know you from line messenger, I haven't used ruby codes in a long time and this one keeps giving me cloudflare errors, I'm not sure if its because of server downtime/maintainance or whatever but yeah anyway heres your script, enjoy farming ;) -LineOne
PS, I changed a few strings to make it look a lil cleaner so you can see the spam income easier, and added the sleep (1) because sleeping for one second before reconnecting helps to prevent cloudflare errors
also you don't need to require json or rubygems in your hackex scripts because its required in the library so its all covered pre-user-input/script
require_relative 'libv5/lib/hackex'
while 1<2
begin
print'Filename: '
fn=gets.chomp
file = fn+'.txt'
f = File.open file, 'r'
puts "MADE BY THE PEOPLE, FOR THE PEOPLE #madebylorax" #helped by lineone
puts ""
puts "--------------------------------------------------------"
puts ""
while line = f.gets
line = line.chomp.split(';')
email, password = line
HackEx.LoginDo(email, password) do |http, auth_token, user|
puts "Retrieving Info..."
puts''
user = HackEx::Request.Do(http, HackEx::Request.UserInfo(auth_token))['user']
bank = HackEx::Request.Do(http, HackEx::Request.UserBank(auth_token))['user_bank']
json = HackEx::Request.Do(http, HackEx::Request.UserSpam(auth_token))['spam']
cash_count=0
tot_count=0
json.each do |j|
earn_rate = j['earning_rate_hr']
total= j['total_earnings']
cash_count+=earn_rate.to_i
tot_count+=total.to_i
end
print "#{email}: current earnings: #{cash_count} per hour, Total earnings #{tot_count},"
userchecking = bank["checking"]
checking = userchecking.scan(/.{1,3}/).join(',')
puts " #{checking} BTC in Checking"
puts ""
puts "--------------------------------------------------------"
puts ""
sleep 1
end
end
rescue
puts"#{$!}"
end
end
Thats fine you can also calculate the total income of your farms by adding new variables at the top example a=0 then adding the number at the end a+=tot_count
This should help:
earning_rates = json.map{|e| e["earning_rate_hr"]}
puts "Earning rates per hour: #{earning_rates.join(" ")}"
puts "Sum of earning rates: #{earning_rates.map{|e| e.to_i}.inject{|sum, x| sum + x}}"

A basic "Helper" program not working as expected

I'm making a basic "Helper" program..
Anyway here's the code:
def sayHelp()
puts "------------List of help and commands-------------"
puts "Help-- Shows a list of commands."
puts "Start [PROGRAM] (PROGRAM ARGS)-- Starts the specified program."
return true
end
version = "1.0"
ccommand = ""
puts "Welcome to RubyBot " + version + "."
puts "------------------------------------"
sleep(3)
system "clear" or system "cls"
puts "Enter \"help\" for a list of commands."
puts "Please enter a command: "
ccommand = gets
if ccommand == "help"
sayHelp()
else
puts "Not right bro"
end
I go ahead and run this and enter help but it just chucks Not right bro up at me.. What am I doing wrong?
ccommand = gets
The string returned by gets has a trailing new line character, remove it and it will work:
ccommand = gets.chomp

How would I access a key from a hash that is within an array that is within a text file in ruby?

I'm trying to create an if/else statement that compares a user's input against all the name keys within a hash that's stored with a text file. How would I write this?
user_accts is the array.
Update of full if/else statement:
elsif choice == "2"
puts "====== NEW CUSTOMER ======"
puts "Choose a username:"
prompt; login_name = gets.chomp
#file.open("cust_accts.txt", "r")
#if #user_accts.map { |acct| acct["name"]}.include?(login_name)
if #user_accts.any? {|acct| acct["name"] == login_name }
puts "Sorry, that username has already been taken"
elsif
puts "Choose a password:"
prompt; login_password = gets.chomp
user_accts << create_account(login_name, login_password)
File.open("cust_accts.txt", "a") { |file| file.puts(user_accts)}
end
original if/else statement:
if login_name == #??? #user_accts.has_key?(login_name) ???
puts "Sorry, that username has already been taken"
elsif
puts "Choose a password:"
prompt; login_password = gets.chomp
user_accts << create_account(login_name, login_password)
File.open("cust_accts.txt", "a") { |file| file.puts(user_accts)}
end
This is exactly what is inputted to the cust_accts.txt file using this command:
user_accts << create_account(login_name, login_password)
File.open("cust_accts.txt", "a") { |file| file.puts(user_accts)}
cust_accts.txt
{"name"=>"Tom", "password"=>"popcorn", "balance"=>0}
{"name"=>"Avril", "password"=>"chain", "balance"=>0}
It's not quite clear what your starting point is.
Assuming you have parsed your text file into #user_accts, so you have:
#user_accts = [{"name"=>"Tom", "password"=>"popcorn", "balance"=>0},
{"name"=>"Avril", "password"=>"chain", "balance"=>0}]
Then you would want to do:
if #user_accts.map {|acct| acct["name"]}.include?(login_name)
puts "Sorry, that username has already been taken"
else
# ...
end
#user_accts = [{"name"=>"Tom", "password"=>"popcorn", "balance"=>0},
{"name"=>"Avril", "password"=>"chain", "balance"=>0}]
if #user_accts.any? {|acct| acct["name"] == "Tom" }
puts "Sorry, that username has already been taken"
else
# ...
end
#=> Sorry, that username has already been taken
It seems like your problem is "How do I store objects in ruby for use at a later date?". If you want to use a text file you need to put it into a format that can be parsed back into a ruby object like JSON or XML. I would recommend using a database though. A database like SQLite is very light weight and easy to learn. An advantage to using a DB is that when you use a DB you can set an option to have your results returned as a hash.
I had a similar problem, eventually decided that a DB was the way to go.

Resources