Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (01/16/09)

Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (01/16/09)

  • Comments 1
  • Likes

How Do I Set Process Affinity?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have this application that I would like to set process affinity for. I know I can use Task Manager, right-click the process, and choose to set affinity, but I would like to be able to do this from a script. Can this be done? I asked a newsgroup on the Internet, and someone said something about hacking the registry, but I am not too keen on doing that.

- AJ

SpacerHey, Scripting Guy! Answer

Hi AJ,

Using Windows PowerShell, this can actually be done rather easily. The trick is in leveraging the power of the .NET framework. We can use the System.Diagnostics.Process .NET Framework class. If you start an instance of Notepad, and then use the Get-Process cmdlet to retrieve that instance, you can see this here:

$notepad = Get-Process -name notepad

Now you want to set the processor affinity to the second processor on the system:

$notepad.ProcessorAffinity = 0x0002

 

How Can I Create a New Office Worksheet and Have It Appear in a Specific Location?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to create a new Office Excel worksheet, but I need it to appear at a specific location. For example, if I have three spreadsheets named test1, test2, and test3, how can I add a worksheet 1a in front of the test2 worksheet?

- SG

SpacerHey, Scripting Guy! Answer

Hi SG,

The key is to use the add method from the worksheets object. When you call the add method, the name of the spread sheet you have in the first position is the one in which your spread sheet will appear in front of. So if you want your spread sheet test1a to appear in front of test2, you give the add method a spread sheet object that represents the test2 spread sheet. You get the spread sheet object by using the item method as seen here:

$test2 = $workbook.WorkSheets.Item("test2")

Once you have your spread sheet object, you feed it to the add method as seen here:

$newSheet = $workbook.worksheets.add($test2)

Now you probably want to rename the newly created spread sheet. To do that you supply a new name for the name property of the new spread sheet. This is seen here:

$newSheet.name = "Test1a"

The completed script is seen here:

$excel = new-object -comobject excel.application
$excel.visible = $true
$workbook = $excel.workbooks.add()
$workbook.WorkSheets.item(1).Name = "test1"
$workbook.WorkSheets.item(2).Name = "test2"
$workbook.WorkSheets.item(3).Name = "test3"
$test2 = $workbook.WorkSheets.Item("test2")
$newSheet = $workbook.worksheets.add($test2)
$newSheet.name = "Test1a"

After the script has been run, the following Office Excel workbook appears. If you look closely, you can see the newly created Test1a spreadsheet:

Image of the workbook that the script creates

 

How Do I Find MP3 Files in a Particular Directory?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I was reading the article about searching for MP3 files on my computer, but I need to only search a particular directory. Here is the script that was in the previous article:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where Extension = 'mp3' OR Extension = 'wma'")

For Each objFile in colFiles
    Wscript.Echo "Name: " & objFile.Name
Next

Can this script be changed to find MP3 files in a particular directory?

- PU

SpacerHey, Scripting Guy! Answer

Hi PU,

Yes, the script can be modified. What you will need to do is modify the where clause. Because you said you were only interested in MP3 files, I deleted the or section of the script to remove the extension = 'wma'. Then I added an AND clause and specified the path. One thing to keep in mind with cim_datafile is that the path separators need two back slashes, not just one. In addition, I am cheating a little bit. By using \\fso\\ for my path, my script will find MP3 files in any fso directory I have on my computer. This can be quite cool, if for instance like me, you have multiple drives on your computer, and you have multiple music folders. Here is the SearchFolderForMP3.vbs script:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where Extension = 'mp3' AND path = '\\fso\\'")

For Each objFile in colFiles
    Wscript.Echo "Name: " & objFile.Name
Next

Top of pageTop of page

How Do I Keep from Overwriting Data Inside a Loop?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I am trying to write a script that will use a predefined server list, perform a WMI query, and write data that is returned to an Office Excel spreadsheet. My problem is that I keep overwriting the disk information in my Office Excel cell while I am in the loop. How can I add all the free space from all my drives together to store into the Office Excel cell?

- RS

SpacerHey, Scripting Guy! Answer

Hi RS,

What you will need to do is to use a variable to hold all your free disk space. Then you want to add the disk space to the data. To do this, use the following type of construction:

A = A + 1

Here is an example of using this:

A = 5
For i = 1 To 3
A = A + 1
WScript.Echo A
Next

When we run the script, we add one to the value of A each time through the loop. The results are seen here:

6
7
8

Top of pageTop of page

How Do I Prevent Pop-Ups from Spawning When I Run VBScript Scripts?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! When I run your scripts that use VBScript, I end up with all these pop-up boxes. I mean, some of your scripts are absolutely useless because of the number of pop-up boxes. I sit there all day clicking OK, OK, OK. Is there anything I can do?

- JJ

SpacerHey, Scripting Guy! Answer

Hi JJ,

Dude, I am sorry! There are a number of things you can do. First of all, you need to read this Web page. It talks about running VBScripts and the difference between cscript and wscript. When you double-click a .vbs file, your computer uses wscript to run the script. The biggest difference is that you will see the pop-up dialog boxes from this command:

Wscript.echo "hi"

Sometimes this is exactly what you want. On the other hand, if the script is printing out the name of every file in your entire computer, then the wscript.echo could produces thousands, or even millions of popup boxes. To prevent this, you need to open a cmd prompt and type cscript in front of the script name. This is seen here:

Cscript c:\fso\mytest.vbs

This forces the script to run in cscript instead of wscript. Again the big advantage is getting rid of the pesky popup boxes. But what if you want a pesky popup box, just not millions of them? Then you use the msgbox function to produce the desired result. Now you can change the default scripting engine, and this is something I recommended when I wrote all the VBScripts for the Windows Vista Resource Kit. (That's the first edition. In the second edition, I wrote all the scripts in Windows PowerShell.) This technique is discussed in more detail in the Microsoft VBScript Step By Step book.

From a best practice, you should always check for the presence of cscript when writing a script that is likely to produce a bunch of annoying pop-up dialog boxes. A perfect candidate for that would be the script that I modified for PU above. Here is an example of checking for cscript before running the script:

CheckForCscript
strComputer = "."
FileExtension = "txt"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
  ("Select * from CIM_Datafile Where Extension = '" + fileExtension + "' AND path = '\\fso\\'")

For Each objFile in colFiles
    Wscript.Echo "Name: " & objFile.Name
Next

Sub CheckForCscript
 If UCase(Right(Wscript.FullName, 11)) = "WSCRIPT.EXE" Then
    MsgBox "This script must be run under CScript."
    Wscript.Quit
 End If
End Sub

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment