Solved

How to Set colFiles to C:\%COMPUTERNAME%

Posted on 2011-09-22
3
570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:Irrylyn
ID: 36582436
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 55

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 36583236
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
ID: 36583684
This was what I needed, thank you!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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