Featured

QTP 10 not Recognizing Oracle Forms Obje

Bidun Meduza shared on our Facebook page a problem and solution that I thought might be useful to you too. Problem He had a situation where QTP 10 was unable to recognize objects of Oracle Forms. Th

Read More
QTP 10 not Recognizing Oracle Forms Objects

New Navigation Menus

In order to ease orientation through our vast knowledge archive, we have added the following navigation menus: First Steps Scripting QTP VBScript Tutorial Knowledge Articles (by levels of expertise) B

Read More
New Navigation Menus

Permalinks

We’ve changed the permalinks format, so if you have been experiencing some trouble with finding materials with the old links, then remove the YYYY/MM/ from the URL and you’d be fine. In ca

Read More
Permalinks

Advanced QTP on Android

A first version of AdvancedQTP for Android can be found here. Now you can take this perfect companion with you to keep updated on what’s new in our site.

Read More
Advanced QTP on Android

New Advanced QTP Features: Layout and FB

As you have most surely noticed, we’ve recently changed the site’s theme in order to enhance the overall user experience. As from today, you can also register and login seamlessly with you

Read More
New Advanced QTP Features: Layout and FB Integration

Working with DOS Command Line

0
by on April 16, 2008 at 23:18

Objectives

The objectives of this article are :

  • Learn how to use DOS command lines in QTP.
  • Alternative complex tasks.
  • Alternative .NET methods.

cmd.exe allows access to the Microsoft Windows Command Prompt, also known as Microsoft DOS.

To-date, cmd.exe is a 32-bit command prompt used in Windows NT, 2000, and XP and offers disk and file maintenance functions to your computer as well as network functions. cmd.exe can be found under System32 folder.

SystemUtil.Run <span style="color: #006080;">"cmd.exe"</span>,<span style="color: #006080;">""</span>,<span style="color: #006080;">"C:\WINDOWS\system32"</span>,<span style="color: #006080;">"open"</span>

image

Capture the window by QTP

image

image

Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> )
Print Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).GetROProperty( <span style="color: #006080;">"title"</span> )

Typing to DOS window

The Type method types the specified string in the window.

Syntax: object.Type KeyboardInput.

KeyboardInput : A String value. The text string and/or constants representing non-alphanumeric keys.

image image

Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).Type <span style="color: #006080;">"Ping advancedqtp.com"</span>
Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).Type micReturn

image

Catching the Response

The GetVisibleText method returns the text from the specified area. The text to capture must be visible in the application window when the step runs. The area is defined by pairs of coordinates that designate two diagonally opposite corners of a rectangle.

Syntax : object.GetVisibleText ([Left], [Top], [Right], [Bottom])

txt = Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).GetVisibleText()
print txt

image

Problems…

SystemUtil.Run <span style="color: #006080;">"cmd.exe"</span>,<span style="color: #006080;">""</span>,<span style="color: #006080;">"C:\WINDOWS\system32"</span>,<span style="color: #006080;">"open"</span>
Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).Type <span style="color: #006080;">"dir"</span>
Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).Type micReturn
txt = Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).GetVisibleText()
print txt

The GetVisibleText method “catches” only the text that is visible. large commands with large responses are useless…

Solution

Wscript.Shell Object

The WSript.Shell object provides functions to read system information and environment variables, work with the registry and manage shortcuts. You create a WSript.Shell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.

<span style="color: #0000ff;">Dim</span> wShell
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )

image

you will see this detailed description if you are using PDM.DLL version 9

Exec Method

Runs an application in a child command-shell, providing access to the StdIn/StdOut/StdErr streams.

Syntax : object.Exec( strCommand )

strCommand : String value indicating the command line used to run the script. The command line should appear exactly as it would if you typed it at the command prompt.

The Exec method returns a WshScriptExec object, which provides status and error information about a script run with Exec along with access to the StdIn, StdOut, and StdErr channels.

%compsec%

What if your colleague installed MS-Windows on D:\ Drive?  –> The C:\Windows\System32\cmd.exe will not work.

Since every machine can have different installation directory, the environment %compsec% returns the full path of the cmd.exe file. image

Executing a command

  • /C– Carries out the command specified by string and then terminates.
  • /K – Carries out the command specified by string but remains.

image

Executing the command

<span style="color: #0000ff;">Dim</span> wShell, exec
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C ping advancedqtp.com"</span> )

WshScriptExec Object

Provides status information about a script run with Exec along with access to the StdIn, StdOut, and StdErr streams.

WshScriptExec.Status Property

Provides status information about a script run with the Exec() method.

  • WshRunning ( = 0 )- The job is still running.
  • WshFinished ( = 1 ) – The job has completed.

WshScriptExec.ExitCode Property

Returns the exit code set by a script or program run using the Exec() method. Executables set an exit code when they finish running. This conveys the status information when a process ends. Often, it is used to send an error code (or some other piece of information) back to the caller. If the process has not finished, the ExitCode property returns 0. The values returned from ExitCode depend on the application that was called.

WshScriptExec.StdOut Property

The StdOut property contains a read-only copy of any information the script may have sent to the standard output.

  • AtEndOfStream- Returns a Boolean value indicating whether the end of an input stream has been reached.
  • ReadAll- Returns all characters from a stream.
  • ReadLine – Reads an entire line from a stream.

Handling Errors

Run the following code. why is the result empty?

<span style="color: #0000ff;">Dim</span> wShell, exec
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C png advancedqtp.com"</span> )
print exec.StdOut.ReadAll

WshScriptExec.StdErr Property

since the “png” command has a syntax error ( ping ), the STDOUT remains empty.

Now, run the following code :

<span style="color: #0000ff;">Dim</span> wShell, exec
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C png advancedqtp.com"</span> )
print exec.StdErr.ReadAll

image

Since we have a syntax error, the STDERR buffer is filled, instead of the STDOUT. but, what about a “bad” command ( without syntax errors )?

<span style="color: #0000ff;">Dim</span> wShell, exec
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C ping no-exists"</span> )
print exec.StdOut.ReadAll

in this case STDOUT will be filled, but not STDERR.

image

How could we know when an error occurred?

<span style="color: #0000ff;">Dim</span> wShell, exec, outStr
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C ping arrdvancedqtp.com"</span> )
<span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> exec.Status = 0
    <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream <span style="color: #0000ff;">Then</span>
        outStr = exec.StdOut.ReadAll
        <span style="color: #0000ff;">If</span> exec.ExitCode = 1 <span style="color: #0000ff;">then</span>
            Reporter.ReportEvent micWarning, <span style="color: #006080;">"Command failed"</span>, outStr
        <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Do</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      outStr = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      Reporter.ReportEvent micFail, <span style="color: #006080;">"Command failed"</span>, outStr
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Do</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   Wait 1
<span style="color: #0000ff;">Loop</span>
Print outStr

Alternative .NET

<span style="color: #0000ff;">Set</span> nt = DotNetFactory.CreateInstance( <span style="color: #006080;">"Microsoft.VisualBasic.Devices.Network"</span> )
<span style="color: #0000ff;">If</span> <span style="color: #0000ff;">CBool</span>( nt.Ping( <span style="color: #006080;">"www.advancedqtp.com"</span> ) ) <span style="color: #0000ff;">Then</span>
   MsgBox( <span style="color: #006080;">"Server pinged successfully."</span> )
<span style="color: #0000ff;">Else</span>
   MsgBox( <span style="color: #006080;">"Ping request timed out."</span> )
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

Demo

Print a list of “.exe” files under System32 without header information in lower case order by file name descending.

 

 

<span style="color: #0000ff;">Option</span> Explicit

<span style="color: #0000ff;">Const</span> SystemFolder = 1
<span style="color: #0000ff;">Dim</span> wShell, exec, fso
<span style="color: #0000ff;">Dim</span> dirList

<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> fso = CreateObject(<span style="color: #006080;">"Scripting.FileSystemObject"</span>)
dirList = fso.GetSpecialFolder( SystemFolder ) & <span style="color: #006080;">"\*.exe"</span>
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C dir "</span> & dirList & <span style="color: #006080;">" /B /O-N /L"</span> )
<span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream <span style="color: #0000ff;">Then</span>
      dirList = exec.StdOut.ReadAll
      <span style="color: #0000ff;">If</span> exec.ExitCode = 1 <span style="color: #0000ff;">then</span>
            Reporter.ReportEvent micWarning, <span style="color: #006080;">"Command failed"</span>, dirList
        <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Do</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      dirList = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      Reporter.ReportEvent micFail, <span style="color: #006080;">"Command failed"</span>, dirList
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Do</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   Wait 1
<span style="color: #0000ff;">Loop</span>
Print dirList

 

, ,

List all Files and Directories in a Specific Directory

0
by on March 31, 2008 at 14:49

Dir displays a list of files and subdirectories in a directory.

DIR is a command used to display a list of files and subdirectories in a directory..

Windows 2000 and Windows XP syntax

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

[drive:][path][filename]

Specifies drive, directory, and/or files to list.

attributes

<span style="font-family: \'Bookman Old Style\'; mso-fareast-font-family: \'Times New Roman\'; mso-bidi-font-family: \'Times New Roman\'; mso-fareast-language: en-us; mso-bidi-language: ar-sa;">D<span style="mso-spacerun: yes;">  </span>Directories
 R<span style="mso-spacerun: yes;">  </span>Read-only files
 H<span style="mso-spacerun: yes;">  </span>Hidden files
 A<span style="mso-spacerun: yes;">  </span>Files ready for archiving
 S<span style="mso-spacerun: yes;">  </span>System files
 -<span style="mso-spacerun: yes;">  </span>Prefix meaning not<?xml:namespace prefix = o /></span>

/B

Uses bare format (no heading information or summary).

/C

Display the thousand separator in file sizes. This is the default. Use /-C to disable display of separator.

/D

Same as wide but files are list sorted by column.

/L

Uses lowercase.

/N

New long list format where filenames are on the far right.

/O

List by files in sorted order.

sortorder

<span style="font-family: \'Bookman Old Style\'; mso-fareast-font-family: \'Times New Roman\'; mso-bidi-font-family: \'Times New Roman\'; mso-fareast-language: en-us; mso-bidi-language: ar-sa;">N<span style="mso-spacerun: yes;">  </span>By name (alphabetic)
 S<span style="mso-spacerun: yes;">  </span>By size (smallest first)
 E<span style="mso-spacerun: yes;">  </span>By extension (alphabetic)
 D<span style="mso-spacerun: yes;">  </span>By date/time (oldest first)
 G<span style="mso-spacerun: yes;">  </span>Group directories first
 -<span style="mso-spacerun: yes;">  </span>Prefix to reverse order<?xml:namespace prefix = o /></span>

/P

Pauses after each screenful of information.

/Q

Display the owner of the file.

/S

Displays files in specified directory and all subdirectories.

/T

Controls which time field displayed or used for sorting

timefield

C Creation

A Last Access

W Last Written

/W

Uses wide list format.

/X

This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place.

/4

Displays four-digit years

the example list all files and folders under C:\. optionally you can load the output to an array, id you need to iterate the list.

<span style="color: #0000ff;">Dim</span> outMsg, listArr
<span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> RunDosCommand( <span style="color: #006080;">"DIR C:\"</span>, outMsg ) <span style="color: #0000ff;">Then</span>
    Reporter.ReportEvent micFail, <span style="color: #006080;">"RunDosCommand"</span>, outMsg
    ExitTest( <span style="color: #006080;">"ERROR:RunDosCommand"</span> )
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
Print outMsg
<span style="color: #008000;">\' ** loading the list in an array</span>
listArr = Split( outMsg, vbCrLf, -1 )

RunDosCommand Function

<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> RunDosCommand( <span style="color: #0000ff;">ByVal</span> cmd, <span style="color: #0000ff;">ByRef</span> stdout )
   <span style="color: #0000ff;">Dim</span> sb, wShell, exec
   <span style="color: #0000ff;">Dim</span> input

   RunDosCommand = <span style="color: #0000ff;">False</span>
   <span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
   <span style="color: #008000;">\' ** Executing the command</span>
   <span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C "</span> & cmd )
   <span style="color: #008000;">\' ** Checking for errors...</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      stdout = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #008000;">\' ** The stringBuilder is faster than concatenation ( & )</span>
   <span style="color: #0000ff;">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080;">"System.Text.StringBuilder"</span> )
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      <span style="color: #008000;">\' ** Appending only not empty lines </span>
      <span style="color: #0000ff;">If</span> Len( input ) > 0 <span style="color: #0000ff;">Then</span>
         sb.AppendLine input
      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">Loop</span>
   <span style="color: #008000;">\' ** retieve the output</span>
   stdout = sb.ToString
   RunDosCommand = <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">Set</span> sb = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>

, , ,

List only directories in a specific directory

0
by on March 31, 2008 at 14:47

Dir displays a list of files and subdirectories in a directory.

DIR is a command used to display a list of files and subdirectories in a directory..

 

Windows 2000 and Windows XP syntax

 

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

 

[drive:][path][filename]

 

Specifies drive, directory, and/or files to list.

 

attributes

 

<span style="font-family: \'Bookman Old Style\'; mso-fareast-font-family: \'Times New Roman\'; mso-bidi-font-family: \'Times New Roman\'; mso-fareast-language: en-us; mso-bidi-language: ar-sa;">D<span style="mso-spacerun: yes;">  </span>Directories
 R<span style="mso-spacerun: yes;">  </span>Read-only files
 H<span style="mso-spacerun: yes;">  </span>Hidden files
 A<span style="mso-spacerun: yes;">  </span>Files ready for archiving
 S<span style="mso-spacerun: yes;">  </span>System files
 -<span style="mso-spacerun: yes;">  </span>Prefix meaning not<?xml:namespace prefix = o /></span>

/B

 

Uses bare format (no heading information or summary).

 

/C

 

Display the thousand separator in file sizes. This is the default. Use /-C to disable display of separator.

 

/D

 

Same as wide but files are list sorted by column.

 

/L

 

Uses lowercase.

 

/N

 

New long list format where filenames are on the far right.

 

/O

 

List by files in sorted order.

 

sortorder

 

<span style="font-family: \'Bookman Old Style\'; mso-fareast-font-family: \'Times New Roman\'; mso-bidi-font-family: \'Times New Roman\'; mso-fareast-language: en-us; mso-bidi-language: ar-sa;">N<span style="mso-spacerun: yes;">  </span>By name (alphabetic)
 S<span style="mso-spacerun: yes;">  </span>By size (smallest first)
 E<span style="mso-spacerun: yes;">  </span>By extension (alphabetic)
 D<span style="mso-spacerun: yes;">  </span>By date/time (oldest first)
 G<span style="mso-spacerun: yes;">  </span>Group directories first
 -<span style="mso-spacerun: yes;">  </span>Prefix to reverse order<?xml:namespace prefix = o /></span>

/P

 

Pauses after each screenful of information.

 

/Q

 

Display the owner of the file.

 

/S

 

Displays files in specified directory and all subdirectories.

 

/T

 

Controls which time field displayed or used for sorting

 

timefield

 

C Creation

A Last Access

W Last Written

 

/W

 

Uses wide list format.

 

/X

 

This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place.

 

/4

 

Displays four-digit years

 

the example list all folders under C:\Automation. optionally you can load the output to an array, id you need to iterate the list.

RunDosCommand Function

<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> RunDosCommand( <span style="color: #0000ff;">ByVal</span> cmd, <span style="color: #0000ff;">ByRef</span> stdout )
   <span style="color: #0000ff;">Dim</span> sb, wShell, exec
   <span style="color: #0000ff;">Dim</span> input

   RunDosCommand = <span style="color: #0000ff;">False</span>
   <span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
   <span style="color: #008000;">\' ** Executing the command</span>
   <span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C "</span> & cmd )
   <span style="color: #008000;">\' ** Checking for errors...</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      stdout = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #008000;">\' ** The stringBuilder is faster than concatenation ( & )</span>
   <span style="color: #0000ff;">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080;">"System.Text.StringBuilder"</span> )
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      <span style="color: #008000;">\' ** Appending only not empty lines </span>
      <span style="color: #0000ff;">If</span> Len( input ) > 0 <span style="color: #0000ff;">Then</span>
         sb.AppendLine input
      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">Loop</span>
   <span style="color: #008000;">\' ** retieve the output</span>
   stdout = sb.ToString
   RunDosCommand = <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">Set</span> sb = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>

, , ,

List only Files and Folders in a Specific Directory and Sub-Directory

0
by on March 31, 2008 at 14:44

Dir displays a list of files and subdirectories in a directory.

DIR is a command used to display a list of files and subdirectories in a directory..

Windows 2000 and Windows XP syntax

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

[drive:][path][filename]

Specifies drive, directory, and/or files to list.

attributes

<span style="font-family: \'Bookman Old Style\'; mso-fareast-font-family: \'Times New Roman\'; mso-bidi-font-family: \'Times New Roman\'; mso-fareast-language: en-us; mso-bidi-language: ar-sa;">D<span style="mso-spacerun: yes;">  </span>Directories
 R<span style="mso-spacerun: yes;">  </span>Read-only files
 H<span style="mso-spacerun: yes;">  </span>Hidden files
 A<span style="mso-spacerun: yes;">  </span>Files ready for archiving
 S<span style="mso-spacerun: yes;">  </span>System files
 -<span style="mso-spacerun: yes;">  </span>Prefix meaning not<?xml:namespace prefix = o /><?XML:NAMESPACE PREFIX = O /></span>

/B

Uses bare format (no heading information or summary).

/C

Display the thousand separator in file sizes. This is the default. Use /-C to disable display of separator.

/D

Same as wide but files are list sorted by column.

/L

Uses lowercase.

/N

New long list format where filenames are on the far right.

/O

List by files in sorted order.

sortorder

<span style="font-family: \'Bookman Old Style\'; mso-fareast-font-family: \'Times New Roman\'; mso-bidi-font-family: \'Times New Roman\'; mso-fareast-language: en-us; mso-bidi-language: ar-sa;">N<span style="mso-spacerun: yes;">  </span>By name (alphabetic)
 S<span style="mso-spacerun: yes;">  </span>By size (smallest first)
 E<span style="mso-spacerun: yes;">  </span>By extension (alphabetic)
 D<span style="mso-spacerun: yes;">  </span>By date/time (oldest first)
 G<span style="mso-spacerun: yes;">  </span>Group directories first
 -<span style="mso-spacerun: yes;">  </span>Prefix to reverse order<?xml:namespace prefix = o /><?XML:NAMESPACE PREFIX = O /></span>

/P

Pauses after each screenful of information.

/Q

Display the owner of the file.

/S

Displays files in specified directory and all subdirectories.

/T

Controls which time field displayed or used for sorting

timefield

C Creation

A Last Access

W Last Written

/W

Uses wide list format.

/X

This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place.

/4

Displays four-digit years

the example list all folders under QTP installation folder. optionally you can load the output to an array, id you need to iterate the list.

<span style="color: #0000ff;">Dim</span> outMsg, listArr, dirName
dirname = Chr( 34 ) & Environment( <span style="color: #006080;">"ProductDir"</span> ) & Chr( 34 )
<span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> RunDosCommand( <span style="color: #006080;">"DIR /S "</span> & dirname, outMsg ) <span style="color: #0000ff;">Then</span>
    Reporter.ReportEvent micFail, <span style="color: #006080;">"RunDosCommand"</span>, outMsg
    ExitTest( <span style="color: #006080;">"ERROR:RunDosCommand"</span> )
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
Print outMsg
<span style="color: #008000;">\' ** loading the list in an array</span>
listArr = Split( outMsg, vbCrLf, -1 )

Note: When using Windows file names, you need to quote the path.

RunDosCommand Function

<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> RunDosCommand( <span style="color: #0000ff;">ByVal</span> cmd, <span style="color: #0000ff;">ByRef</span> stdout )
   <span style="color: #0000ff;">Dim</span> sb, wShell, exec
   <span style="color: #0000ff;">Dim</span> input

   RunDosCommand = <span style="color: #0000ff;">False</span>
   <span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
   <span style="color: #008000;">\' ** Executing the command</span>
   <span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C "</span> & cmd )
   <span style="color: #008000;">\' ** Checking for errors...</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      stdout = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #008000;">\' ** The stringBuilder is faster than concatenation ( & )</span>
   <span style="color: #0000ff;">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080;">"System.Text.StringBuilder"</span> )
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      <span style="color: #008000;">\' ** Appending only not empty lines </span>
      <span style="color: #0000ff;">If</span> Len( input ) > 0 <span style="color: #0000ff;">Then</span>
         sb.AppendLine input
      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">Loop</span>
   <span style="color: #008000;">\' ** retieve the output</span>
   stdout = sb.ToString
   RunDosCommand = <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">Set</span> sb = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>

, , ,

Delete one File in any Directory

0
by on March 31, 2008 at 06:46

Del is a command used to delete files from the computer.

DEL or ERASE is a command used to delete files from the computer.
Windows 2000 and Windows XP syntax
Deletes one or more files.
DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

names

 

Specifies a list of one or more files or directories. Wildcards may be used to delete multiple files. If a directory is specified, all files within the directory will be deleted.

 

/P

 

Prompts for confirmation before deleting each file.

 

/F

 

Force deleting of read-only files.

 

/S

 

Delete specified files from all subdirectories.

 

/Q

 

Quiet mode, do not ask if ok to delete on global wildcard.

 

/A

 

Selects files to delete based on attributes

 

attributes

 

R  Read-only files
S  System files
H  Hidden files
A  Files ready for archiving
-  Prefix meaning not

 

IMPORTANT WARNING: This command is easy to use, but it is the most dangerous one you will encounter in DOS (apart form FORMAT). If you aren’t careful, you may delete a file which you, the system, or someone else needs. And, unless you have saved a backup of that file, the erased file is gone for good. For this reason it is good practice to use only complete file specifications with the ERASE/DEL command (and to keep backups of your most valuable files). As a safety precaution, never use the wild-card characters ‘*’ and ‘?’ in ERASE/DEL commands.

<span style="color: #0000ff;">Dim</span> outMsg
<span style="color: #0000ff;">If</span> RunDosCommand( <span style="color: #006080;">"DEL c:\windows\test.doc"</span>, outMsg ) <span style="color: #0000ff;">Then</span>
   Reporter.ReportEvent micPass, <span style="color: #006080;">"RunDosCommand"</span>, <span style="color: #006080;">"Ended successfully."</span>
<span style="color: #0000ff;">Else</span>
   Reporter.ReportEvent micFail, <span style="color: #006080;">"RunDosCommand"</span>, outMsg
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

the example deletes the c:\windows\test.doc in the windows directory if it exists, otherwise an error message will be displayed in the Reporter.

RunDosCommand Function

<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> RunDosCommand( <span style="color: #0000ff;">ByVal</span> cmd, <span style="color: #0000ff;">ByRef</span> stdout )
   <span style="color: #0000ff;">Dim</span> sb, wShell, exec
   <span style="color: #0000ff;">Dim</span> input

   RunDosCommand = <span style="color: #0000ff;">False</span>
   <span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
   <span style="color: #008000;">\' ** Executing the command</span>
   <span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C "</span> & cmd )
   <span style="color: #008000;">\' ** Checking for errors...</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      stdout = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #008000;">\' ** The stringBuilder is faster than concatenation ( & )</span>
   <span style="color: #0000ff;">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080;">"System.Text.StringBuilder"</span> )
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      <span style="color: #008000;">\' ** Appending only not empty lines </span>
      <span style="color: #0000ff;">If</span> Len( input ) > 0 <span style="color: #0000ff;">Then</span>
         sb.AppendLine input
      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">Loop</span>
   <span style="color: #008000;">\' ** retieve the output</span>
   stdout = sb.ToString
   RunDosCommand = <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">Set</span> sb = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>

, ,

Delete all Files in a Folder

0
by on March 31, 2008 at 06:43

Del is a command used to delete files from the computer.

DEL or ERASE is a command used to delete files from the computer.

 

Windows 2000 and Windows XP syntax

 

Deletes one or more files.

 

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

names

 

Specifies a list of one or more files or directories. Wildcards may be used to delete multiple files. If a directory is specified, all files within the directory will be deleted.

 

/P

 

Prompts for confirmation before deleting each file.

 

/F

 

Force deleting of read-only files.

 

/S

 

Delete specified files from all subdirectories.

 

/Q

 

Quiet mode, do not ask if ok to delete on global wildcard.

 

/A

 

Selects files to delete based on attributes

 

attributes

 

R  Read-only files
S  System files
H  Hidden files
A  Files ready for archiving
-  Prefix meaning not

 

IMPORTANT WARNING: This command is easy to use, but it is the most dangerous one you will encounter in DOS (apart form FORMAT). If you aren’t careful, you may delete a file which you, the system, or someone else needs. And, unless you have saved a backup of that file, the erased file is gone for good. For this reason it is good practice to use only complete file specifications with the ERASE/DEL command (and to keep backups of your most valuable files). As a safety precaution, never use the wild-card characters ‘*’ and ‘?’ in ERASE/DEL commands.

When Using wild-cards ( regular expressions ) the default
DOS command does not let you auto-erase all files, but confirm one by one. it can bypassed using the switch /Q

<span style="color: #0000ff;">Dim</span> outMsg
<span style="color: #0000ff;">If</span> RunDosCommand( <span style="color: #006080;">"DEL /Q c:\logs\temp\*.*"</span>, outMsg ) <span style="color: #0000ff;">Then</span>
   Reporter.ReportEvent micPass, <span style="color: #006080;">"RunDosCommand"</span>, <span style="color: #006080;">"Ended successfully."</span>
<span style="color: #0000ff;">Else</span>
   Reporter.ReportEvent micFail, <span style="color: #006080;">"RunDosCommand"</span>, outMsg
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

the example d indicates that you would like to delete all files in the c:\logs\temp directory, otherwise an error message will be displayed in the Reporter.

This command will not delete files with “Read Only” attribute. to delete also “Read Only” files add the switch /F

RunDosCommand Function

<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> RunDosCommand( <span style="color: #0000ff;">ByVal</span> cmd, <span style="color: #0000ff;">ByRef</span> stdout )
   <span style="color: #0000ff;">Dim</span> sb, wShell, exec
   <span style="color: #0000ff;">Dim</span> input

   RunDosCommand = <span style="color: #0000ff;">False</span>
   <span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
   <span style="color: #008000;">\' ** Executing the command</span>
   <span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C "</span> & cmd )
   <span style="color: #008000;">\' ** Checking for errors...</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      stdout = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #008000;">\' ** The stringBuilder is faster than concatenation ( & )</span>
   <span style="color: #0000ff;">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080;">"System.Text.StringBuilder"</span> )
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      <span style="color: #008000;">\' ** Appending only not empty lines </span>
      <span style="color: #0000ff;">If</span> Len( input ) > 0 <span style="color: #0000ff;">Then</span>
         sb.AppendLine input
      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">Loop</span>
   <span style="color: #008000;">\' ** retieve the output</span>
   stdout = sb.ToString
   RunDosCommand = <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">Set</span> sb = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>

, ,

Delete all Fles in a Folder Matching a Pattern

0
by on March 31, 2008 at 06:39

Del is a command used to delete files from the computer.

DEL or ERASE is a command used to delete files from the computer.

 

Windows 2000 and Windows XP syntax

 

Deletes one or more files.

 

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

names

 

Specifies a list of one or more files or directories. Wildcards may be used to delete multiple files. If a directory is specified, all files within the directory will be deleted.

 

/P

 

Prompts for confirmation before deleting each file.

 

/F

 

Force deleting of read-only files.

 

/S

 

Delete specified files from all subdirectories.

 

/Q

 

Quiet mode, do not ask if ok to delete on global wildcard.

 

/A

 

Selects files to delete based on attributes

 

attributes

 

R  Read-only files
S  System files
H  Hidden files
A  Files ready for archiving
-  Prefix meaning not

 

IMPORTANT WARNING: This command is easy to use, but it is the most dangerous one you will encounter in DOS (apart form FORMAT). If you aren’t careful, you may delete a file which you, the system, or someone else needs. And, unless you have saved a backup of that file, the erased file is gone for good. For this reason it is good practice to use only complete file specifications with the ERASE/DEL command (and to keep backups of your most valuable files). As a safety precaution, never use the wild-card characters ‘*’ and ‘?’ in ERASE/DEL commands.

When Using wild-cards ( regular expressions ) the default
DOS command does not let you auto-erase all files, but confirm one by one. it can bypassed using the switch /Q
The switch /F forces removes the “read only” files.

<span style="color: #0000ff;">Dim</span> outMsg
<span style="color: #0000ff;">If</span> RunDosCommand( <span style="color: #006080;">"ERASE /Q /F C:\Logs\temp\*.tmp"</span>, outMsg ) <span style="color: #0000ff;">Then</span>
   Reporter.ReportEvent micPass, <span style="color: #006080;">"RunDosCommand"</span>, <span style="color: #006080;">"Ended successfully."</span>
<span style="color: #0000ff;">Else</span>
   Reporter.ReportEvent micFail, <span style="color: #006080;">"RunDosCommand"</span>, outMsg
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

the example indicates that you would like to delete all files in the C:\Logs\temp directory that match *.tmp, otherwise an error message will be displayed in the Reporter.

RunDosCommand Function

<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> RunDosCommand( <span style="color: #0000ff;">ByVal</span> cmd, <span style="color: #0000ff;">ByRef</span> stdout )
   <span style="color: #0000ff;">Dim</span> sb, wShell, exec
   <span style="color: #0000ff;">Dim</span> input

   RunDosCommand = <span style="color: #0000ff;">False</span>
   <span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
   <span style="color: #008000;">\' ** Executing the command</span>
   <span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C "</span> & cmd )
   <span style="color: #008000;">\' ** Checking for errors...</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      stdout = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #008000;">\' ** The stringBuilder is faster than concatenation ( & )</span>
   <span style="color: #0000ff;">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080;">"System.Text.StringBuilder"</span> )
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      <span style="color: #008000;">\' ** Appending only not empty lines </span>
      <span style="color: #0000ff;">If</span> Len( input ) > 0 <span style="color: #0000ff;">Then</span>
         sb.AppendLine input
      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">Loop</span>
   <span style="color: #008000;">\' ** retieve the output</span>
   stdout = sb.ToString
   RunDosCommand = <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">Set</span> sb = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>

, , ,

Delete all “Read Only” in a Specific Folder

0
by on March 31, 2008 at 06:37

Del is a command used to delete files from the computer.

DEL or ERASE is a command used to delete files from the computer.

 

Windows 2000 and Windows XP syntax

 

Deletes one or more files.

 

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

names

 

Specifies a list of one or more files or directories. Wildcards may be used to delete multiple files. If a directory is specified, all files within the directory will be deleted.

 

/P

 

Prompts for confirmation before deleting each file.

 

/F

 

Force deleting of read-only files.

 

/S

 

Delete specified files from all subdirectories.

 

/Q

 

Quiet mode, do not ask if ok to delete on global wildcard.

 

/A

 

Selects files to delete based on attributes

 

attributes

 

R  Read-only files
S  System files
H  Hidden files
A  Files ready for archiving
-  Prefix meaning not

 

IMPORTANT WARNING: This command is easy to use, but it is the most dangerous one you will encounter in DOS (apart form FORMAT). If you aren’t careful, you may delete a file which you, the system, or someone else needs. And, unless you have saved a backup of that file, the erased file is gone for good. For this reason it is good practice to use only complete file specifications with the ERASE/DEL command (and to keep backups of your most valuable files). As a safety precaution, never use the wild-card characters ‘*’ and ‘?’ in ERASE/DEL commands.

When Using wild-cards ( regular expressions ) the default
DOS command does not let you auto-erase all files, but confirm one by one. it can bypassed using the switch /Q

<span style="color: #0000ff;">Dim</span> outMsg
<span style="color: #0000ff;">If</span> RunDosCommand( <span style="color: #006080;">"ERASE /Q /A:R C:\Logs\temp\*.*"</span>, outMsg ) <span style="color: #0000ff;">Then</span>
   Reporter.ReportEvent micPass, <span style="color: #006080;">"RunDosCommand"</span>, <span style="color: #006080;">"Ended successfully."</span>
<span style="color: #0000ff;">Else</span>
   Reporter.ReportEvent micFail, <span style="color: #006080;">"RunDosCommand"</span>, outMsg
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

the example d indicates that you would like to delete all files in the C:\Logs\temp directory that match *.tmp, otherwise an error message will be displayed in the Reporter.

RunDosCommand function

<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> RunDosCommand( <span style="color: #0000ff;">ByVal</span> cmd, <span style="color: #0000ff;">ByRef</span> stdout )
   <span style="color: #0000ff;">Dim</span> sb, wShell, exec
   <span style="color: #0000ff;">Dim</span> input

   RunDosCommand = <span style="color: #0000ff;">False</span>
   <span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
   <span style="color: #008000;">\' ** Executing the command</span>
   <span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C "</span> & cmd )
   <span style="color: #008000;">\' ** Checking for errors...</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      stdout = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #008000;">\' ** The stringBuilder is faster than concatenation ( & )</span>
   <span style="color: #0000ff;">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080;">"System.Text.StringBuilder"</span> )
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      <span style="color: #008000;">\' ** Appending only not empty lines </span>
      <span style="color: #0000ff;">If</span> Len( input ) > 0 <span style="color: #0000ff;">Then</span>
         sb.AppendLine input
      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">Loop</span>
   <span style="color: #008000;">\' ** retieve the output</span>
   stdout = sb.ToString
   RunDosCommand = <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">Set</span> sb = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>

, , ,

Delete all Temporary Files

0
by on March 31, 2008 at 06:35

Del is a command used to delete files from the computer.

DEL or ERASE is a command used to delete files from the computer.

 

Windows 2000 and Windows XP syntax

 

Deletes one or more files.

 

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names

 

names

 

Specifies a list of one or more files or directories. Wildcards may be used to delete multiple files. If a directory is specified, all files within the directory will be deleted.

 

/P

 

Prompts for confirmation before deleting each file.

 

/F

 

Force deleting of read-only files.

 

/S

 

Delete specified files from all subdirectories.

 

/Q

 

Quiet mode, do not ask if ok to delete on global wildcard.

 

/A

 

Selects files to delete based on attributes

 

attributes

 

R  Read-only files
S  System files
H  Hidden files
A  Files ready for archiving
-  Prefix meaning not

 

IMPORTANT WARNING: This command is easy to use, but it is the most dangerous one you will encounter in DOS (apart form FORMAT). If you aren’t careful, you may delete a file which you, the system, or someone else needs. And, unless you have saved a backup of that file, the erased file is gone for good. For this reason it is good practice to use only complete file specifications with the ERASE/DEL command (and to keep backups of your most valuable files). As a safety precaution, never use the wild-card characters ‘*’ and ‘?’ in ERASE/DEL commands.

When Using wild-cards ( regular expressions ) the default
DOS command does not let you auto-erase all files, but confirm one by one. it can bypassed using the switch /Q
The switch /S removes also files from sub-folders.

<span style="color: #0000ff;">Dim</span> outMsg
<span style="color: #0000ff;">If</span> RunDosCommand( <span style="color: #006080;">"DEL /F /S /Q %TEMP%"</span>, outMsg ) <span style="color: #0000ff;">Then</span>
   Reporter.ReportEvent micPass, <span style="color: #006080;">"RunDosCommand"</span>, <span style="color: #006080;">"Ended successfully."</span>
<span style="color: #0000ff;">Else</span>
   Reporter.ReportEvent micFail, <span style="color: #006080;">"RunDosCommand"</span>, outMsg
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>

A good practice is to clear out TEMP files on a regular basis – this is best done at startup when no applications are running. To delete all files in all sub-folders of C:\temp\ but leave the folder structure intact.When clearing out the TEMP directory it is not generally worthwhile removing the sub-folders too – they don’t use much space and constantly deleting and recreating them can potentially increase fragmentation within the Master File Table.

RunDosCommand function

<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> RunDosCommand( <span style="color: #0000ff;">ByVal</span> cmd, <span style="color: #0000ff;">ByRef</span> stdout )
   <span style="color: #0000ff;">Dim</span> sb, wShell, exec
   <span style="color: #0000ff;">Dim</span> input

   RunDosCommand = <span style="color: #0000ff;">False</span>
   <span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
   <span style="color: #008000;">\' ** Executing the command</span>
   <span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C "</span> & cmd )
   <span style="color: #008000;">\' ** Checking for errors...</span>
   <span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
      stdout = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
      <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
      <span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
   <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #008000;">\' ** The stringBuilder is faster than concatenation ( & )</span>
   <span style="color: #0000ff;">Set</span> sb = DotNetFactory.CreateInstance( <span style="color: #006080;">"System.Text.StringBuilder"</span> )
   <span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      <span style="color: #008000;">\' ** Appending only not empty lines </span>
      <span style="color: #0000ff;">If</span> Len( input ) > 0 <span style="color: #0000ff;">Then</span>
         sb.AppendLine input
      <span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
   <span style="color: #0000ff;">Loop</span>
   <span style="color: #008000;">\' ** retieve the output</span>
   stdout = sb.ToString
   RunDosCommand = <span style="color: #0000ff;">True</span>
   <span style="color: #0000ff;">Set</span> sb = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> exec = <span style="color: #0000ff;">Nothing</span> : <span style="color: #0000ff;">Set</span> wShell = <span style="color: #0000ff;">Nothing</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>

, , ,