Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


My script doesn't work...

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,41531
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> My script doesn't work...
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 My script doesn't work... - 12/29/2006 10:29:14 AM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
Sorry about the non-descript subject but I know next to nothing about scripting and don't really know what's wrong.

I found this script at the Microsoft scripting site (http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1117.mspx) and every so slightly modified it but it doesn't work:

dtmDate = Date - 60

strDay = Day(dtmDate)

If Len(strDay) < 2 Then
   strDay = "0" & strDay
End If

strMonth = Month(dtmDate)

If Len(strMonth) < 2 Then
   strMonth = "0" & strMonth
End If

strYear = Year(dtmDate)

strTargetDate = strYear & strMonth & strDay

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='n:\bmg000\'} Where " _
       & "ResultClass = CIM_DataFile")

For Each objFile In FileList
   strDate = Left(objFile.CreationDate, 8)
   If strDate < strTargetDate Then
       objFile.Delete
   End If
Next

When run I get the following error:

Script: n:\60 day delete.vbs
Line 27
Char 1
Error: 0x80041002
Code: 80041002
Source: (null)

Line 27 is "For Each objFile In FileList" and is not part of the script that I modified. I changed the 7 day date to 60 days and removed the reference to BAK files so it should delete ALL files older than 60 days.

Anyone have an idea?

Thanks,

Mike
 
 
Post #: 1
 
 RE: My script doesn't work... - 12/29/2006 11:01:25 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Try removing the last "\" from the name.

n:\bmg000\

change it to

n:\bmg000

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to big0mike)
 
 
Post #: 2
 
 RE: My script doesn't work... - 12/29/2006 11:20:01 AM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
OK, no error now but nothing appears to be happening. Again, I don't know much about VBS but I assumed I'd see something like a DOS window open like when I execute a BAT file.

(in reply to big0mike)
 
 
Post #: 3
 
 RE: My script doesn't work... - 12/29/2006 11:23:17 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
No, you're not going to see anything happen and nothing will tell you it is complete unless you make it do so with a wscript.echo or a msgbox or some other method.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to big0mike)
 
 
Post #: 4
 
 RE: My script doesn't work... - 12/29/2006 5:44:43 PM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
I didn't even see that it had deleted any files. I took a directory of files and copied it to that N drive and I've got a program that compares directories and each directory seemed untouched after executing the file.

Is there a simple method to open a window that displays every file as it deletes?

(in reply to big0mike)
 
 
Post #: 5
 
 RE: My script doesn't work... - 12/29/2006 6:12:07 PM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
I believe that since you are copying the files over, they all have a creation date as of today so it is not matching your criteria.  Perhaps changing it so it looks for LastModified date may be something to look into.
strDate = Left(objFile.LastModified, 8).

The way I would test it before having it delete anything is to echo things out and use cscript to see the output.  Notice how I simply commented out your delete and put a wscript.echo there.  This should show you what files met the criteria specified.  Run the script by opening a command prompt and typing:
cscript.exe thenameofthisscript.vbs

If no file names are showing then that means the files aren't meeting your criteria.


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to big0mike)
 
 
Post #: 6
 
 RE: My script doesn't work... - 12/30/2006 2:43:40 AM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
I thought about the creation date thing as well and looked for that. It appeared that all of the directories that were created had a creation date of 12/29 but the files that were copied had their original creation date.

So, cscript.exe is how you run it in a command prompt? That's perfect. I think using that method along with the addition of the echo you added, and uncommenting the delete command, will work just right.

I won't be back in the office until Tuesday so I'll try it out then.

Thanks for the help.

Mike

(in reply to dm_4ever)
 
 
Post #: 7
 
 RE: My script doesn't work... - 1/2/2007 6:11:47 AM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
Well, I added the echo line and I'm running it in a CMD window nothing happens. But, it just dawned on me... that bmg000 foler has no actual files in it. It contains one folder with another 5 folders in it with multiple folders/files buried within. So, I'm sure that's why it's not doing squat.

I assume there is a way to rig the script to look into subfolders?

Mike

(in reply to big0mike)
 
 
Post #: 8
 
 RE: My script doesn't work... - 1/2/2007 7:51:07 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Will you be running your script locally or remotely?  If you intend to run it from your PC, then you may want to use the FileSystemObject instead of WMI for this.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to big0mike)
 
 
Post #: 9
 
 RE: My script doesn't work... - 1/2/2007 8:04:06 AM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
I'll be running it locally...

Well, like I said originally I don't know squat about VBS so it would probably be easier for me to do a search and view it by date and delete all the old stuff than to have to figure a whole new language out.

Thanks for the help.

Mike

(in reply to dm_4ever)
 
 
Post #: 10
 
 RE: My script doesn't work... - 1/2/2007 8:30:23 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
I took a script I had posted in another thread here that did a file count and modified it to look at the creationdate. 


      

< Message edited by dm_4ever -- 1/2/2007 8:32:15 AM >


_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to big0mike)
 
 
Post #: 11
 
 RE: My script doesn't work... - 1/2/2007 9:08:15 AM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
Well, I copied your script and the only thing I changed was c:\temp to d:\ftproot\bmg000 and it doesn't do anything even though I know there are files that are years old within those subdirectories.

So I went back to the original and did what you suggested and try the LastModified option and point it to a specific directory that has files in it and I do get the list. But that option will not work because of the number of directories I would have to search.

But, at least I know something is working now...

(in reply to dm_4ever)
 
 
Post #: 12
 
 RE: My script doesn't work... - 1/2/2007 9:18:58 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You can do the same with this one and this one should look in all subdirectories.

change:
    objFile.DateCreated
to 
    objFile.DateLastModified


_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to big0mike)
 
 
Post #: 13
 
 RE: My script doesn't work... - 1/2/2007 10:00:08 AM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
Yep, that worked. :)

So, why would it not do anything but then changing DateCreated to DateLastModified (both of which should be valid as far as the age of the file) make it work?

Now another dilemna... You can't really use either argument for deleting folders, can you? I ran the script and now I've got a ton of empty folders. But, and I'm not sure this is how it works, if a file is modified the directory that it's in would have a LastModified date the same as the last modified file. Or maybe not...

I see that there is an "objFolder" so it must be handled in a similar way. Would something like this be feasible?

If objFolder.DateLastModified < strTargetDate Then
   WScript.Echo objFolder.Name
   objFolder.Delete

(in reply to dm_4ever)
 
 
Post #: 14
 
 RE: My script doesn't work... - 1/2/2007 10:41:11 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
From what I've seeen, the creation date changes to the date you actually copy the file to a location. 

Yes, you can delete the folder in the same manner as the files.


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to big0mike)
 
 
Post #: 15
 
 RE: My script doesn't work... - 1/3/2007 3:58:39 AM   
  big0mike

 

Posts: 9
Score: 0
Joined: 12/29/2006
Status: offline
I added that last routine and the script runs, deletes the first directory and stops giving an error on line 33 which is DeleteOldFiles(subfolder.Path).

I noticed as it ran that it deleted the directory but not files individually so it must be seeing the modified date of the folder and just deleting it. Am I safe in assuming that there will be no case when a directory will contain a new file but the directory itself will not have a LastModified date the same as the newest file? What about a directory that goes, let's say, 4 levels deep. At the bottom is a newer file so it's parent directory will have an identical LastModified date but will that directory's parent directory have the same LastModified date? I can't have it deleting a top level directory if x levels deep there are new files that should remain.

So, back to the original script... Would there need to be 3 subroutines?


      

(in reply to dm_4ever)
 
 
Post #: 16
 
 RE: My script doesn't work... - 1/3/2007 6:23:49 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You could probably spilt things up into different subs that do different things or you could just  play with the logic a little.  Maybe check the File and Folder count of each folder and if it is not equal to 0 then delete it.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to big0mike)
 
 
Post #: 17
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> My script doesn't work... Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts