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

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

  • Comments 1
  • Likes

How Do I Compress Files?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! After some time without needing help, I've run into a problem about which I have been unable to discover any information. Perhaps you can aid me. Assume you have (1) a single file, and (2) a folder that contains a number of files of various types. Further assume that you wish to store those files in a format that is compressed as efficiently as possible. I am unable to find any information on how to script this. Many thanks for the information you may be able to provide.

- DS

SpacerHey, Scripting Guy! Answer

Hi DS,

The API we use to compress files via the filesystem is licensed, and one of the restrictions was that we could not provide a command line interface to it. I just wrote a TechNet Magazine article (it will be out in a month) that talks about using the makecab.makecab api to create cab files, which does a good job of compressing files. This might work for your needs. In the Microsoft Press book, Windows PowerShell Scripting Guide, I also talk about using the .NET Framework compression classes to create compressed files. (You can also purchase a third-party compression utility that provides easy command-line compression. It is not very expensive and may be the easiest way to go.)

 

How Do I Delete Files by Using Wild Card Characters?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m very new to scripting and am trying to create a script that will unregister a dll and then delete files that are on the system. The dll part I think I got figured out. Here’s what I have:

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "regsvr32.exe c:\windows\downloaded program files\rsclientprint.dll /u"

I need to delete 14 files. They are all rspclient_####.dll and .rll files, and are located in the C:\windows\system32 directory. Thanks for any help and suggestions.

- KV

SpacerHey, Scripting Guy! Answer

Hi KV,

This article talks about using wild card patterns when deleting files. The system32 directory is very important, however, and I would recommend you test this technique and possibly backup the computer before running it. Your best bet would be to test your script on a virtual machine with undo disks enabled. You can download Virtual PC 2007. It is a free product and works very well.

 

How Do I Script with Office Access?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I would like to see stuff about scripting with Office Access.

- OG

SpacerHey, Scripting Guy! Answer

Hi OG,

We do have some stuff about scripting with Office Access. In reality Office Access is just a database after all, and any ADO script would work as long as you change the provider information. But here are some more specific resources:

Importing and Exporting XML Data Using Microsoft Access

Compacting an Access Database

Reading Data from a Microsoft Access Database

“Hey, Scripting Guy!” Archive

Community-Submitted Scripts Repository

 

How Do I Change Fixed IP Addresses to DHCP?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a question. Currently I am working for a major international company with a rather huge network across the globe. I am assigned to a project to resolve some inheritance from the past. In Europe we have nine production sites where all workstations are assigned with fixed IP addresses. Management has ordered us to get rid of the fixed IP addresses for all client workstations and convert them to DHCP. Do you have some suggestions?

- DP

SpacerHey, Scripting Guy! Answer

Hi DP,

Here is a really good series of nine articles about automating TCP/IP networking on client computers.

 

How Can I Get a Script to Work on Windows Server 2003 That Already Works on Windows XP?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! This Script works perfectly on a Windows XP machine, but It does not work on a Windows Server 2003 machine. The error is Line 5, Char: 1: Error: Not Found, Code 80041002, Source: SWbemServicesEX. Any Ideas why this works on Windows XP and not on Windows 2003?

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _
       & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objSoftware = objWMIService.Get("Win32_Product")
errReturn = objSoftware.Install("c:\uptmagnt-MS-5.0.21-x86.msi",,True)
WScript.Echo errReturn

- JA

SpacerHey, Scripting Guy! Answer

Hi JA,

This is because by default the MSI provider is not installed on Windows Server 2003. To install it, open Control Panel, choose to add the Microsoft Windows component Management and Monitoring Tools, and then click Details. The WMI Windows Installer Provider is available there. It is a small addition, but I believe it requires a reboot for installation to complete.

 

How Can I Copy a File to a New Location and Rename It If a File of the Same Name Already Exists?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I copy a file that always has the same name from one location to another and check whether the file exists and if yes rename it at the second location with a running number. For example, the file xyz.bak would be changed to xyz1.bak. In this way I could copy backup images to an external hard drive and have them sorted by name.

- SG

SpacerHey, Scripting Guy! Answer

Hi SG,

Use the fileexists method from the filesystemobject to see if the file exists. Use the copyfile method from the filesystemobject to rename the file. Here is an interesting article that talks about incrementally renaming files based upon the file creation date. Here is a sample script that will illustrate some of the concepts.

CheckFileRename.vbs

Dim path,file, extension, originalFile
path = "C:\fso"
file = "a"
extension = ".txt"
originalFile = path & "\" & file & extension
copyPath = "C:\fso1"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(originalFile) Then
 WScript.Echo "Original file exists. Proceeding to copy"
Else
 WScript.Echo "Original file does not exist. Exiting script."
 WScript.Quit
End If

If objFSO.FileExists(copyPath & "\" & file & extension) Then
 WScript.Echo "copy file also exists. Incrementing name"
 file = file & "1"
 copyPath = copyPath & "\" & file & extension
 objFSO.CopyFile originalFile, copyPath
 Else
 WScript.Echo "Copy file does not exist. Copying same"
 copyPath = copyPath & "\" & file & extension
 WScript.Echo copypath
 WScript.Echo originalFile
 objFSO.CopyFile originalFile, copyPath
End If

Well, believe it or not, another week has come and gone. It is the weekend, baby! Hope you have a safe and enjoyable weekend and that you will join us next week for Outlook week when we will look at using Windows PowerShell to automate the Outlook email client. See you then.

 

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
  • Hi! What does this error mean?

    Line: 10

    Symbol: 1

    Code: 80041002

    Source: SWbemServicesEx

    set wshshell=CreateObject("wscript.shell")

    call wshshell.run("%comspec% /c mofcomp -N:root\cimv2 %SystemRoot%\system32\Wbem\Wbemcons.mof&&pause",9,-1)

    Computer="."

    Set Service = GetObject("winmgmts:\\" & Computer & "\Root\CIMV2")

    Set oFilter = Service.Get("__EventFilter").SpawnInstance_()

    oFilter.Name = "MonPrnJob"

    oFilter.QueryLanguage = "WQL"

    oFilter.Query = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PrintJob'"

    oFilter.Put_

    Set oConsumer = Service.Get("LogFileEventConsumer").SpawnInstance_()

    oConsumer.Name = "MonPrnJobLog"

    oConsumer.FileName = "C:\PrintLog\MonPrn.txt"

    oConsumer.Text = "Компьютер: %TargetInstance.Name%, Имя принтера: %TargetInstance.Name%, Документ: %TargetInstance.Document%, Пользователь: %TargetInstance.Owner%, Всего страниц: %TargetInstance.TotalPages%, Время печати:  %TargetInstance.TimeSubmitted%, Отпечатано страниц: %TargetInstance.PagesPrinted%"

    oConsumer.Put_

    Set oFilter = Service.Get("__EventFilter.Name='MonPrnJob'")

    Set oConsumer = Service.Get("LogFileEventConsumer.Name='MonPrnJobLog'")

    Set oBinding = Service.Get("__FilterToConsumerBinding").SpawnInstance_()

    oBinding.Filter = oFilter.Path_

    oBinding.Consumer = oConsumer.Path_

    oBinding.Put_

    OS Windows 2008 SP2 Std