Solved

How to Set colFiles to C:\%COMPUTERNAME%

Posted on 2011-09-22
3
561 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 54

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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