Simple things in life make more meaning, than waiting for big things to happen. Small things allow us to enjoy, large things change direction. This blog contains the small things in my life.
Thursday, January 01, 2015
Thursday, July 17, 2014
Lesson 9 : Some fun
1729 is known as Hardy Ramanujam Number. (There is a wiki article here). Reading the article all the way down, I read an interesting part that the Japanese Scientist Masahiki Fujiwara found out.
1729 –> add the numbers and you get 1+7+2+9=19
Reverse 19 and you get 91. 19x91=1729. According to him only 2 more numbers other than 1729 show this property. One is 81 and other is 1458. I decided to write a small piece of code to verify this.
| for x=10 to 9999 |
| C:\scripts>nos.vbs //nologo C:\scripts> |
And yes there are only 3 numbers.
Monday, May 12, 2014
Lesson 8 : Solution to the question
Question was to modify the code in the last lesson, where
- the comma could be replaced by tab, for making it easy to read
- to add the columns and show the total.
In the following code, it takes two parameters, first is csv file, and second is col. The same old file is used. The csv file first
| C:\scripts\file-io>type sample.csv |
The output of the program
| C:\scripts\file-io>03-addcols-csv.vbs sample.csv 4 //nologo Sum of Col 4 is 2121 |
And finally the code
| Const ReadMode=1 |
What more can be done.
a. You can validate if the column selected exists and if it does exists is it a numeric value.
b. You can check if the csv file passed as a parameter exists in the first place.
Lesson 8 : Working with CSV files
The fun starts working with CSV files. CSV files are comma separated value, files. Basically that means there are commas between the columns. So who creates them? Almost every application that creates logs, dumps performance data, configuration, etc. Before XML files became the craze CSV ruled the administrator’s world.
Below is a sample CSV file
| abcd,12,E_123,300 |
Let us write a code that can display any column we like. If we pass the parameter 0 (zero) we get the entire file. If we pass a specific number we get that column. If we pass a number more than the columns we show the entire file.
First the code
| 'Show the selected coloumn Const ReadMode=1 |
And now the output
| C:\scripts\file-io>02-show-contents.vbs sample.csv 0 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 1 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 2 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 3 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 4 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 5 //nologo C:\scripts\file-io> |
Some exercises for you to try.
1. Suppose we need the sum of a column (like column 2 or 4) then modify the above code so that it works in the following way.
c:\>progname csvfile col
The output should a tab between the cols and not a csv and a sum below.
Friday, May 02, 2014
Lesson 7 : Working with Text files
In our routine as an administrator, we routinely work with text files. Let us try to understand how to work with them.
A usual logic would be
- Open the file, Loop line by line, display the contents (or process them)
Example 1:
In this example, we try to display the contents of the file that is passed as a parameter at the command line. I have not done validations and assume that a file name is already there. You can add three validations, first check if there is an argument. Second check if the file exists. Third check if it is a text file. I have a sample file called “sample.txt” in the same folder as the program.
| Const ReadMode=1 set fso=CreateObject("Scripting.FileSystemObject") do until fstext.AtEndOfStream |
Let us check the output
| C:\scripts\file-io>01-show-contents.vbs sample.txt //nologo C:\scripts\file-io> |
Let us make it more useful. At the end, let us say we want to display the total lines in this file.
| Const ReadMode=1 do until fstext.AtEndOfStream |
| C:\scripts\file-io>01-show-contents.vbs sample.txt //nologo C:\scripts\file-io> |
What if we needed the lines and words.
a. Use the split function, it splits a string and returns an array
b. use the ubound function to check how many elements are there in the array, and add 1
| Const ReadMode=1 do until fstext.AtEndOfStream |
Sample Output of the same file
| C:\scripts\file-io>01-show-contents.vbs sample.txt //nologo C:\scripts\file-io> |
Let us make it more complete. We do the following changes, we pass the parameter filename with a number, 1=show file, 2=show lines, 3=show words, 4=show all
Code
| Const ReadMode=1 do until fstext.AtEndOfStream |
Various Runs
| C:\scripts\file-io>01-show-contents.vbs sample.txt 1 //nologo C:\scripts\file-io>01-show-contents.vbs sample.txt 2 //nologo C:\scripts\file-io>01-show-contents.vbs sample.txt 3 //nologo C:\scripts\file-io>01-show-contents.vbs sample.txt 4 //nologo C:\scripts\file-io> |
Thursday, May 01, 2014
Lesson 6: Functions and Procedures (vbscript)
Functions and Procedures make your code modular. They help you to keep repeated statements as blocks of code that can be reused. Both functions and procedures do the same thing, except, Functions can return value. So as a good practice never let your functions have any statements to print(display) or get input from keyboard.
Let your functions take parameters and return the value. How does a function return a value? Function_name=value you want to return. Enough of theory.
This program finds the value of (a+b) squared.
(a+b)2 =a2 +2ab+b2
| ‘File-0105-1.vbs (this is file name I have) Function Sq_ab(a,b) Sub Print(str) 'Main program starts here a=int(wscript.arguments(0)) |
Sample Output
| C:\scripts>0105-1.vbs 2 3 //nologo C:\scripts>0105-1.vbs 7 9 //nologo C:\scripts> |
Exercises for you.
1. Write functions for (a-b)2, a2-b2, (a+b)3 and so on
Saturday, April 19, 2014
Windows Scripting. Lesson 5. Magic Square
A magic square is one where the total of rows, columns and diagonals is the same.
Take a 3x3 magic square.
The Rules are pretty simple. The rule is called top-left rule.
1. Start with 1. Put it in the centre of the 1st row. So 1 is in (1,2) Row 1, Col 2.
2. Goto top left. In this case top is row 0, so top comes to the 3rd row. Left is row 1. So the next number is (3,1).
3. Continue with top left. Top of 3 is 2, left of 1 is 3rd column. So we are in (2,3).
4. Now the top left from 2,3 is 1,2. This place is occupied, you have a 1 there. So you need to go back to original location (2,3). Then come a place down but same column. So from 2,3 we come to 3,3. Put a 4 here. Continue this way.
5. In this case the totals of rows, col and diagonal is 15. Why 15?
6. We use the natural numbers 1 to 9. Sum of 1 to 9 is n(n+1)/2. In this example we have 9x10/2=45. We have 3 rows. So we get 45/3 or 15.
| sub Print(str) ' Main Program starts from here x(1,2)=1 |
- print procedure makes the printing easy.
- dim, defines an array. In this case it is an array 3 rows x 3 cols. Each value in the array is defined as 0. 1 is put then in the first row and second column.
- for loop rotates eight times for the eight numbers. a and b are the locations needed. We try top left, and if it is occupied (>0), then we go back to the original location and come one col down.
- the for loop then prints the 3 coloumns. Each row is formatted is by adding a tab (chr(9). When you compile and run this program, (magic3.vbs)
| C:\scripts>magic3 //nologo C:\scripts> |
Can we make a generic version of this program. Where we can print for any odd number. Since vbscript does not allow to define an array with a variable, I have defined a large array (20,20) and allow to print a magic square. The beauty is you can pass the number as a parameter. If the parameter is even, then we add 1 and make it a odd number. The program is very similar to the previous program.
| sub Print(str) sz=int(wscript.arguments(0)) x(1,(sz+1)/2)=1 |
Sample output.
| C:\scripts>magicgen 5 //nologo C:\scripts>magicgen 7 //nologo |
An exercise for you. Can you use the n(n+1)/2 formula as mentioned earlier and print one line at the end of the program.
Sum of Rows/Col is _______
Like this
| C:\scripts>magicgen 5 //nologo |
Friday, April 18, 2014
Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 4–FileSystemObject Contd..
Continuing on FileSystemObject, Let us look at and example to extract the properties of a file. There are some new things introduced in this example
1. SUB. This is for a procedure. you can pass parameteres just like functions but just that you cannot return any values. I have put a simple procedure called Print, that takes a string as a parameter.
2. Parameteres. You can pass parameteres to a vbscript. Wscript.argument, is where you can see the list.
| Sub Print(x) |
A few examples
| C:\scripts>folder.vbs c:\53-Photos\ar-1.jpg Path C:\53-Photos\ar-1.jpg C:\scripts>folder folder.vbs Path C:\scripts\folder.vbs C:\scripts> |
You can try an example to show the file size in MB or GB depending on the size of the file. Or may be list an output like the DIR command.
Tuesday, April 15, 2014
Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 3–Objects
After the basics of variables, Selection and Iteration statements, let us do something useful in real life. We start with the first object FileSystemObject. This gives us access to the filesystem. An essential part of scripting. We may need to create files, modify content, look for their existence. The idea is to make you familiar with using objects as well.
Let us take an example
| Set fs = CreateObject("Scripting.FileSystemObject") |
Save this script as fsdrives.vbs
Run it
| C:\scripts>cscript fsdrives.vbs //nologo C:\scripts> |
I have three drives on my laptop. Your machine may have more or less. Let us understand the code.
Line 1 is creating an object of FileSystemObject type. Without this you cannot use what is in this object. An object is contains methods (functions) and variables. The variables may be simple variables or may be collections. Collections are more like an array, you can use a for loop to go through each of the items, and then access the properties of each.
fs is an instance of the FileSystemObject
fs.drives is referencing a property of the FileSystemObject
fs.drives returns a collection of drives.
To loop through it we need a for loop. dc is the drive collections we have used. You have a collections of drives. For each drive there are many properties. We are referencing one of the property called driveletter.
Let us make the above program more richer. We not only need the drive, but also want to know the capacity and free space.
| ' Main Code Starts here Set fs = CreateObject("Scripting.FileSystemObject") ' We have explored, volumename, filesystem, totalsize, freespace |
Copy and paste the code. Run it.
| C:\scripts>cscript fsdrives.vbs //nologo Number of Drives 3 C:\scripts> |
That is the output I get. Your challenge would be to fine tune the code further so that the numbers are aligned below each other. On your machine you may see more or less drives and the numbers different.
Monday, April 14, 2014
Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 2–Solution Part 2
Fibonacci Series Solution
| wscript.echo "Fibonacci Series" |
Output
| C:\scripts>cscript fibo.vbs //nologo |
An interesting video on Fibonacci Series can be viewed here.
Sunday, April 13, 2014
Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 2–Solution
The following is the solution of the question in the last post. Printing the prime numbers between 1 and 100. This may not be the best logic, yet it gets what to be done. It is more important to develop a timely solution than an untimely one.
| wscript.echo "1" |
and the output
| C:\scripts>cscript prime.vbs //nologo |
Some more examples that you should try
1. List the Fibonacci series – first 15 numbers
2. Find the Square root of any number correct to 4 decimal places. (use Newton’s method)
Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 2
In any programming language there are three things we need to learn at the earliest.
1. Types of variable
2. Selection Statements
3. Iteration or Loops
Since ours is a “get ready quick” course. We will only try what we think is essential.
1. Variable.
There are no types of variables. What ever you assign the variable takes that values. Unlike in other programming languages you have int, char, fload, date etc. There is nothing here. You can just put any value in any variable. The upside is variable usage is easy. Same variables can be recycled. The downside is explicit conversions are needed everytime a variable of a specific type is needed.
Assume you have a program with the following
| i=5 |
When you run it there is no error.
2. Selection statements.
Keeping it simple.
if <condn> then
<statements>
else
<statements>
| randomize |
randomize – resets the seed, so that you get a real random numbers all the time. rnd generates a random number between 0 and 0.999xxx. The int function converts the output to an integer (we don’t need the decimals). The mod is a operation to find the remainder.
The above program prints either even or odd depending on the random number. In the example the program is saved as third.vbs
| C:\scripts>cscript third.vbs 7 C:\scripts> C:\scripts>cscript third.vbs 1 C:\scripts>third 0 C:\scripts>cscript third.vbs 6 C:\scripts> |
3. Iteration or Loops
The most convenient loop is the for – next.
An example
| wscript.echo "Program 2" |
A simple program the prints, from 1 to 5. And then random numbers between 0 and 9.
| Program 2 |
Try this exercise.
Print the first 100 prime numbers.
use the concepts learnt.
Friday, April 04, 2014
Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 1
Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 1
Good if you know the old Visual Basic
1. How to write your first script. (lets call this as first.vbs)
1. run cmd.exe
2. create a folder called c:\scripts
3. run notepad first.vbs
4. Save and run this file as follows
5. Try to run the file without using cscript and see what you get
6. This is because by default vbs files are executed using wscript
7. what if you wanted to change the default. Do the following
8. What if you wanted to change back the default to wscript
wscript //H:wscript
-----------------------------------------------------------------end of lesson
Monday, December 23, 2013
Even Browsers can give your location
Yes. If you are on facebook, or for the matter on any web site, and worry about your privacy, which you should then what to do?.
The first thing that you can or should do is, disable your location. Let your browser not know your location. How do get this done on three main browsers.
1. Firefox. (works on Ver 26)
a. Open a window and type about:config
b. Agree to the warning.
c. Search for geo.enabled and Set it as false. Look in the image.
2. Google Chrome
a. Open a windows and type chrome://settings
b. Click on Advanced Settings
c. Under Privacy click on “Content Settings”
d. Scroll down and choose “Location Setting” and select
3. Internet Explorer
a. goto Tools->Internet Options
Select the “Privacy Tab”
And check the box as shown above.
And now surf the internet atleast not worrying about your location is tracked. What next?
Monday, December 09, 2013
Lessons : The honest truth about dishonesty.
On my recent travel I read this book. Very relevant and lots of research gone into it.
You can click on the book to goto Amazon.
Dishonesty, that we hardly think about in our daily lives is sometimes very subtle. I look back and find that in my life as well there are cases where it can be seen. For instance taking more than an hour for lunch, or sometimes taking a long tea break. Also the research done in the book gives insights on how we can ensure practices that can reduce dishonesty in our work place.
A worth read.
Monday, November 18, 2013
Cloud Storage. What to look for.
Every one talks cloud. Let us look at Cloud Storage. What it means? You store your data at a third party place. Why? May be lower cost, administration simplification, security, protection etc. All these things encourage us.
Where are the risks? Like all business a cloud business can fail too. What if your cloud company is the single point of failure. What happens then?
a. Your data sits on the cloud. Your cloud provider closes shop. Do you have enough time to get it back? Even you have that time, do you have enough space to get it back. Even if you can get it back will the provider have enough bandwidth for all those who want their data?
b. Suppose you got back the data, you also somehow managed to secure some storage, will your applications work? Who will make changed to them? Can you keep your RPO?
So then what is the solution? May be in a next post.
Tuesday, July 02, 2013
Bye Bye Altavista
Come 8th July and another search engine altavista is going to leave cyberspace. Altavista has been one of the first search engines, indexing millions of pages. It was a product that too ahead of its time. Some how through companies buying each other, it came with Yahoo. Now Yahoo has decided to stop this product.
Surprising but in my two decades in IT I have started to see even good products reaching their end of life. Have a look at this page as of today.
From 8th July the domain name www.altavista.com will redirect to yahoo.com.
Source : BBC
Sunday, June 30, 2013
Google Reader
After all the requests, signatures, announcements, one of the very good service from Google, the Google Reader will be retiring tonight. When I logged in today I got this message,
Felt sad to see this. I had been a regular of this since many years. So finally I logged in today and deleted all my feeds. Make the life of Google a bit easier.
I hope Google changes its mind. And hope that other of my favorite services like GMail, Search Engine, Google Maps, Google Earth, Google Documents also do not face the same fate as the Reader.
Saturday, June 29, 2013
Get Start Button in Windows 8
One of the notably sad omission in windows 8 is the start button. The 8.1 version had lots of promise. Bloggers got greatly impressed with it when they saw the preview. The actual working was never published. What happened when 8.1 was released? The magical start button never came. The one put is practically of no use.
So how do we get the start button back. A genuine start button. One that works the same way it worked in Windows 7 and earlier versions. The best and free among the pack is Classic Shell.
So what happens after you install the Classic Shell. It should look something like this.
Where do you download it from?
a. From here
A normal setup.exe, next, next, finish. And you have the shell.
A thanks to the developers of classic shell.
Get back productive, and get back fast.
Monday, March 04, 2013
Allway sync
The amount of data that we are storing is on the rise. A normal household owning a computer easily has a TB of data. You just add up the hard disks, USB sticks, DVDs and CDs. Loss of data can mean a great loss.
What is the easiest way to sync your data. Say for instance you want to sync your internal disk to an external USB hard disk.
Of the many software one I like is AllwaySync. Easy to use. Download and within minutes you are on the go.
Some pieces of advice.
1. Keep your hard disk organized. I mean decide a directory structure and maintain it. For instance you can have folders like the following that I have on my disk.
I always like to begin with numbers. That way the organization is well and easy to understand. Alphabetical order is just a waste.
2. Keep the same structure on all your hard disks. So you need to thus create one plan
3. Keep atleast sync to two USB disks. This way you keep three copies. One on your local hard drive, two external copies. Sync them say every Friday. Since they have the same structure, It is just a single click.
So next time there should be no way to lose data.
Happy Syncing.