WPROWADZENIE DO INFORMATYKI


 

WHS - Windows Host Scripting

    Technologia skryptowa WHS wykorzystuje następujące obiekty (klasy obiektów:

 

 

W ramach technologii WHS możliwe jest tworzenie skryptów:

    -    JavaScript

    -    VBScript

Przykład I

//=================================

// Prosty skrypt: myscript.js

//=================================

WScript.Echo("Hi, how are you");

 

 

 

//=================================

// Prosty skrypt: myscript.vbs

//=================================

' najprostszy skrypt VBS, Hello world! Example script

MsgBox("Hello world!")

 

//========================================
// wypisz wszystkie wprowadzone argumenty 
//========================================
var objArgs     = WScript.Arguments;
var argsNamed   = WScript.Arguments.Named;
var argsUnnamed = WScript.Arguments.Unnamed;


for (i = 0; i < objArgs.length; i++)
{
    WScript.Echo(objArgs(i));
}

WScript.Echo("There are " + argsNamed.length + " named arguments.");
WScript.Echo("There are " + argsUnnamed.length + " unnamed arguments.");

 

//========================================
// wypisz wszystkie wprowadzone argumenty 
//========================================
Set objArgs = WScript.Arguments
WScript.Echo WScript.Arguments.Count
For Each strArg in objArgs
    WScript.Echo strArg
Next

 

//========================================
// start an application, feed key strokes
//========================================
var WshShell = WScript.CreateObject("WScript.Shell");

WshShell.Run("notepad");
WScript.Sleep(100);          // czekaj 100 ms
WshShell.AppActivate("Notepad");
WScript.Sleep(100);
WshShell.SendKeys("Hello, World");
WshShell.SendKeys("~");
WshShell.SendKeys("From WHS script");
 

//========================================
// create/read/delete a registry key
//========================================
var WshShell = WScript.CreateObject("WScript.Shell");

WshShell.RegWrite ("HKCU\\Software\\MyTest\\Name", "World", "REG_SZ");

var bKey = WshShell.RegRead ("HKCU\\Software\\MyTest\\Name");
WScript.Echo (bKey);

WshShell.RegDelete ("HKCU\\Software\\MyTest\\Name");
 

 

Root key Name Skrót
HKEY_CURRENT_USER HKCU
HKEY_LOCAL_MACHINE HKLM
HKEY_CLASSES_ROOT HKCR
HKEY_USERS HKEY_USERS
HKEY_CURRENT_CONFIG HKEY_CURRENT_CONFIG

Cztery możliwe rodzeje zmiennych podano poniżej:

Typ Opis Zmienna
REG_SZ A string A string
REG_DWORD A number An integer
REG_BINARY A binary value An integer
REG_EXPAND_SZ An expandable string
(e.g., "%windir%\\calc.exe")
A string

 

//=====================================
// copy/move/delete file
//=====================================
var FSO = WScript.CreateObject("Scripting.FileSystemObject");
FSO.CopyFile("c:\\old.txt", "c:\\saved\\");
FSO.CopyFolder("c:\\x", "c:\\y");
FSO.DeleteFile(DesktopPath + "\\test.lnk")
FSO.MoveFile(DesktopPath, MyDocumentsPath);


//=====================================
// read/write a text file
//=====================================

var FSO = WScript.CreateObject("Scripting.FileSystemObject");
fobj = FSO.CreateTextFile("c:\\testfile.txt", true);
fobj.WriteLine("This is a test.");
fobj.Close();
 


fobj = FSO.OpenTextFile("c:\\testfile.txt");
WScript.Echo(fobj.ReadLine());
fobj.Close();

 

 Przykład II

'=====================================

' przykładowe pobranie argumentów wprowadzonych przez użytkownika

'=====================================

 

    Dim Args, ArgList

    Args = WScript.Arguments

    For i =0 to Args.Count - 1

 

    ArgList = ArgList &i &":"&Args(i) & chr(13)

    Next 

 

    MsgBox "No. of arguments: " & Args.Count & chr(13) & ArgList

 

    Przykład III

    Wykorzystanie obiektu Dictionary

 

'=====================================

' utwórz obiekt Dictionary

'=====================================  

 

    Dim Dict ‘Create an instance

    Set Dict = CreateObject("Scripting.Dictionary")

    Dict.Add "a", "Athens" ‘Add some keys and items 

    Dict.Add "b", "Belgrade" 

    Dict.Add "c", "Cairo"

    Dict.Add "d", "Doncaster"

    Dict.Add "e", "Eastwood"

    key = InputBox("Enter a letter a - e")

    MsgBox "Item selected: " & Dict.Item(key)

 

    Przykład IV

        ' Zamień zawartość plików

    Function Update( source, target )

            Dim f1,f2,d1,d2,c1,c2

            if fs.FileExists( source ) then

                ' source file is accessible

                set f1 = fs.GetFile( source )

                d1 = f1.DateLastModified

                c1 = Year(d1) * 10000 + Month(d1) * 100 + Day(d1)

        if fs.FileExists( target ) then

                set f2 = fs.GetFile( target )

                d2 = f2.DateLastModified

                c2 = Year(d2) * 10000 + Month(d2) * 100 + Day(d2)

        else

                c2 = 0

      end if

        if c1 < c2 then

        ' overwrite local copy with new version

        f1.Copy target,True

        end if

    end if

End Function

   

' begin script execution

            Dim fs

            set fs = WScript.CreateObject("Scripting.FileSystemObject")

            s = "
d:\\a.txt"

            t = "
d:\b.txt"

            update s, t

 

 

Interakcja z użytkownikiem

Dim strName 'Define the variable

strName = InputBox("Enter your name:") 'Pobierz nazwę użytkownika

WScript.echo "Hello " & strName 'Wypisz nazwę użytownika

 

Option Explicit

'Define the variables
Dim Birthday, AgeInSeconds

'Get the users DOB
Birthday = InputBox("Please enter your date of birth:")

'Calculate the difference using DateDiff
AgeInSeconds = DateDiff("s", Birthday, Now)

'Display the results
WScript.echo "You are " & AgeInSeconds & " seconds old."

 

Internet Explorer - uruchamianie

 

 

Set objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = True
boolBrowserRunning = True

Do While objIE.Visible
WScript.Sleep 500
Loop

Sub objIE_NavigateComplete2(ByVal pDisp, URL)
WScript.Echo "You just navigated to", URL
End Sub

Sub objIE_OnQuit()
boolBrowserRunning = False
End Sub
 

//=================================

//

//================================

 

objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = true

while (objIE.Visible)
{
    WScript.Sleep(500)
}

function objIE_NavigateComplete2(pDisp, URL)
{
    WScript.Echo("You just navigated to", URL)
}

 
function objIE_OnQuit()
{
    boolBrowserRunning = false ;
}


 

 

 

 

Zmienne środowiskowe dostępne z poziomu skryptów WHS:

 

 

    NUMBER_OF_PROCESSORS

    PROCESSOR_ARCHITECTURE

    PROCESSOR_IDENTIFIER 

    PROCESSOR_LEVEL

    PROCESSOR_VERSION

    OS

    COMSPEC

    HOMEDRIVE

    HOMEPATH

    PATH

    PATHEXT

    PROMPT

    SYSTEMDRIVE

    SYSTEMROOT

    WINDIR

    TEMP

    TMP

 

//=================================

//

//================================  

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEnv = WshShell.Environment("System")
WScript.Echo objEnv("OS")