Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 05-27-2008, 06:21 AM   #1 (permalink)
Registered User
 
Join Date: May 2008
Posts: 4
OS: XP SP1


VBScript Help: File In Use

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!
manekineko is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 05-27-2008, 01:58 PM   #2 (permalink)
Design Team Member
 
jamiemac2005's Avatar
 
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,888
OS: Vista, various linux distros


Re: VBScript Help: File In Use

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
jamiemac2005 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-28-2008, 05:28 AM   #3 (permalink)
Registered User
 
Join Date: May 2008
Posts: 4
OS: XP SP1


Re: VBScript Help: File In Use

Quote:
Originally Posted by jamiemac2005 View Post
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?
manekineko is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-28-2008, 09:16 AM   #4 (permalink)
Design Team Member
 
jamiemac2005's Avatar
 
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,888
OS: Vista, various linux distros


Re: VBScript Help: File In Use

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

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



Cheers,
Jamey

Last edited by jamiemac2005; 05-28-2008 at 09:20 AM.
jamiemac2005 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-29-2008, 07:45 AM   #5 (permalink)
Registered User
 
Join Date: May 2008
Posts: 4
OS: XP SP1


Re: VBScript Help: File In Use

Wow! That sounds amazingly simple and it would be awesome if it worked. I'll give it a shot today and let you know how it goes. Thanks!!!

Last edited by manekineko; 05-29-2008 at 07:47 AM.
manekineko is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-29-2008, 09:53 AM   #6 (permalink)
Registered User
 
Join Date: May 2008
Posts: 4
OS: XP SP1


Re: VBScript Help: File In Use

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... :(
manekineko is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-29-2008, 10:38 AM   #7 (permalink)
Design Team Member
 
jamiemac2005's Avatar
 
Join Date: Jul 2007
Location: Coventry, UK
Posts: 1,888
OS: Vista, various linux distros


Re: VBScript Help: File In Use

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
jamiemac2005 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 12:47 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85