Solved

How to Set colFiles to C:\%COMPUTERNAME%

Posted on 2011-09-22
3
528 Views
Last Modified: 2012-05-12
I have one script running daily on a server that copies files into C:\%COMPUTERNAME% for multiple computers.

This creats a folder in the C: drive of each computer labeled as that computer's name.

Now, I am creating a script to delete any files in C:\%COMPUTERNAME% that is older than one month.  However, when I use the following, I get an invalid character error message.  How can I make this work?

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\%COMPUTERNAME%'} Where " _
        & "ResultClass = CIM_DataFile")

Note:  When I change %COMPUTERNAME% to an actual folder like C:\TEST  to test, it works fine.  So I know I'm not using the correct syntax.

Additional Note:  Script will be run on Windows Server 2003 and 2008.

Thank you.
0
Comment
Question by:Irrylyn
  • 2
3 Comments
 

Author Comment

by:Irrylyn
Comment Utility
Here is my complete .vbs:



strComputer = "."

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

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\%COMPUTERNAME%'} Where " _
        & "ResultClass = CIM_DataFile")

strCurrentDate = Now

For Each objFile In colFiles
    strFileDate = WMIDateStringToDate(objFile.CreationDate)
    intHours = DateDiff("m", strFileDate, strCurrentDate)
    If intHours >= 3 Then
        Wscript.Echo objFile.Delete
    End If
Next

Function WMIDateStringToDate(dtmInstallDate)
    WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
        Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
            & " " & Mid (dtmInstallDate, 9, 2) & ":" & _
                Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
                    13, 2))
End Function
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
Comment Utility
Try this.

Set objShell = CreateObject("WScript.Shell")
strComputerName = objShell.ExpandEnvironmentStrings("%computername%")

strComputer = "."

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

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\" & strComputerName & "'} Where " _
        & "ResultClass = CIM_DataFile")

strCurrentDate = Now

For Each objFile In colFiles
    strFileDate = WMIDateStringToDate(objFile.CreationDate)
    intHours = DateDiff("m", strFileDate, strCurrentDate)
    If intHours >= 3 Then
        Wscript.Echo objFile.Delete
    End If
Next

Function WMIDateStringToDate(dtmInstallDate)
    WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
        Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
            & " " & Mid (dtmInstallDate, 9, 2) & ":" & _
                Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
                    13, 2))
End Function

Open in new window

~bp
0
 

Author Closing Comment

by:Irrylyn
Comment Utility
This was what I needed, thank you!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now