Jump to content

Recommended Posts

Posted

Hey all,

Unimportant information:

One of my jobs is to make sure that our NAS server for a few customers doesn't fill up. The servers can manage their own space, but the way the system was sold is that both servers were put on the NAS as 2 folders, instead of creating two different arrays. So, when server 1 hits its threshold, it sees that the NAS is 95% full, and moves it down to 90%. But, Server 1 only has access to Server 1 files, so while we may have 12 months online for server 2, we may only have 9 months online for server 1.

Somewhat less unimportant information:

What I'd like to do is create a VBScript that I can run monthly to delete all files older then x days (probably 300). I'd also like to have a functionality of writing all the files that are to be deleted to a text file first, that way they can be reviewed and logged if needed.

This is sort of what I'm working with thus far:

strDate = "20031102000000.000000+000"

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'")
For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next

As you can see there are two problems. One, its doing older then date Nov 2nd, 2003, instead of automagically figuring out 300 days ago. Two, it's echoing the file names to the console instead of writing them to a file.

And three its not actually deleting the file, but I was planning on having that be a separate file anyway, something like this:

strDate = "20090701000000.000000+000"

DIM fso, LogFile

strComputer = "."
Set fso = CreateObject("Scripting.FileSystemObject")
Set LogFile = fso.CreateTextFile("d:\VBS\OldFileLog.txt", True)
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'" & _ 
    " AND Drive = 'D:'")
For Each objFile in colFiles
    objFile.Delete
Next

Thanks in advance for any help!!

-Brian

Posted

to get the date 300 days ago, use:

DateAdd("d",-300,date())

also, to write them to a file, you would use

objFile.WriteLine(objFile.Name)

Posted
to get the date 300 days ago, use:

DateAdd("d",-300,date())

also, to write them to a file, you would use

objFile.WriteLine(objFile.Name)

I don't understand how to get the "objFile.WriteLine(objFile.Name)" to work, where do I get the name from?

I'm doing this now:

strDate = "20090701000000.000000+000"
strComputer = "."

dt = Replace(Date,"/","-")
strFileName = "D:\VBS\" & dt & "-OldFiles.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFileName)

Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'" & _ 
    " AND Drive = 'D:'")

For Each objFile in colFiles
    objFile.WriteLine (objFile.OldFiles) 
Next

objFile.Close

But when I run it I get "Microsoft VBScript runtime error: Object doesn't support this property or method: 'objFile.OldFiles' "

I feel like I don't understand how the referencing is done, because I can run a script like this that works fine:

strComputer = "."

dt = Replace(Date,"/","-")
strFileName = "D:\VBS\" & dt & "-FreeSpace.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFileName)

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DeviceID = 'C:'")

For Each objDisk in colDisks
    objFile.WriteLine (objDisk.FreeSpace)
Next

objFile.Close

As you can see it's using objDisk.FreeSpace, the only other reference to FreeSpace I see in the code is in the strFileName.

Thanks for the help!

Posted
the FreeSpace comes from the Win32_LogicalDisk class

Ah, ok.

I think I'm running into an issue where I just want to make this script work for this one purpose without trying to learn anything else, so I'm missing vital pieces of information.

When I run the script thusly:

strComputer = "."

dt = Replace(Date,"/","-")
strFileName = "D:\VBS\" & dt & "-FreeSpace.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFileName)

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DeviceID = 'C:'")

For Each objDisk in colDisks
    objFile.WriteLine(objFile.Name)
Next

objFile.Close

I get: Object doesn't support this property or method: 'objFile.Name'.

I think unfortunately I'm going to have to tackle this problem from the ground up so I actually understand what I'm doing. Any good resources out there?

Posted
Ah, ok.

I think I'm running into an issue where I just want to make this script work for this one purpose without trying to learn anything else, so I'm missing vital pieces of information.

When I run the script thusly:

strComputer = "."

dt = Replace(Date,"/","-")
strFileName = "D:\VBS\" & dt & "-FreeSpace.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFileName)

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DeviceID = 'C:'")

For Each objDisk in colDisks
    objFile.WriteLine(objFile.Name)
Next

objFile.Close

I get: Object doesn't support this property or method: 'objFile.Name'.

I think unfortunately I'm going to have to tackle this problem from the ground up so I actually understand what I'm doing. Any good resources out there?

Since this script is for free space, you would do:

objFile.WriteLine(objDisk.FreeSpace)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...