Link to home
Start Free TrialLog in
Avatar of clynch302
clynch302Flag for United States of America

asked on

VBS issue

I have VBS that I mashed together that is supposed to look in a folder and take .bkf extensions and change them to .txt. Then it is supposed to delete older files.

It gives me an error on line 9.

Can someone help me on this??

Thanks
Dim fso, startFolder, OlderThanDate

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='\\server\itdept\adbackup'} Where " _
        & "ResultClass = CIM_DataFile")
For Each objFile In FileList
    If objFile.Extension = "bkf" Then
        strNewName = objFile.Drive & objFile.Path & _
            objFile.FileName & "." & "txt"
            errResult = objFile.Rename(strNewName)
        Wscript.Echo errResult
    End If
Next


 Set fso = CreateObject("Scripting.FileSystemObject")
 startFolder = "\\server\itdept\adbackup"
 OlderThanDate = DateAdd("d", -2, Date) ' 2 days 
 DeleteOldFiles startFolder, OlderThanDate 
 
  
 Function DeleteOldFiles(folderName, BeforeDate) 
 Dim folder, file, fileCollection, folderCollection, subFolder 
 Set folder = fso.GetFolder(folderName) 
 Set fileCollection = folder.Files 
 For Each file In fileCollection 
 	If file.DateLastModified < BeforeDate Then 
 		fso.DeleteFile(file.Path) 
 	End If 
 Next 
 Set folderCollection = folder.SubFolders 
 For Each subFolder In folderCollection 
 	DeleteOldFiles subFolder.Path, BeforeDate 
 Next 
 End Function

Open in new window

Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

I don't think it likes computer name being define as "."

strComputer = "."

Are you using this for more than one computer or are you planning on passing computer name as param?

Trying sticking actual computer name there and see what happens.

Also, do you have a different file name other than bkf on that folder?
Avatar of Bill Prew
Bill Prew

What error are you getting?

~bp
The error says (null) with some error code. That's what I got when I attempted to test it to see if I could figure out what could be wrong.

I wanted to clarify that defining your computer as strComputer="." doesn't mean there is something wrong with that declaration.

I would try using this to diagnose vmi to see if something is amiss with it.

http://windowsxp.mvps.org/repairwmi.htm

Avatar of clynch302

ASKER

This is used on my computer only.  I have a folder that files are copied to that are .bkf. I need them to be .txt. So this is basically all ran from 1 system. I have a server unc in that I was testing.
Also, when I add my computer name it does not error out but it also does not rename the .bkf to .txt.
Hi, CIM_DataFile won't allow you to enumerate a remote folder.  If you have admin rights, you should set strComputer to the remote computer, then make the path relative to that computer.

strComputer = "server"
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='D:\itdept\adbackup'} Where " _
        & "ResultClass = CIM_DataFile")

Open in new window


Regards,

Rob.
Ya, no dice.n I am still getting the same error. Basically all I need to do is rename bkf to txt and then delete the the oldest files. In my script the delete portion works just not the extension rename.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for taking the time to assist me.
No problem. Thanks for the grade.

Regards,

Rob.