Solved

VBS issue

Posted on 2011-09-23
10
343 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:clynch302
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 36589013
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?
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 36589151
What error are you getting?

~bp
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 36589233
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

0
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!

 

Author Comment

by:clynch302
ID: 36589935
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.
0
 

Author Comment

by:clynch302
ID: 36589964
Also, when I add my computer name it does not error out but it also does not rename the .bkf to .txt.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36591064
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.
0
 

Author Comment

by:clynch302
ID: 36600274
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.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 36614685
OK, try it by deleting the target file (if it exists), and then using Move instead.  Also, this doesn't use the CIM_DataFile query, so it might be a bit quicker.

Make sure you test in on a test set of files first.

Regards,

Rob.
Dim objFSO, startFolder, OlderThanDate
Set objFSO = CreateObject("Scripting.FileSystemObject")
startFolder = "\\server\itdept\adbackup"
OlderThanDate = DateAdd("d", -2, Date) ' 2 days 
DeleteOldFiles startFolder, OlderThanDate 

RenameFiles startFolder

Sub DeleteOldFiles(folderName, BeforeDate) 
	For Each file In objFSO.GetFolder(folderName).Files
		If file.DateLastModified < BeforeDate Then
			fso.DeleteFile(file.Path), True
		End If 
	Next 
	For Each subFolder In objFSO.GetFolder(folderName).SubFolders
		DeleteOldFiles subFolder.Path, BeforeDate 
	Next 
End Sub

Sub RenameFiles(folderName)
	For Each objFile In objFSO.GetFolder(startFolder).Files
		If objFile.Extension = "bkf" Then
			strNewName = objFile.Path & ".txt"
			If objFSO.FileExists(strNewName) = True Then objFSO.DeleteFile strNewName, True
			objFSO.MoveFile objFile.Path, strNewName
		End If
	Next
	For Each subFolder In objFSO.GetFolder(folderName).SubFolders
		RenameFiles subFolder.Path
	Next 
End Sub

Open in new window

0
 

Author Closing Comment

by:clynch302
ID: 36710994
Thank you for taking the time to assist me.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36713576
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

729 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