Monday, December 28, 2009

Easy ScreenShot using Java and VBS

This Script will take the screen shots and will save it in a folder named with System Date. It will also amend the screen shot to a Word File named as System Date.

Procedure::

Step 1:
Save the following Java code in ScreenImage.java file:

/***Java Codes Starts Here+++ */
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.io.IOException;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Rectangle;
import javax.imageio.ImageIO;
import java.awt.Robot;
import java.io.File;
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;

public class ScreenImage
{
     /**
     *  Convenience method to create a BufferedImage of the desktop
     *
     *  @param     fileName name of file to be created or null
     *  @return    image the image for the given region
     *  @exception AWTException see Robot class constructors
     *  @exception IOException if an error occurs during writing
     */
    public static BufferedImage createDesktopImage(String fileName)
        throws AWTException, IOException
    {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle region = new Rectangle(0, 0, d.width, d.height);
        return ScreenImage.createImage(region, fileName);
    }

    /**
     *  Create a BufferedImage from a rectangular region on the screen.
     *
     *  @param     region region on the screen to create image from
     *  @param     fileName name of file to be created or null
     *  @return    image the image for the given region
     *  @exception AWTException see Robot class constructors
     *  @exception IOException if an error occurs during writing
     */
    public static BufferedImage createImage(Rectangle region, String fileName)
        throws AWTException, IOException
    {
        BufferedImage image = new Robot().createScreenCapture( region );
        ScreenImage.writeImage(image, fileName);
        return image;
    }

    /**
     *  Write a BufferedImage to a File.
     *
     *  @param     image image to be written
     *  @param     fileName name of file to be created
     *  @exception IOException if an error occurs during writing
    */
    public static void writeImage(BufferedImage image, String fileName)
        throws IOException
    {
        if (fileName == null) return;

        int offset = fileName.lastIndexOf( "." );
        String type = offset == -1 ? "jpg" : fileName.substring(offset + 1);

        ImageIO.write(image, type, new File( fileName ));
    }
  
    public static void main(String args[])
        throws Exception
    {
        String path =new java.io.File(".").getCanonicalPath();
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        String folderName = dateFormat.format(date);
        File folder = new File(path + "\\" + folderName);
        if (!folder.exists())
        {
            folder.mkdir();
        }
      
        path = path + "\\" + folderName;
        folder = new File(path);
        File[] listOfFiles = folder.listFiles();
  
        int imageNo = listOfFiles.length + 1;
        ScreenImage.createDesktopImage( path + "\\" + imageNo + ".jpg" );
    }
}

/***Java Code Ends Here--- */

Step 2:
Compile the Java Code in the same directory as ScreenImage.java file.

Step 3:
Save the following code as Take Screenshot.vbs in the same directory of the class file:

***VBS Code Starts Here+++
Const END_OF_STORY = 6
Const MOVE_SELECTION = 0

dd = DatePart("d",Date)
mm = DatePart("m",Date)
yy = DatePart("YYYY",Date)

If dd < 10 Then
    dd = "0" & dd
End If

If mm < 10 Then
    mm = "0" & mm
End If

today = dd & "-" & mm & "-" & yy

Set fso = CreateObject("Scripting.FileSystemObject")
Path= fso.GetParentFolderName(wscript.ScriptFullName)

Set WshShell = CreateObject("WScript.Shell")
Set WshSystemEnv = WshShell.Environment("USER")
WshSystemEnv("CLASSPATH") = Path

set objShell = wscript.createObject("wscript.shell")

'objShell.Run "cmd /c cd " & Path & " & javac ScreenImage.java", 0 , True

objShell.Run "cmd /C java ScreenImage",0,True

docFile = Path & "\" & today & ".doc"

imagePath = Path & "\" & today & "\"

fileCount=0

For Each file In fso.GetFolder(imagePath).Files
    fileCount=fileCount+1
Next

image = imagePath & fileCount & ".jpg"

If fso.FileExists (docFile) Then
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = False
    Set objDoc = objword.Documents.Open(docFile)
    Set objSelection = objWord.Selection
    objSelection.EndKey END_OF_STORY, MOVE_SELECTION
    Set colShapes = objDoc.Shapes
    Set objShape = objSelection.InlineShapes.AddPicture(image)
    objDoc.Save
Else
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = False
    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection
    objSelection.EndKey END_OF_STORY, MOVE_SELECTION
    Set colShapes = objDoc.Shapes
    Set objShape = objSelection.InlineShapes.AddPicture(image)
    objDoc.SaveAs(docFile)
End If

objWord.Quit

Msgbox "Done !!!"
***VBS Code Ends Here---

Step 4:
Add the Take Screenshot.vbs shortcut to Quick Launch toolbar of Windows and Start Clicking....

Wednesday, December 16, 2009

Get Drive Free Space info usnig VBS and WMI

ave the following code in a text file and "Save As" in '.vbs' extension. Then double click on the .vbs file to get free space information of all drives:

'***Code Starts Here+++
On Error Resume Next

Dim strComputer
Dim objWMIService
Dim colItems

strComputer = "."

Set objWMIService = GetObject( "winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( "SELECT Freespace,DeviceID FROM Win32_LogicalDisk",,48)

For Each objItem in colItems
   WScript.Echo "Drive Name: " & objItem.DeviceID & vbCrLf & "FreeSpace: " & ROUND((objItem.Freespace / (1024 * 1024 * 1024)),2) & " GB"
Next

'***Code Ends Here---

Change IE Proxy Setting by VBS

Save the following code in a text file and "Save As" in '.vbs' extension. Then double click on the .vbs file to change proxy:

'***Code Starts Here+++
Option Explicit
On Error Resume Next
Dim objShell
Dim regProxyServerLoc
Dim ProxyServerName

regProxyServerLoc = "HKEY_CURRENT_USER\Software\Microsoft\" & _
    "Windows\CurrentVersion\Internet Settings\ProxyServer"

Set objShell = CreateObject("WScript.Shell")

ProxyServerName = objShell.RegRead(regProxyServerLoc)

If ProxyServerName = "proxy1.com:8080" Then

   objShell.RegWrite regProxyServerLoc,"proxy2.com:8080"

   WScript.Echo "BT Proxy Enabled"

ElseIf ProxyServerName = "proxy2.com:8080" Then

   objShell.RegWrite regProxyServerLoc,"proxy1.com:8080"

   WScript.Echo "TechM Proxy Enabled"

End If

'***Code Ends Here---

Thursday, December 10, 2009

Get File List using VBS

Save the following code in a text file and "Save As" in '.vbs' extension. Then double click on the .vbs file and provide the folder path for which you want to get report.

'***Code Starts Here+++
Dim Input
Set objFSO = CreateObject("Scripting.FileSystemObject")
Input = InputBox ("Enter the full path to the folder (Defalut Current Directory)","Enter Full Path")

Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
If Trim(Input)="" Then
    Input = "."
End If
Set oFolder = oFS.GetFolder(Input)

strFile = "Report.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFile)
objFile.WriteLine("File Name" & "," & "File Size (in MB)" & "," & "File Path")
ShowMainFolderDetails oFolder
ShowFolderDetails oFolder
msgbox ("Done !!!")

sub ShowFolderDetails(oF)
    dim F
    for each F in oF.Subfolders
        ShowFolderDetails(F)
        ShowFileList(F)
    next
end sub

sub ShowMainFolderDetails(oF)
    ShowFileList(oF)
end sub

Function ShowFileList(fs)
    Set fc = fs.Files
    For Each f1 in fc
        objFile.WriteLine(f1.name & "," & (f1.Size/1024)/1024 & "," & f1.Path)
    Next
    ShowFileList = s
End Function

'***Code Ends Here---
The report will be created in the same folder of .vbs file with name "Report.csv'.

Also using the following code ONLY subfolder list can be obtained:

'***Code Starts Here+++
Dim Input
Set objFSO = CreateObject("Scripting.FileSystemObject")
Input = InputBox ("Enter the full path to the folder (Defalut Current Directory)","Enter Full Path")

Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
If Trim(Input)="" Then
    Input = "."
End If
Set oFolder = oFS.GetFolder(Input)

strFile = "Test.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFile)
objFile.WriteLine("Folder Name" & "," & "Folder Size (in MB)" & "," & "Folder Path")
ShowFolderDetails oFolder
msgbox ("Done !!!")

sub ShowFolderDetails(oF)
    dim F
    for each F in oF.Subfolders
        objFile.WriteLine(F.Name & "," & (F.Size/1024)/1024 & "," & F.Path)
    next
end sub

'***Code Ends Here---