?
Solved

How to Set colFiles to C:\%COMPUTERNAME%

Posted on 2011-09-22
3
Medium Priority
?
574 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 56

Accepted Solution

by:
Bill Prew earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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 …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

764 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