![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| 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: * 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 |
|
|||||||
| Programming A discussion forum for programs and programming used in tech-related businesses. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#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!
|
|
|
|
| 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 |
|
|
#2 (permalink) |
|
Design Team Member
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 |
|
|
|
|
|
#3 (permalink) | |
|
Registered User
Join Date: May 2008
Posts: 4
OS: XP SP1
|
Re: VBScript Help: File In Use
Quote:
Thanks for the suggestions Jamey. Anyone else have any ideas?
|
|
|
|
|
|
|
#4 (permalink) |
|
Design Team Member
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
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 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. |
|
|
|
|
|
#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. |
|
|
|
|
|
#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... :( |
|
|
|
|
|
#7 (permalink) |
|
Design Team Member
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 |
|
|
|
![]() |
| Thread Tools | |
|
|