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
Solved

How to Set colFiles to C:\%COMPUTERNAME%

Posted on 2011-09-22
3
557 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
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 53

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

861 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