Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

What's it take to delete a file

Posted on 2006-05-16
11
Medium Priority
?
225 Views
Last Modified: 2010-05-18
I'm writing to an INI file, and want to make sure it's deleted before recreating it.

Dim strItem, strSection, oItem, objFSO
strSection = "System Data"
Set objFSO=("Scripting.FileSystemObject")
For Each oItem In QueryWMI("Win32_ComputerSystem")
  With oItem
    strServer = .Caption
    objFSO.DeleteFile "c:\Report\" & strServer & ".ini", True

If I 'pause' here - the file still exists...why?  Even if I destroy objFSO, it's still sitting there... : \
0
Comment
Question by:sirbounty
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16691022
>I'm writing to an INI file, and want to make sure it's deleted before recreating it.
instead of deleting it; open the file in Write mode (instead of Append mode), that will do the same thing...

did you refresh the browser (explorer) ?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16691124
usual thing is to check for the existence of the file then delete
0
 
LVL 67

Author Comment

by:sirbounty
ID: 16691170
Yes, I refreshed.  I can still see it/open it.
Checking for its existence would be a nice safeguard, but if this thing there, it's run before, thus it's likely the file already exists...but good idea.
0
Technology Partners: 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!

 
LVL 3

Expert Comment

by:trainsdse
ID: 16693123
Have you tried testing this code with a hard-coded file name to see if it's a problem with strServer or a problem with the filesystemobject?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16693695
Is the problem just a check, when u open the file and write to it, does your close statement get executed? Im just wondering if the file is not closed, is it leaving it locked?

I would of thought it gets unlocked if u free the object up anyway.

So what did FileExists return?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 16695105
I think that the file still exists at that point is that a buffer still associated with the file handle exists in the program.
It can still exist after the objFSO object is destroyed because the actual memory associated with the object has not actually been cleaned up (flushed, or garbage collected) by the run-time.

0
 
LVL 67

Author Comment

by:sirbounty
ID: 16695116
This is a weekly job.  It writes to the file, then exits.
The 'next' time it runs, I want it to delete the file before it even starts.
So, there's no handle open to it, as I haven't even referenced it before this.  The lines above are the very beginning of the script...
0
 
LVL 2

Accepted Solution

by:
cwile earned 500 total points
ID: 16721541
Have you tried something like this?

Set fso = CreateObject("Scripting.FileSystemObject")
Set aFile = fso.GetFile("c:\Report\" & strServer & ".ini")
aFile.Delete
0
 
LVL 67

Author Comment

by:sirbounty
ID: 16721781
Sorry - same results.
Very strange... :(
0
 
LVL 67

Author Comment

by:sirbounty
ID: 16721804
Ah I believe I found it...
Appears to have been simply declared incorrectly. : (
You'll notice in my initial post, I have:
Set objFSO=("Scripting.FileSystemObject")

which should read
Set objFSO=CreateObject("Scripting.FileSystemObject")

<smack!>
0
 
LVL 67

Author Comment

by:sirbounty
ID: 16721827
That last post helped prompt me, and really was correct.
I just neglected to update that first line in my code:
Set fso = CreateObject("Scripting.FileSystemObject")
which is why it still didn't work.

This is what I have in the end...
Set objFSO=CreateObject("Scripting.FileSystemObject")
For Each oItem In QueryWMI("Win32_ComputerSystem")
  With oItem
    strServer = .Caption
    objFSO.DeleteFile "c:\Report\" & strServer & ".ini", True

Thanx for the help! :^)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Progress
Introduction to Processes

569 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