Sunday, June 13, 2010

VB scripts to play with file

Script to read a file using while loop.

#!/usr/local/ActivePerl-5.8/bin/perl
if (open(MYFILE, "file1")) {
$line = ;
while ($line ne "") {
print ($line);
$line = ;
}
}

Script to write in a file

#!/usr/local/bin/perl
open (MYPIPE, "cat >hello");
print MYPIPE ("Hi, Dave! Your Perl program sent this!\n");

close (MYPIPE); 


Script to search specific word from the file.

#!/usr/local/bin/perl
print ("Word to search for $ARGV[0]\n");
$filecount = 1;
$totalwordcount = 0;

while ($filecount <= @ARGV-1) {
unless (open (INFILE, $ARGV[$filecount]))
{
die ("Can't open input file $ARGV[$filecount]\n");
            }
            $wordcount = 0;
            while ($line = )
            {
                        chop ($line);
                        print ("Before split : $line\n");
                        @words = split(/ /, $line);
                        print ("Content is : @words\n");
                        $w = 1;
                        while ($w <= @words) {
                                    if ($words[$w-1] eq $ARGV[0]) {
                                                $wordcount += 1;
                                    }
                                    $w++;
                                    }
            }         
}

print ("total number of occurrences $totalwordcount\n");

' Script to create a instant of existing file and list out the complete information of the same

Dim filesys, demofile, createdate

Set filesys = CreateObject("Scripting.FileSystemObject")
Set demofile = filesys.GetFile("e:\TEST.xls")

createdate = demofile.DateCreated
WScript.Echo (createdate)

strHomeFolder = "E:\QTP"

Set objShell = CreateObject("Wscript.Shell")
Set filesys = CreateObject("Scripting.FileSystemObject")

If filesys.FolderExists (strHomeFolder) Then
            ' Assign user permission to home folder.
            intRunError = objShell.Run("%COMSPEC% /c Echo Y
            cacls " & strHomeFolder & " /t /c /g Administrators:F ", 2, True)
            WScript.Echo (intRunError)

            If intRunError <> 0 Then
                               Wscript.Echo "Error assigning permissions for user " & strUser & " to home folder " & strHomeFolder
            Else
       Wscript.Echo "In inner loop"
            End If
End if



WScript.Echo (demofile.Attributes)
WScript.Echo (demofile.Type)
WScript.Echo "Date created: " & demofile.DateCreated
Wscript.Echo "Date last accessed: " & demofile.DateLastAccessed
Wscript.Echo "Date last modified: " & demofile.DateLastModified
Wscript.Echo "Drive: " & demofile.Drive
Wscript.Echo "Name: " & demofile.Name
Wscript.Echo "Parent folder: " & demofile.ParentFolder
Wscript.Echo "Path: " & demofile.Path
Wscript.Echo "Short name: " & demofile.ShortName
Wscript.Echo "Short path: " & demofile.ShortPath
Wscript.Echo "Size: " & demofile.Size
Wscript.Echo "Type: " & demofile.Type

WScript.Quit

' Script to write in the file and read using loop
' Also to get the version detail of vbscript.

Dim filesys, text, readfile, contents

set filesys = CreateObject("Scripting.FileSystemObject")
Set text = filesys.CreateTextFile("c:\test.txt")

text.Write "Find the last character in the text file"
text.Write " '
text.Write "Find the last character in the text fil"
text.Close

Set readfile = filesys.OpenTextFile("c:\somefile2.txt", 1, false)
Do while readfile.AtEndOfLine <> true 'Condition to get end letter of first line'

'Do while readfile.AtEndOfStream <> true 'Condition to get end letter of file'
        contents = readfile.Read(1)
'WScript.Echo contents 'Print all the letter for file'
Loop
readfile.close

'Response.Write "The last character in the text file is '" & contents & "."
WScript.Echo "The last character in the text file is '" & contents & "."
WScript.Echo ScriptEngine
WScript.Echo ScriptEngineBuildVersion
WScript.Echo ScriptEngineMajorVersion
WScript.Echo ScriptEngineMinorVersion
WScript.Echo Second(Time)
WScript.Echo Tan(55.0)

No comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | JCpenney Printable Coupons