Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to kill/delete files in a folder

Posted on 2009-04-08
6
Medium Priority
?
563 Views
Last Modified: 2013-11-27
I am working in Access 2002 and I am trying to kill/delete files (if any) in my windows xp explorer folder.  I wrote the following constant to identify the path.  However when it gets to the if statement A_ImportDir_MIC actually equals the path so it drops into my if statement to kill the files even though there are no files in the folder Micros email attachments.  

How can I rewrite the script to make sure its looking for the files within the folder with the *.txt extensions and if there are none get out of the if statement?  Thank you.


<CODE>
Option Compare Database   'Use database order for string comparisons
Const A_ImportDir_MIC = "C:\Micros email attachments\"

'KILL THE FILES
      If A_ImportDir_MIC <> "" Then
        Kill "C:\Micros email attachments\*.txt"
      End If
0
Comment
Question by:Mahonek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 24100888
you could try looping using dir

dim sFolder as string
dim sFile as string

    sFolder = "c:\myfolder\"
    sFile = Dir$(sFolder)
    Do While Len(sFile) > 0
         kill sFolder & sFile
         sFile= Dir$
    Loop

other way is to use the file system object
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 24100911
   Dim fso As Object
    Dim sourceFolder As Object
    Dim f As Object
    Dim sFolder As String
   
   
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    sFolder = "c:\myfolder\"
    Set sourceFolder = fso.GetFolder(sFolder)
    For Each f In sourceFolder.Files
        Kill sFolder & f.Name
    Next f
0
 
LVL 44

Expert Comment

by:GRayL
ID: 24101015
If Dir(A_importDir_MIC & "*.txt") <> "" Then
  Kill A_importDir_MIC & "*.txt"
End If
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 65

Expert Comment

by:rockiroads
ID: 24101040
scripts I did deletes all files. Using the orignal dir method, you can add in *.txt to bring back text files only

a better way to use scripting is to create the file object then use the delete method instead of kill


0
 

Author Closing Comment

by:Mahonek
ID: 31568205
Great!  Thanks.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24101939
No worries, Sorry but I forgot to check for the txt instructions but that can be done by some simple string manipulation
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

688 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