onsdag den 23. september 2009

Hello Virtual PC Part I

Today I'll start a larger hello, and its not scripting. It's a setup. The setup of a Virtual PC testing environment.

 
Lets start with the basics - downloading and installing virtual PC. You can find it here: http://www.microsoft.com/windows/virtual-pc/ - and if not.. then google it. Run the installation and follow the instructions.
After the installation I recommend that you  alter the keyboard settings. Expecially if you are danish. To do that just follow File|Options which leads to this dialog.

 
This is the first part of making a test environment.. But don't worry it gets more exciting.

 
Loopback network
We'll need a loopback network. I have grabbed this from Microsoft support.
To manually install the Microsoft Loopback adapter in Windows XP, follow these steps:
  • Click Start, and then click Control Panel.
  • If you are in Classic view, click Switch to Category View under Control Panel in the left pane.
  • Double-click Printers and Other Hardware, and then click Next.
  • Under See Also in the left pane, click Add Hardware,and then click Next.
  • Click Yes, I have already connected the hardware, and then click Next.
  • At the bottom of the list, click Add a new hardware device, and then click Next.
  • Click Install the hardware that I manually select from a list, and then click Next.
  • Click Network adapters, and then click Next.
  • In the Manufacturer box, click Microsoft.
  • In the Network Adapter box, click Microsoft Loopback Adapter, and then click Next.
  • Click Finish.
http://support.microsoft.com/kb/839013

Directory structure
I think the best structure for the environment is as follows. I use c:\vpc\ as the virtual pc root, and under this I have
  • baseimages
  • configuration
  • install

torsdag den 17. september 2009

Hello file

Lets try something a little more interesting. Files.

1. Make a vbs-file and add these lines

Option Explicit


' Declare the variables
Dim oFileSystemObject
Dim oTestFile

Set oFileSystemObject = CreateObjec("Scripting.FileSystemObject")

' Write to the file
Set oTestFile = oFileSystemObject.CreateTextFile("C:\HelloWorld.txt")

oTestFile.WriteLine "Hello World!"

oTestFile.Close
set oTestFile = nothing

' Read from the file
set oTestFile = oFileSystemObject.OpenTextFile("C:\HelloWorld.txt")

While not oTestFile.AtEndOfStream
wscript.echo oTestFile.Readline
wend
oTestFile.Close
set oTestFile = nothing
set oFileSystemObject = nothing

2. Save and run the file.

You should see a dialog with Hello World! Furthermore you should also could see a newly created textfile which contains Hello World!

Now, lets see how this works:
First we instantiate a Scripting.FileSystemObject, which is a standard ActiveX component. It can be used to a lot of the basic filehandling commands. Copying, moving, deleting, creation etc.
We then use its CreateTextFile method to instantiate a textstream object (oTestFile). That object can be used to write lines to the file.
Then we close the file so we can open it again and finally we get rid of the object from the memory by setting it to nothing.

To get som output, we use the oFileSystemObject to open the file again into the oTestFile textstream. We use the ReadLine method until the file is at its end to write all the lines in the file. And again close it and get rid of the objects.

A little tip you can try is to write more lines in the file and see that it reads them all.

Until next time.. Say hello to the world from me.

søndag den 13. september 2009

JScript

The windows scripting host also supports javascript. This is also an easy way to script your computer.
Same procedure as before.
1. Make a textfile and rename it to HelloWorld.js
2. Add this line (notice, that JScript is case sensitive)
WScript.Echo("Hello World!");

3. Save the file and doubleclick

Voilá your third Hello World

fredag den 11. september 2009

VB-Script

Now to another easy way to script your computer - throug vbscript. VB stands for Visual Basic, and perhaps you have heard about it.

Again I will start with a very easy script.
1. Make a file - the same way as in the first blog, but this time it should be renamed to HelloWorld.VBS
2. Edit the file and add these lines

Option Explicit
Wscript.echo "Hello World!"
msgbox "Hello world!"

3. Save the file and doubleclick.

You should see two dialog boxes on your screen with "Hello World!" and you might wonder why I showed you both methods, but it has a very good reason.

Try the following:
1. Click StartRun and write "wscript \helloworld.vbs" where path is the path to the file.. (TIP: You can drag the file to the textfield) - the same thing happens.
2. Click StartRun and write "cscript \helloworld.vbs" - notice what happens.

The wscript.echo checks if its running in a commandline or a windows environment and writes the text in the right environment. Msgbox allways uses a dialog. If you dont have a chance for user interaction, you do not want a dialog box that stops the script.

I haven't mentioned the first line (Option Explicit) yet. Actually it's not important in this example, but I personally thinks it's a good thing to learn from the start. When you put Option Explicit in the start of a vb-script, the interpreter demands that all variables are declared. If you don't understand this right now - don't worry, i'll get back to it.

Hello world

Hello world is typically the first testprogramme you write to see how a tecnique works and this blog is dedicated to first testprogrammes in all kind of languages some of them are easy and others might be a bit more complecated.
If you have ideas to a good blogpost then write me.
But lets get startet.
The first "Hello world" is a batch script - very very simple.
1. Make a .bat-file. The easiest way is to rightclick in a directory and pick New -> Text Document . Rename the file to HelloWorld.Bat.








notice: if you dont see the .txt extension in the filename you have to setup windows to see known extensions.
2. rightclick the file and select edit
3. in the fileeditor (typically notepad from standard) write following:
@echo off
echo Hello World
pause
4. close the editor and doubleclick the file


And now.. What actually happens.

The first line @echo off actually tells the system not to write anything unless you tell it to. The @-sign is only there to keep it from writing that line.. Confusing? Well maybe :-)

The second line echo Hello World tells the system to write Hello World.

The last line - pause tells the system to wait for a userinput.


Your first Hello World .. here you go :-)