Tech Support banner

Status
Not open for further replies.
1 - 7 of 7 Posts

·
Registered
Joined
·
5 Posts
Discussion Starter #1
I need some help with a VBScript I'm writing. The script searches through a directory and all its subdirectories for files that contain "log" in their extension and are older than 7 days. When running it, I encountered a problem: one of the files that fits the requirements for deletion is in use. How can I detect if a file is in use and if it is, skip over it without deleting it? Thanks in advance! :smile:
 

·
Registered
Joined
·
2,276 Posts
So if it's in use i suspect that windows went and told you it's in use and skipped over it anyway? am i right there?
So in reality you want to surpress the error?

Actually i have an idea... the .log files, are they ordered in any particular way? (e.g. chronologically) because if it's like a "27-05-08.log" then you could simply get the date and compare the log's name with the string "[date].log" and then decide whether to delete the file or not...

Sorry i haven't written anything in vbscript so i can't give you any actual help on a script...

Cheers,
Jamey
 

·
Registered
Joined
·
5 Posts
Discussion Starter #3
So if it's in use i suspect that windows went and told you it's in use and skipped over it anyway? am i right there?
So in reality you want to surpress the error?

Actually i have an idea... the .log files, are they ordered in any particular way? (e.g. chronologically) because if it's like a "27-05-08.log" then you could simply get the date and compare the log's name with the string "[date].log" and then decide whether to delete the file or not...

Sorry i haven't written anything in vbscript so i can't give you any actual help on a script...

Cheers,
Jamey
Actually, what happens is that windows will throw a permissions error and the script will have to wait until the user clicks "ok" on the error message before closing. I believe it does clean up everything else that doesn't throw an error so yeah I guess I do want to just suppress the error so the script can end without any human interaction. I currently have a work-around in place where I have it check to see if the filename matches one of the filenames I've hardcoded and if it does, just skip trying to delete it. It's working fine but I'd prefer something more reliable so if files other than the ones I've hardcoded are being used, it doesn't trip up the script.

Thanks for the suggestions Jamey. Anyone else have any ideas? :confused:
 

·
Registered
Joined
·
2,276 Posts
Oh okay, yeah i can imagine the error "you need permission to mess with this file" with an ok/cancel/try again sorta thing, you're using xp and i'm on vista(regretably) so the error will be different in my mind to on your pc...

Okay I HAVE AN ANSWER!! :grin:

I just took a nice little trip into vbscript and tried simulating your situation (by trying to delete an executeable that's running and one that isn't)... So that i could understand what's going on...

So here's the script i used to start with (p.s. it's simple because i'm a supernoob with vbscript):

Code:
dim filesys, demofile
set filesys = CreateObject ("Scripting.FileSystemObject")


set demofile = filesys.GetFile("c:\Project1.exe")

demofile.Delete
set demofile = filesys.GetFile("c:\Project1 - copy.exe")
demofile.Delete
Now i ran project1.exe and then tried running the script to get the permissions error you describe (which is different to what i thought it was)

And then went googleing to find a lovely line of code "on error resume next" which suppresses an error and resumes with the code execution(when set above the error[at the begginning of the script])

so my script became:

Code:
On Error resume next
dim filesys, demofile
set filesys = CreateObject ("Scripting.FileSystemObject")


set demofile = filesys.GetFile("c:\Project1.exe")

demofile.Delete
set demofile = filesys.GetFile("c:\Project1 - copy.exe")
demofile.Delete
And the script deleted "project1 - copy.exe" whilst leaving project1.exe[which was in use] alone...

And did not show an error, so my answer is: add the line "on error resume next" to the beginning of your code to suppress the error

I hope this is a decent answer and i hope it helps you

:grin:

Cheers,
Jamey
 

·
Registered
Joined
·
5 Posts
Discussion Starter #6
Hmm.. it doesn't seem to be working for me. I put "on error resume next" at the beginning of my code and tested it by taking a file that was about a year old and renaming it to have the .log extension so it fits the criteria for deletion. I ran the script and there were no Windows errors, but the test file wasn't deleted. I think what happens is that if it hits the file that throws the error, it will just quit the script and it might not even get to check some of the other files.

I wish I could figure out how to generate a log file.. that would probably help. But, I'm pretty new to VBScript as well and I have no idea how to get that working... :(
 

·
Registered
Joined
·
2,276 Posts
hmm "On Error Resume Next" literally means that if an error occurs just continue with execution... It should work...

Are there functions(/subs i can't remember) within the code? Actually, if you could post the script then i'm sure someone more experienced can take a look at it (I will too but the chances of me spotting anything is slim)...

Cheers,
Jamey
 
1 - 7 of 7 Posts
Status
Not open for further replies.
Top